Skip to content

Commit

Permalink
Issue bot - special NotAnalysedTraitRule that runs even with onlyFile…
Browse files Browse the repository at this point in the history
…s=true
  • Loading branch information
ondrejmirtes committed Aug 15, 2023
1 parent 5f59d58 commit 6c941ae
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions issue-bot/playground.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
rules:
- PHPStan\Rules\Playground\FunctionNeverRule
- PHPStan\Rules\Playground\MethodNeverRule
- PHPStan\Rules\Playground\NotAnalysedTraitRule
61 changes: 61 additions & 0 deletions src/Rules/Playground/NotAnalysedTraitRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Playground;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\CollectedDataNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\Traits\TraitDeclarationCollector;
use PHPStan\Rules\Traits\TraitUseCollector;
use function sprintf;
use function strtolower;

/**
* @implements Rule<CollectedDataNode>
*/
class NotAnalysedTraitRule implements Rule
{

public function getNodeType(): string
{
return CollectedDataNode::class;
}

public function processNode(Node $node, Scope $scope): array
{
$traitDeclarationData = $node->get(TraitDeclarationCollector::class);
$traitUseData = $node->get(TraitUseCollector::class);

$declaredTraits = [];
foreach ($traitDeclarationData as $file => $declaration) {
foreach ($declaration as [$name, $line]) {
$declaredTraits[strtolower($name)] = [$file, $name, $line];
}
}

foreach ($traitUseData as $usedNamesData) {
foreach ($usedNamesData as $usedNames) {
foreach ($usedNames as $usedName) {
unset($declaredTraits[strtolower($usedName)]);
}
}
}

$errors = [];
foreach ($declaredTraits as [$file, $name, $line]) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Trait %s is used zero times and is not analysed.',
$name,
))
->file($file)
->line($line)
->tip('See: https://phpstan.org/blog/how-phpstan-analyses-traits')
->build();
}

return $errors;
}

}

0 comments on commit 6c941ae

Please sign in to comment.