Skip to content

Commit

Permalink
feat: Display token types in debug:layer output (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickkusebauch authored Mar 25, 2024
1 parent fb63701 commit cd5bb85
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
19 changes: 14 additions & 5 deletions docs/debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@ With the `debug:layer`-command you can list all tokens which are matched in
a specific layer. This command only shows tokens that would be emitted by your analyser configuration.

```console
$ php deptrac.phar debug:layer --config-file=examples/DirectoryLayer.depfile.yaml Layer1

examples\Layer1\AnotherClassLikeAController
examples\Layer1\SomeClass
examples\Layer1\SomeClass2
$ php deptrac.phar debug:layer --config-file=deptrac.config.php Time

---------------------------------------------------- ------------
Time Token Type
---------------------------------------------------- ------------
/src/Supportive/Time/Period.php file
/src/Supportive/Time/StartedPeriod.php file
/src/Supportive/Time/Stopwatch.php file
/src/Supportive/Time/StopwatchException.php file
Qossmic\Deptrac\Supportive\Time\Period class-like
Qossmic\Deptrac\Supportive\Time\StartedPeriod class-like
Qossmic\Deptrac\Supportive\Time\Stopwatch class-like
Qossmic\Deptrac\Supportive\Time\StopwatchException class-like
---------------------------------------------------- ------------
```

## `debug:token`
Expand Down
14 changes: 5 additions & 9 deletions src/Core/Analyser/TokenInLayerAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

use function array_values;
use function in_array;
use function natcasesort;

class TokenInLayerAnalyser
{
Expand All @@ -43,7 +42,7 @@ public function __construct(
}

/**
* @return string[]
* @return list<array{string, string}>
*
* @throws AnalyserException
*/
Expand All @@ -58,8 +57,7 @@ public function findTokensInLayer(string $layer): array
foreach ($astMap->getClassLikeReferences() as $classReference) {
$classToken = $this->tokenResolver->resolve($classReference->getToken(), $astMap);
if (array_key_exists($layer, $this->layerResolver->getLayersForReference($classToken))) {
$matchingTokens[] = $classToken->getToken()
->toString();
$matchingTokens[] = [$classToken->getToken()->toString(), TokenType::CLASS_LIKE->value];
}
}
}
Expand All @@ -68,8 +66,7 @@ public function findTokensInLayer(string $layer): array
foreach ($astMap->getFunctionReferences() as $functionReference) {
$functionToken = $this->tokenResolver->resolve($functionReference->getToken(), $astMap);
if (array_key_exists($layer, $this->layerResolver->getLayersForReference($functionToken))) {
$matchingTokens[] = $functionToken->getToken()
->toString();
$matchingTokens[] = [$functionToken->getToken()->toString(), TokenType::FUNCTION->value];
}
}
}
Expand All @@ -78,13 +75,12 @@ public function findTokensInLayer(string $layer): array
foreach ($astMap->getFileReferences() as $fileReference) {
$fileToken = $this->tokenResolver->resolve($fileReference->getToken(), $astMap);
if (array_key_exists($layer, $this->layerResolver->getLayersForReference($fileToken))) {
$matchingTokens[] = $fileToken->getToken()
->toString();
$matchingTokens[] = [$fileToken->getToken()->toString(), TokenType::FILE->value];
}
}
}

natcasesort($matchingTokens);
uasort($matchingTokens, static fn (array $a, array $b): int => $a[0] <=> $b[0]);

return array_values($matchingTokens);
} catch (UnrecognizedTokenException $e) {
Expand Down
7 changes: 1 addition & 6 deletions src/Supportive/Console/Command/DebugLayerRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ public function run(?string $layer, OutputInterface $output): void

try {
foreach ($debugLayers as $debugLayer) {
$matchedLayers = array_map(
static fn (string $token) => (array) $token,
$this->analyser->findTokensInLayer($debugLayer)
);

$output->getStyle()->table([$debugLayer], $matchedLayers);
$output->getStyle()->table([$debugLayer, 'Token Type'], $this->analyser->findTokensInLayer($debugLayer));
}
} catch (AnalyserException $e) {
throw CommandRunException::analyserException($e);
Expand Down

0 comments on commit cd5bb85

Please sign in to comment.