Skip to content

Commit 07bf72b

Browse files
Avoid crashing on enum without value
1 parent 56cea01 commit 07bf72b

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

src/Reflection/ClassReflection.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\Reflection;
44

55
use Attribute;
6+
use LogicException;
67
use PhpParser\Node\Arg;
78
use PhpParser\Node\Expr\StaticCall;
89
use PhpParser\Node\Identifier;
@@ -945,7 +946,11 @@ public function getEnumCases(): array
945946
foreach ($this->reflection->getCases() as $case) {
946947
$valueType = null;
947948
if ($case instanceof ReflectionEnumBackedCase) {
948-
$valueType = $this->initializerExprTypeResolver->getType($case->getValueExpression(), $initializerExprContext);
949+
try {
950+
$valueType = $this->initializerExprTypeResolver->getType($case->getValueExpression(), $initializerExprContext);
951+
} catch (LogicException) {
952+
// Enum case does not have a value
953+
}
949954
}
950955
$caseName = $case->getName();
951956
$attributes = $this->attributeReflectionFactory->fromNativeReflection($case->getAttributes(), InitializerExprContext::fromClass($this->getName(), $this->getFileName()));
@@ -972,7 +977,11 @@ public function getEnumCase(string $name): EnumCaseReflection
972977
$case = $this->reflection->getCase($name);
973978
$valueType = null;
974979
if ($case instanceof ReflectionEnumBackedCase) {
975-
$valueType = $this->initializerExprTypeResolver->getType($case->getValueExpression(), InitializerExprContext::fromClassReflection($this));
980+
try {
981+
$valueType = $this->initializerExprTypeResolver->getType($case->getValueExpression(), InitializerExprContext::fromClassReflection($this));
982+
} catch (LogicException) {
983+
// Enum case does not have a value
984+
}
976985
}
977986

978987
$attributes = $this->attributeReflectionFactory->fromNativeReflection($case->getAttributes(), InitializerExprContext::fromClass($this->getName(), $this->getFileName()));

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,15 @@ public function testBug8537(): void
11631163
$this->assertNoErrors($errors);
11641164
}
11651165

1166+
#[RequiresPhp('>= 8.0')]
1167+
public function testBug7927(): void
1168+
{
1169+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-7927.php');
1170+
$this->assertCount(2, $errors);
1171+
$this->assertSame('Enum case Bug7927\Test::One does not have a value but the enum is backed with the "int" type.', $errors[0]->getMessage());
1172+
$this->assertSame('Enum case Bug7927\Test::Two does not have a value but the enum is backed with the "int" type.', $errors[1]->getMessage());
1173+
}
1174+
11661175
public function testBug8146(): void
11671176
{
11681177
$errors = $this->runAnalyse(__DIR__ . '/data/bug-8146b.php');
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug7927;
4+
5+
enum Test : int
6+
{
7+
case One;
8+
case Two;
9+
}
10+
11+
$v = Test::One;
12+
13+
function doIt(Test $v) : string
14+
{
15+
switch($v)
16+
{
17+
case Test::One:
18+
return "One";
19+
case Test::Two:
20+
return "Two";
21+
default:
22+
throw new \ErrorException("Unknown '{$v->name}'.");
23+
}
24+
}
25+
26+
echo doIt($v);

0 commit comments

Comments
 (0)