Skip to content

Commit 83a0ea6

Browse files
committed
refactor
1 parent 6ed0c9a commit 83a0ea6

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

src/Rules/Classes/ClassAttributesRule.php

+13-8
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/data/bug-12281.php

-3
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)