-
Notifications
You must be signed in to change notification settings - Fork 426
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 9ea56ec
Showing
5 changed files
with
127 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,51 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Traits; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleError; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use function array_map; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<Node\Stmt\ClassConst> | ||
*/ | ||
class ConstantsInTraitsRule implements Rule | ||
{ | ||
|
||
public function __construct(private PhpVersion $phpVersion) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\ClassConst::class; | ||
} | ||
|
||
/** | ||
* @param Node\Stmt\ClassConst $node | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if ($this->phpVersion->supportsConstantsInTraits()) { | ||
return []; | ||
} | ||
|
||
if (!$scope->isInTrait()) { | ||
return []; | ||
} | ||
|
||
return array_map( | ||
static fn (Node\Const_ $const): RuleError => RuleErrorBuilder::message(sprintf( | ||
'Constant %s is declared inside a trait but is only supported on PHP 8.2 and later.', | ||
$const->name->toString(), | ||
))->identifier('constants.inTraits')->nonIgnorable()->build(), | ||
$node->consts, | ||
); | ||
} | ||
|
||
} |
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\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)); | ||
} | ||
|
||
public function dataRule(): array | ||
{ | ||
return [ | ||
[ | ||
80100, | ||
[ | ||
[ | ||
'Constant FOO is declared inside a trait but is only supported on PHP 8.2 and later.', | ||
7, | ||
], | ||
[ | ||
'Constant BAR is declared inside a trait but is only supported on PHP 8.2 and later.', | ||
8, | ||
], | ||
[ | ||
'Constant 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'; | ||
} |