Skip to content

Commit 24f916f

Browse files
committed
refactor
1 parent 2ca9940 commit 24f916f

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

src/Rules/Classes/ClassAttributesRule.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,27 @@ public function processNode(Node $node, Scope $scope): array
3838
'class',
3939
);
4040

41-
$classReflection = $scope->getClassReflection();
41+
$classReflection = $node->getClassReflection();
4242
if (
43-
$classReflection !== null
44-
&& ($classReflection->isReadOnly() || $classReflection->isEnum() || $classReflection->isInterface())
43+
$classReflection->isReadOnly()
44+
|| $classReflection->isEnum()
45+
|| $classReflection->isInterface()
4546
) {
46-
$message = 'Attribute class AllowDynamicProperties cannot be used with readonly classes.';
47+
$typeName = 'readonly class';
48+
$identifier = 'class.allowDynamicPropertiesReadonly';
4749
if ($classReflection->isEnum()) {
48-
$message = 'Attribute class AllowDynamicProperties cannot be used with enums.';
50+
$typeName = 'enum';
51+
$identifier = 'class.allowDynamicPropertiesEnum';
4952
}
5053
if ($classReflection->isInterface()) {
51-
$message = 'Attribute class AllowDynamicProperties cannot be used with interface.';
54+
$typeName = 'interface';
55+
$identifier = 'class.allowDynamicPropertiesInterface';
5256
}
5357

5458
if (count($classReflection->getNativeReflection()->getAttributes('AllowDynamicProperties')) > 0) {
55-
$errors[] = RuleErrorBuilder::message($message)
56-
->identifier('class.allowDynamicPropertiesReadonly')
59+
$errors[] = RuleErrorBuilder::message(sprintf('Attribute class AllowDynamicProperties cannot be used with %s.', $typeName))
60+
->identifier($identifier)
61+
->nonIgnorable()
5762
->build();
5863
}
5964
}

tests/PHPStan/Rules/Classes/ClassAttributesRuleTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,11 @@ public function testBug12281(): void
178178
$this->checkImplicitMixed = true;
179179
$this->analyse([__DIR__ . '/data/bug-12281.php'], [
180180
[
181-
'Attribute class AllowDynamicProperties cannot be used with readonly classes.',
181+
'Attribute class AllowDynamicProperties cannot be used with readonly class.',
182182
05,
183183
],
184184
[
185-
'Attribute class AllowDynamicProperties cannot be used with enums.',
185+
'Attribute class AllowDynamicProperties cannot be used with enum.',
186186
12,
187187
],
188188
[

tests/PHPStan/Rules/Classes/data/bug-12281.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,3 @@ enum BlogDataEnum { /* … */ }
1414

1515
#[\AllowDynamicProperties]
1616
interface BlogDataInterface { /* … */ }
17-
18-
#[\AllowDynamicProperties]
19-
trait BlogDataTrait { /* … */ }

0 commit comments

Comments
 (0)