Skip to content

Commit 6ed0c9a

Browse files
committed
suport enum, interfaces
1 parent 0b1bcb9 commit 6ed0c9a

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/Rules/Classes/ClassAttributesRule.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,20 @@ public function processNode(Node $node, Scope $scope): array
3939
);
4040

4141
$classReflection = $scope->getClassReflection();
42-
if ($classReflection !== null && $classReflection->isReadOnly()) {
42+
if (
43+
$classReflection !== null
44+
&& ($classReflection->isReadOnly() || $classReflection->isEnum() || $classReflection->isInterface())
45+
) {
46+
$message = 'Attribute class AllowDynamicProperties cannot be used with readonly classes.';
47+
if ($classReflection->isEnum()) {
48+
$message = 'Attribute class AllowDynamicProperties cannot be used with enums.';
49+
}
50+
if ($classReflection->isInterface()) {
51+
$message = 'Attribute class AllowDynamicProperties cannot be used with interface.';
52+
}
53+
4354
if (count($classReflection->getNativeReflection()->getAttributes('AllowDynamicProperties')) > 0) {
44-
$errors[] = RuleErrorBuilder::message(sprintf('Attribute class AllowDynamicProperties cannot be used with readonly classes.', $classReflection->getName()))
55+
$errors[] = RuleErrorBuilder::message($message)
4556
->identifier('class.allowDynamicPropertiesReadonly')
4657
->build();
4758
}

tests/PHPStan/Rules/Classes/ClassAttributesRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,14 @@ public function testBug12281(): void
181181
'Attribute class AllowDynamicProperties cannot be used with readonly classes.',
182182
05,
183183
],
184+
[
185+
'Attribute class AllowDynamicProperties cannot be used with enums.',
186+
12,
187+
],
188+
[
189+
'Attribute class AllowDynamicProperties cannot be used with interface.',
190+
15,
191+
],
184192
]);
185193
}
186194

0 commit comments

Comments
 (0)