Skip to content

Commit

Permalink
Process enum case expression
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 6, 2023
1 parent 59e3e94 commit f64b27c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,13 @@ private function processStmtNode(
$scope->getNativeType($const->value),
);
}
} elseif ($stmt instanceof Node\Stmt\EnumCase) {
$hasYield = false;
$throwPoints = [];
$this->processAttributeGroups($stmt->attrGroups, $scope, $nodeCallback);
if ($stmt->expr !== null) {
$this->processExprNode($stmt->expr, $scope, $nodeCallback, ExpressionContext::createDeep());
}
} elseif ($stmt instanceof Node\Stmt\Nop) {
$hasYield = false;
$throwPoints = $overridingThrowPoints ?? [];
Expand Down
15 changes: 15 additions & 0 deletions tests/PHPStan/Node/AttributeArgRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PhpParser\Node;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use const PHP_VERSION_ID;

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

public function testEnumCaseAttribute(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->analyse([__DIR__ . '/data/enum-case-attribute.php'], [
[
AttributeArgRule::ERROR_MESSAGE,
10,
],
]);
}

}
13 changes: 13 additions & 0 deletions tests/PHPStan/Node/data/enum-case-attribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php // lint >= 8.1

namespace EnumCaseAttributeCheck;

use NodeCallbackCalled\UniversalAttribute;

enum Foo
{

#[UniversalAttribute(1)]
case TEST;

}
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,13 @@ public function testBug8204(): void
$this->analyse([__DIR__ . '/data/bug-8204.php'], []);
}

public function testBug9005(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->analyse([__DIR__ . '/data/bug-9005.php'], []);
}

}
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/DeadCode/data/bug-9005.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php // lint >= 8.1

namespace Bug9005;

enum Test: string
{
private const PREFIX = 'my-stuff-';

case TESTING = self::PREFIX . 'test';

case TESTING2 = self::PREFIX . 'test2';
}

0 comments on commit f64b27c

Please sign in to comment.