-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First collector rule: NotAnalysedTraitRule
- Loading branch information
1 parent
199dee4
commit c4d0527
Showing
8 changed files
with
185 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
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
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
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,55 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Traits; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\CollectedDataNode; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
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)->build(); | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
} |
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,29 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Traits; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Collectors\Collector; | ||
|
||
/** | ||
* @implements Collector<Node\Stmt\Trait_, array{string, int}> | ||
*/ | ||
class TraitDeclarationCollector implements Collector | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\Trait_::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope) | ||
{ | ||
if ($node->namespacedName === null) { | ||
return null; | ||
} | ||
|
||
return [$node->namespacedName->toString(), $node->getLine()]; | ||
} | ||
|
||
} |
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,26 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Traits; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Collectors\Collector; | ||
use function array_map; | ||
|
||
/** | ||
* @implements Collector<Node\Stmt\TraitUse, list<int, string>> | ||
This comment has been minimized.
Sorry, something went wrong. |
||
*/ | ||
class TraitUseCollector implements Collector | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\TraitUse::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope) | ||
{ | ||
return array_map(static fn (Node\Name $traitName) => $traitName->toString(), $node->traits); | ||
} | ||
|
||
} |
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,37 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Traits; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<NotAnalysedTraitRule> | ||
*/ | ||
class NotAnalysedTraitRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new NotAnalysedTraitRule(); | ||
} | ||
|
||
protected function getCollectors(): array | ||
{ | ||
return [ | ||
new TraitDeclarationCollector(), | ||
new TraitUseCollector(), | ||
]; | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/not-analysed-trait.php'], [ | ||
[ | ||
'Trait NotAnalysedTrait\Bar is used zero times and is not analysed.', | ||
10, | ||
], | ||
]); | ||
} | ||
|
||
} |
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,20 @@ | ||
<?php | ||
|
||
namespace NotAnalysedTrait; | ||
|
||
trait Foo | ||
{ | ||
|
||
} | ||
|
||
trait Bar | ||
{ | ||
|
||
} | ||
|
||
class UsesFoo | ||
{ | ||
|
||
use Foo; | ||
|
||
} |
does this class miss a
- phpstan.rules.rule
tag?