-
Notifications
You must be signed in to change notification settings - Fork 430
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0205282
commit c3ce077
Showing
6 changed files
with
198 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Traits; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\CollectedDataNode; | ||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<CollectedDataNode> | ||
*/ | ||
class ConstantsInTraitsRule implements Rule | ||
{ | ||
|
||
public function __construct(private PhpVersion $phpVersion) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return CollectedDataNode::class; | ||
} | ||
|
||
/** | ||
* @param CollectedDataNode $node | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if ($this->phpVersion->supportsConstantsInTraits()) { | ||
return []; | ||
} | ||
|
||
$traitConstantsData = $node->get(TraitConstantsCollector::class); | ||
|
||
$errors = []; | ||
|
||
foreach ($traitConstantsData as $file => $collectedData) { | ||
foreach ($collectedData as $classConstants) { | ||
foreach ($classConstants as $constants) { | ||
foreach ($constants as [$trait, $const, $line]) { | ||
$errors[] = RuleErrorBuilder::message(sprintf( | ||
'Constant %s::%s is declared inside a trait but is only supported on PHP 8.2 and later.', | ||
$trait, | ||
$const, | ||
)) | ||
->file($file) | ||
->line($line) | ||
->nonIgnorable() | ||
->identifier('constants.inTraits') | ||
->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,47 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Traits; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Collectors\Collector; | ||
use PHPStan\ShouldNotHappenException; | ||
use function array_map; | ||
use function array_values; | ||
|
||
/** | ||
* @implements Collector<Node\Stmt\Trait_, list<array<int, array{string, string, int}>>> | ||
*/ | ||
class TraitConstantsCollector implements Collector | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\Trait_::class; | ||
} | ||
|
||
/** | ||
* @param Node\Stmt\Trait_ $node | ||
*/ | ||
public function processNode(Node $node, Scope $scope) | ||
{ | ||
$traitName = $node->namespacedName?->toString() ?? $node->name?->toString(); | ||
|
||
if ($traitName === null) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
|
||
return array_values(array_map( | ||
static fn (Node\Stmt\ClassConst $classConst): array => array_map( | ||
static fn (Node\Const_ $const): array => [ | ||
$traitName, | ||
$const->name->toString(), | ||
$classConst->getLine(), | ||
], | ||
$classConst->consts, | ||
), | ||
$node->getConstants(), | ||
)); | ||
} | ||
|
||
} |
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,68 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Traits; | ||
|
||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\Traits\TraitConstantsCollector; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<ConstantsInTraitsRule> | ||
*/ | ||
class ConstantsInTraitsRuleTest extends RuleTestCase | ||
{ | ||
|
||
private int $phpVersionId; | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new ConstantsInTraitsRule(new PhpVersion($this->phpVersionId)); | ||
} | ||
|
||
protected function getCollectors(): array | ||
{ | ||
return [ | ||
new TraitConstantsCollector(), | ||
]; | ||
} | ||
|
||
public function dataRule(): array | ||
{ | ||
return [ | ||
[ | ||
80100, | ||
[ | ||
[ | ||
'Constant ConstantsInTraits\FooBar::FOO is declared inside a trait but is only supported on PHP 8.2 and later.', | ||
7, | ||
], | ||
[ | ||
'Constant ConstantsInTraits\FooBar::BAR is declared inside a trait but is only supported on PHP 8.2 and later.', | ||
8, | ||
], | ||
[ | ||
'Constant ConstantsInTraits\FooBar::QUX is declared inside a trait but is only supported on PHP 8.2 and later.', | ||
8, | ||
], | ||
], | ||
], | ||
[ | ||
80200, | ||
[], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider dataRule | ||
* | ||
* @param list<array{0: string, 1: int, 2?: string}> $errors | ||
*/ | ||
public function testRule(int $phpVersionId, array $errors): void | ||
{ | ||
$this->phpVersionId = $phpVersionId; | ||
$this->analyse([__DIR__ . '/data/constants-in-traits.php'], $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,9 @@ | ||
<?php // lint >= 8.2 | ||
|
||
namespace ConstantsInTraits; | ||
|
||
trait FooBar | ||
{ | ||
const FOO = 'foo'; | ||
public const BAR = 'bar', QUX = 'qux'; | ||
} |