From 6c941aecbfd47dd09315d995821eb904288f04c1 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Tue, 15 Aug 2023 21:42:36 +0200 Subject: [PATCH] Issue bot - special NotAnalysedTraitRule that runs even with onlyFiles=true --- issue-bot/playground.neon | 1 + src/Rules/Playground/NotAnalysedTraitRule.php | 61 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 src/Rules/Playground/NotAnalysedTraitRule.php diff --git a/issue-bot/playground.neon b/issue-bot/playground.neon index 599cf39732..fe80928a5f 100644 --- a/issue-bot/playground.neon +++ b/issue-bot/playground.neon @@ -1,3 +1,4 @@ rules: - PHPStan\Rules\Playground\FunctionNeverRule - PHPStan\Rules\Playground\MethodNeverRule + - PHPStan\Rules\Playground\NotAnalysedTraitRule diff --git a/src/Rules/Playground/NotAnalysedTraitRule.php b/src/Rules/Playground/NotAnalysedTraitRule.php new file mode 100644 index 0000000000..bf5da0aa79 --- /dev/null +++ b/src/Rules/Playground/NotAnalysedTraitRule.php @@ -0,0 +1,61 @@ + + */ +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; + } + +}