-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue bot - special NotAnalysedTraitRule that runs even with onlyFile…
…s=true
- Loading branch information
1 parent
5f59d58
commit 6c941ae
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |