Skip to content

Commit d40ae3e

Browse files
phpstan-botclaude
authored andcommitted
Also report enum case outside of enum in traits
Add trait test case as requested in review. Handle the trait scope separately since traits are analyzed in the context of their consumer class. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ff86aa9 commit d40ae3e

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/Rules/EnumCases/EnumCaseOutsideEnumRule.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ public function getNodeType(): string
2222

2323
public function processNode(Node $node, Scope $scope): array
2424
{
25+
if ($scope->isInTrait()) {
26+
return [
27+
RuleErrorBuilder::message('Enum case can only be used in enums.')
28+
->nonIgnorable()
29+
->identifier('enum.caseOutsideOfEnum')
30+
->build(),
31+
];
32+
}
33+
2534
if (!$scope->isInClass()) {
2635
return [];
2736
}

tests/PHPStan/Rules/EnumCases/EnumCaseOutsideEnumRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public function testRule(): void
2929
'Enum case can only be used in enums.',
3030
14,
3131
],
32+
[
33+
'Enum case can only be used in enums.',
34+
19,
35+
],
3236
]);
3337
}
3438

tests/PHPStan/Rules/EnumCases/data/bug-14252.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ interface Bar
1414
case Active;
1515
}
1616

17+
trait Baz
18+
{
19+
case Active;
20+
}
21+
22+
class BazConsumer
23+
{
24+
use Baz;
25+
}
26+
1727
enum Qux
1828
{
1929
case Active;

0 commit comments

Comments
 (0)