Skip to content

Commit f64b27c

Browse files
committed
Process enum case expression
1 parent 59e3e94 commit f64b27c

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

Diff for: src/Analyser/NodeScopeResolver.php

+7
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,13 @@ private function processStmtNode(
14861486
$scope->getNativeType($const->value),
14871487
);
14881488
}
1489+
} elseif ($stmt instanceof Node\Stmt\EnumCase) {
1490+
$hasYield = false;
1491+
$throwPoints = [];
1492+
$this->processAttributeGroups($stmt->attrGroups, $scope, $nodeCallback);
1493+
if ($stmt->expr !== null) {
1494+
$this->processExprNode($stmt->expr, $scope, $nodeCallback, ExpressionContext::createDeep());
1495+
}
14891496
} elseif ($stmt instanceof Node\Stmt\Nop) {
14901497
$hasYield = false;
14911498
$throwPoints = $overridingThrowPoints ?? [];

Diff for: tests/PHPStan/Node/AttributeArgRuleTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\Node;
66
use PHPStan\Rules\Rule;
77
use PHPStan\Testing\RuleTestCase;
8+
use const PHP_VERSION_ID;
89

910
/**
1011
* @extends RuleTestCase<Rule>
@@ -42,4 +43,18 @@ public function testRule(string $file, string $expectedError, array $lines): voi
4243
$this->analyse([$file], $errors);
4344
}
4445

46+
public function testEnumCaseAttribute(): void
47+
{
48+
if (PHP_VERSION_ID < 80100) {
49+
$this->markTestSkipped('Test requires PHP 8.1.');
50+
}
51+
52+
$this->analyse([__DIR__ . '/data/enum-case-attribute.php'], [
53+
[
54+
AttributeArgRule::ERROR_MESSAGE,
55+
10,
56+
],
57+
]);
58+
}
59+
4560
}

Diff for: tests/PHPStan/Node/data/enum-case-attribute.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php // lint >= 8.1
2+
3+
namespace EnumCaseAttributeCheck;
4+
5+
use NodeCallbackCalled\UniversalAttribute;
6+
7+
enum Foo
8+
{
9+
10+
#[UniversalAttribute(1)]
11+
case TEST;
12+
13+
}

Diff for: tests/PHPStan/Rules/DeadCode/UnusedPrivateConstantRuleTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,13 @@ public function testBug8204(): void
8383
$this->analyse([__DIR__ . '/data/bug-8204.php'], []);
8484
}
8585

86+
public function testBug9005(): void
87+
{
88+
if (PHP_VERSION_ID < 80100) {
89+
$this->markTestSkipped('Test requires PHP 8.1.');
90+
}
91+
92+
$this->analyse([__DIR__ . '/data/bug-9005.php'], []);
93+
}
94+
8695
}

Diff for: tests/PHPStan/Rules/DeadCode/data/bug-9005.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php // lint >= 8.1
2+
3+
namespace Bug9005;
4+
5+
enum Test: string
6+
{
7+
private const PREFIX = 'my-stuff-';
8+
9+
case TESTING = self::PREFIX . 'test';
10+
11+
case TESTING2 = self::PREFIX . 'test2';
12+
}

0 commit comments

Comments
 (0)