Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug commands #647

Merged
merged 4 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
use Qossmic\Deptrac\AstRunner\Resolver\AnonymousClassResolver;
use Qossmic\Deptrac\AstRunner\Resolver\ClassConstantResolver;
use Qossmic\Deptrac\AstRunner\Resolver\TypeResolver;
use Qossmic\Deptrac\ClassLikeAnalyser;
use Qossmic\Deptrac\ClassLikeLayerResolverFactory;
use Qossmic\Deptrac\Collector\BoolCollector;
use Qossmic\Deptrac\Collector\ClassNameCollector;
use Qossmic\Deptrac\Collector\ClassNameRegexCollector;
use Qossmic\Deptrac\Collector\DirectoryCollector;
use Qossmic\Deptrac\Collector\ExtendsCollector;
use Qossmic\Deptrac\Collector\FunctionNameCollector;
use Qossmic\Deptrac\Collector\ImplementsCollector;
use Qossmic\Deptrac\Collector\InheritanceLevelCollector;
use Qossmic\Deptrac\Collector\InheritsCollector;
Expand All @@ -31,8 +30,8 @@
use Qossmic\Deptrac\Configuration\Loader\YmlFileLoader;
use Qossmic\Deptrac\Configuration\ParameterResolver;
use Qossmic\Deptrac\Console\Command\AnalyseCommand;
use Qossmic\Deptrac\Console\Command\DebugClassLikeCommand;
use Qossmic\Deptrac\Console\Command\DebugLayerCommand;
use Qossmic\Deptrac\Console\Command\DebugTokenCommand;
use Qossmic\Deptrac\Console\Command\DebugUnassignedCommand;
use Qossmic\Deptrac\Console\Command\InitCommand;
use Qossmic\Deptrac\Dependency\InheritanceFlatter;
Expand All @@ -55,6 +54,8 @@
use Qossmic\Deptrac\OutputFormatter\XMLOutputFormatter;
use Qossmic\Deptrac\OutputFormatterFactory;
use Qossmic\Deptrac\RulesetEngine;
use Qossmic\Deptrac\TokenAnalyser;
use Qossmic\Deptrac\TokenLayerResolverFactory;
use Qossmic\Deptrac\UnassignedAnalyser;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use function Symfony\Component\DependencyInjection\Loader\Configurator\param;
Expand Down Expand Up @@ -102,7 +103,7 @@
$services->set(TypeResolver::class);

$services
->set(ClassLikeLayerResolverFactory::class)
->set(TokenLayerResolverFactory::class)
->args([
service(Registry::class),
service(ParameterResolver::class),
Expand All @@ -115,31 +116,31 @@
service(FileResolver::class),
service(Resolver::class),
service(RulesetEngine::class),
service(ClassLikeLayerResolverFactory::class),
service(TokenLayerResolverFactory::class),
]);

$services
->set(ClassLikeAnalyser::class)
->set(TokenAnalyser::class)
->args([
service(AstRunner::class),
service(FileResolver::class),
service(ClassLikeLayerResolverFactory::class),
service(TokenLayerResolverFactory::class),
]);

$services
->set(LayerAnalyser::class)
->args([
service(AstRunner::class),
service(FileResolver::class),
service(ClassLikeLayerResolverFactory::class),
service(TokenLayerResolverFactory::class),
]);

$services
->set(UnassignedAnalyser::class)
->args([
service(AstRunner::class),
service(FileResolver::class),
service(ClassLikeLayerResolverFactory::class),
service(TokenLayerResolverFactory::class),
]);

$services->set(RulesetEngine::class);
Expand Down Expand Up @@ -195,6 +196,9 @@
$services
->set(ClassNameCollector::class)
->tag('collector');
$services
->set(FunctionNameCollector::class)
->tag('collector');
$services
->set(ClassNameRegexCollector::class)
->tag('collector');
Expand Down Expand Up @@ -257,9 +261,9 @@
]);

$services
->set(DebugClassLikeCommand::class)
->set(DebugTokenCommand::class)
->args([
service(ClassLikeAnalyser::class),
service(TokenAnalyser::class),
service(Loader::class),
]);

