-
Notifications
You must be signed in to change notification settings - Fork 478
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
8ec0a96
commit 4a33890
Showing
4 changed files
with
138 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Classes; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\AttributesCheck; | ||
use PHPStan\Rules\Rule; | ||
|
||
/** | ||
* @implements Rule<Node\Stmt\ClassConst> | ||
*/ | ||
class ClassConstantAttributesRule implements Rule | ||
{ | ||
|
||
private AttributesCheck $attributesCheck; | ||
|
||
public function __construct(AttributesCheck $attributesCheck) | ||
{ | ||
$this->attributesCheck = $attributesCheck; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\ClassConst::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
return $this->attributesCheck->check( | ||
$scope, | ||
$node->attrGroups, | ||
\Attribute::TARGET_CLASS_CONSTANT, | ||
'class constant' | ||
); | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
tests/PHPStan/Rules/Classes/ClassConstantAttributesRuleTest.php
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,54 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Classes; | ||
|
||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\AttributesCheck; | ||
use PHPStan\Rules\ClassCaseSensitivityCheck; | ||
use PHPStan\Rules\FunctionCallParametersCheck; | ||
use PHPStan\Rules\NullsafeCheck; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleLevelHelper; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<ClassConstantAttributesRule> | ||
*/ | ||
class ClassConstantAttributesRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
$reflectionProvider = $this->createReflectionProvider(); | ||
return new ClassConstantAttributesRule( | ||
new AttributesCheck( | ||
$reflectionProvider, | ||
new FunctionCallParametersCheck( | ||
new RuleLevelHelper($reflectionProvider, true, false, true), | ||
new NullsafeCheck(), | ||
new PhpVersion(80000), | ||
true, | ||
true, | ||
true, | ||
true | ||
), | ||
new ClassCaseSensitivityCheck($reflectionProvider, false) | ||
) | ||
); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
if (!self::$useStaticReflectionProvider && PHP_VERSION_ID < 80000) { | ||
$this->markTestSkipped('Test requires PHP 8.0.'); | ||
} | ||
|
||
$this->analyse([__DIR__ . '/data/class-constant-attributes.php'], [ | ||
[ | ||
'Attribute class ClassConstantAttributes\Foo does not have the class constant target.', | ||
26, | ||
], | ||
]); | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
tests/PHPStan/Rules/Classes/data/class-constant-attributes.php
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,45 @@ | ||
<?php | ||
|
||
namespace ClassConstantAttributes; | ||
|
||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
class Foo | ||
{ | ||
|
||
} | ||
|
||
#[\Attribute(\Attribute::TARGET_CLASS_CONSTANT)] | ||
class Bar | ||
{ | ||
|
||
} | ||
|
||
#[\Attribute(\Attribute::TARGET_ALL)] | ||
class Baz | ||
{ | ||
|
||
} | ||
|
||
class Lorem | ||
{ | ||
|
||
#[Foo] | ||
private const FOO = 1; | ||
|
||
} | ||
|
||
class Ipsum | ||
{ | ||
|
||
#[Bar] | ||
private const FOO = 1; | ||
|
||
} | ||
|
||
class Dolor | ||
{ | ||
|
||
#[Baz] | ||
private const FOO = 1; | ||
|
||
} |