Skip to content

Commit a345704

Browse files
kukulichondrejmirtes
authored andcommitted
Added EnumCaseAttributesRule
1 parent 79e0a79 commit a345704

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\EnumCases;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Rules\AttributesCheck;
8+
use PHPStan\Rules\Rule;
9+
10+
/**
11+
* @implements Rule<Node\Stmt\EnumCase>
12+
*/
13+
class EnumCaseAttributesRule implements Rule
14+
{
15+
16+
private AttributesCheck $attributesCheck;
17+
18+
public function __construct(AttributesCheck $attributesCheck)
19+
{
20+
$this->attributesCheck = $attributesCheck;
21+
}
22+
23+
public function getNodeType(): string
24+
{
25+
return Node\Stmt\EnumCase::class;
26+
}
27+
28+
public function processNode(Node $node, Scope $scope): array
29+
{
30+
return $this->attributesCheck->check(
31+
$scope,
32+
$node->attrGroups,
33+
\Attribute::TARGET_CLASS_CONSTANT,
34+
'class constant'
35+
);
36+
}
37+
38+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\EnumCases;
4+
5+
use PHPStan\Php\PhpVersion;
6+
use PHPStan\Rules\AttributesCheck;
7+
use PHPStan\Rules\ClassCaseSensitivityCheck;
8+
use PHPStan\Rules\FunctionCallParametersCheck;
9+
use PHPStan\Rules\NullsafeCheck;
10+
use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper;
11+
use PHPStan\Rules\Rule;
12+
use PHPStan\Rules\RuleLevelHelper;
13+
use PHPStan\Testing\RuleTestCase;
14+
15+
/**
16+
* @extends RuleTestCase<EnumCaseAttributesRule>
17+
*/
18+
class EnumCaseAttributesRuleTest extends RuleTestCase
19+
{
20+
21+
protected function getRule(): Rule
22+
{
23+
$reflectionProvider = $this->createReflectionProvider();
24+
return new EnumCaseAttributesRule(
25+
new AttributesCheck(
26+
$reflectionProvider,
27+
new FunctionCallParametersCheck(
28+
new RuleLevelHelper($reflectionProvider, true, false, true, false),
29+
new NullsafeCheck(),
30+
new PhpVersion(80100),
31+
new UnresolvableTypeHelper(),
32+
true,
33+
true,
34+
true,
35+
true
36+
),
37+
new ClassCaseSensitivityCheck($reflectionProvider, false)
38+
)
39+
);
40+
}
41+
42+
public function testRule(): void
43+
{
44+
if (!self::$useStaticReflectionProvider && PHP_VERSION_ID < 80100) {
45+
$this->markTestSkipped('Test requires PHP 8.1.');
46+
}
47+
48+
$this->analyse([__DIR__ . '/data/enum-case-attributes.php'], [
49+
[
50+
'Attribute class EnumCaseAttributes\AttributeWithPropertyTarget does not have the class constant target.',
51+
26,
52+
],
53+
]);
54+
}
55+
56+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php // lint >= 8.1
2+
3+
namespace EnumCaseAttributes;
4+
5+
#[\Attribute(\Attribute::TARGET_PROPERTY)]
6+
class AttributeWithPropertyTarget
7+
{
8+
9+
}
10+
11+
#[\Attribute(\Attribute::TARGET_CLASS_CONSTANT)]
12+
class AttributeWithClassConstantTarget
13+
{
14+
15+
}
16+
17+
#[\Attribute(\Attribute::TARGET_ALL)]
18+
class AttributeWithTargetAll
19+
{
20+
21+
}
22+
23+
enum Lorem
24+
{
25+
26+
#[AttributeWithPropertyTarget]
27+
case FOO;
28+
29+
}
30+
31+
enum Ipsum
32+
{
33+
34+
#[AttributeWithClassConstantTarget]
35+
case FOO;
36+
37+
}
38+
39+
enum Dolor
40+
{
41+
42+
#[AttributeWithTargetAll]
43+
case FOO;
44+
45+
}

0 commit comments

Comments
 (0)