Expand Down
9 changes: 4 additions & 5 deletions docs/debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ your depfile. All commands output one issue per line and can therefore be easily
combined with other tools like `wc` or `grep`.

* [`debug:layer`](#debuglayer)
* [`debug:class`](#debugclass)
* [`debug:token`](#debugtoken)
* [`debug:unassigned`](#debugunassigned)

## `debug:layer`
Expand All @@ -21,13 +21,12 @@ examples\Layer1\SomeClass
examples\Layer1\SomeClass2
```

## `debug:class`
## `debug:token`

The `debug:class`-command will let you know which layers a specified class-like
belongs to.
The `debug:token` (previously `debug:class-like`)-command will let you know which layers a specified token belongs to.

```bash
php deptrac.phar debug:class-like examples/DirectoryLayer.depfile.yaml 'examples\Layer1\AnotherClassLikeAController'
php deptrac.phar debug:token examples/DirectoryLayer.depfile.yaml 'examples\Layer1\AnotherClassLikeAController' class-like

Controller
Layer1
Expand Down
4 changes: 2 additions & 2 deletions src/Analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ class Analyser
private FileResolver $fileResolver;
private Resolver $resolver;
private RulesetEngine $rulesetEngine;
private ClassLikeLayerResolverFactory $classLikeLayerResolverFactory;
private TokenLayerResolverFactory $classLikeLayerResolverFactory;

public function __construct(
AstRunner $astRunner,
FileResolver $fileResolver,
Resolver $resolver,
RulesetEngine $rulesetEngine,
ClassLikeLayerResolverFactory $classLikeLayerResolverFactory
TokenLayerResolverFactory $classLikeLayerResolverFactory
) {
$this->astRunner = $astRunner;
$this->fileResolver = $fileResolver;
Expand Down
12 changes: 12 additions & 0 deletions src/AstRunner/AstMap/SuperGlobalName.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@

final class SuperGlobalName implements TokenName
{
public const ALLOWED_NAMES = [
'GLOBALS',
'_SERVER',
'_GET',
'_POST',
'_FILES',
'_COOKIE',
'_SESSION',
'_REQUEST',
'_ENV',
];

private string $name;

public function __construct(string $name)
Expand Down
15 changes: 2 additions & 13 deletions src/AstRunner/AstParser/NikicPhpParser/FileReferenceVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,13 @@
use Qossmic\Deptrac\AstRunner\AstMap\ClassReferenceBuilder;
use Qossmic\Deptrac\AstRunner\AstMap\FileReferenceBuilder;
use Qossmic\Deptrac\AstRunner\AstMap\ReferenceBuilder;
use Qossmic\Deptrac\AstRunner\AstMap\SuperGlobalName;
use Qossmic\Deptrac\AstRunner\Resolver\DependencyResolver;
use Qossmic\Deptrac\AstRunner\Resolver\TypeResolver;
use Qossmic\Deptrac\AstRunner\Resolver\TypeScope;

class FileReferenceVisitor extends NodeVisitorAbstract
{
private const SUPERGLOBALS = [
'GLOBALS',
'_SERVER',
'_GET',
'_POST',
'_FILES',
'_COOKIE',
'_SESSION',
'_REQUEST',
'_ENV',
];

private FileReferenceBuilder $fileReferenceBuilder;

/** @var DependencyResolver[] */
Expand Down Expand Up @@ -182,7 +171,7 @@ public function leaveNode(Node $node)
}
}

if ($node instanceof Node\Expr\Variable && in_array($node->name, self::SUPERGLOBALS, true)) {
if ($node instanceof Node\Expr\Variable && in_array($node->name, SuperGlobalName::ALLOWED_NAMES, true)) {
$this->currentReference->superglobal($node->name, $node->getLine());
}

Expand Down
6 changes: 3 additions & 3 deletions src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use Psr\Container\ContainerInterface;
use Qossmic\Deptrac\Console\Command\AnalyseCommand;
use Qossmic\Deptrac\Console\Command\DebugClassLikeCommand;
use Qossmic\Deptrac\Console\Command\DebugLayerCommand;
use Qossmic\Deptrac\Console\Command\DebugTokenCommand;
use Qossmic\Deptrac\Console\Command\DebugUnassignedCommand;
use Qossmic\Deptrac\Console\Command\InitCommand;
use Qossmic\Deptrac\ContainerBuilder;
Expand Down Expand Up @@ -45,8 +45,8 @@ public function doRun(InputInterface $input, OutputInterface $output): int
/** @var AnalyseCommand $analyseCommand */
$analyseCommand = $container->get(AnalyseCommand::class);

/** @var DebugClassLikeCommand $debugClassLikeCommand */
$debugClassLikeCommand = $container->get(DebugClassLikeCommand::class);
/** @var DebugTokenCommand $debugClassLikeCommand */
$debugClassLikeCommand = $container->get(DebugTokenCommand::class);

/** @var DebugLayerCommand $debugLayerCommand */
$debugLayerCommand = $container->get(DebugLayerCommand::class);
Expand Down
60 changes: 0 additions & 60 deletions src/Console/Command/DebugClassLikeCommand.php

This file was deleted.

80 changes: 80 additions & 0 deletions src/Console/Command/DebugTokenCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace Qossmic\Deptrac\Console\Command;

use Qossmic\Deptrac\AstRunner\AstMap\ClassLikeName;
use Qossmic\Deptrac\AstRunner\AstMap\FileName;
use Qossmic\Deptrac\AstRunner\AstMap\FunctionName;
use Qossmic\Deptrac\Configuration\Loader;
use Qossmic\Deptrac\Console\Command\Exception\SingleDepfileIsRequiredException;
use Qossmic\Deptrac\TokenAnalyser;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class DebugTokenCommand extends Command
{
private TokenAnalyser $analyser;
private Loader $loader;

public function __construct(
TokenAnalyser $analyser,
Loader $loader
) {
parent::__construct();

$this->analyser = $analyser;
$this->loader = $loader;
}

protected function configure(): void
{
$this->setName('debug:token');
$this->setAliases(['debug:class-like']);

$this->addArgument('depfile', InputArgument::REQUIRED, 'Path to the depfile');
$this->addArgument('token', InputArgument::REQUIRED, 'Full qualified token name to debug');
$this->addArgument('type', InputArgument::OPTIONAL, 'Token type (class-like, function, file)', 'class-like');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$depfile = $input->getArgument('depfile');

if (!is_string($depfile)) {
throw SingleDepfileIsRequiredException::fromArgument($depfile);
}

/** @var string $tokenName */
$tokenName = $input->getArgument('token');
/** @var string $tokenType */
$tokenType = $input->getArgument('type');

$configuration = $this->loader->load($depfile);

switch ($tokenType) {
case 'class-like':
$token = ClassLikeName::fromFQCN($tokenName);
break;
case 'function':
$token = FunctionName::fromFQCN($tokenName);
break;
case 'file':
$token = new FileName($tokenName);
break;
default:
throw new \RuntimeException('Invalid token type "'.$tokenType.'". Only "class-like", "function" or "file" are supported.');
}

$layers = $this->analyser->analyse($configuration, $token);

natcasesort($layers);

$output->writeln($layers);

return 0;
}
}
4 changes: 2 additions & 2 deletions src/LayerAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class LayerAnalyser
{
private AstRunner $astRunner;
private FileResolver $fileResolver;
private ClassLikeLayerResolverFactory $classLikeLayerResolverFactory;
private TokenLayerResolverFactory $classLikeLayerResolverFactory;

public function __construct(
AstRunner $astRunner,
FileResolver $fileResolver,
ClassLikeLayerResolverFactory $classLikeLayerResolverFactory
TokenLayerResolverFactory $classLikeLayerResolverFactory
) {
$this->astRunner = $astRunner;
$this->fileResolver = $fileResolver;
Expand Down
Loading