Skip to content

Commit 4452972

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

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

src/Reflection/ClassReflection.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,11 @@ public function getEnumCases(): array
945945
foreach ($this->reflection->getCases() as $case) {
946946
$valueType = null;
947947
if ($case instanceof ReflectionEnumBackedCase) {
948-
$valueType = $this->initializerExprTypeResolver->getType($case->getValueExpression(), $initializerExprContext);
948+
try {
949+
$valueType = $this->initializerExprTypeResolver->getType($case->getValueExpression(), $initializerExprContext);
950+
} catch (\LogicException) {
951+
// Enum case does not have a value
952+
}
949953
}
950954
$caseName = $case->getName();
951955
$attributes = $this->attributeReflectionFactory->fromNativeReflection($case->getAttributes(), InitializerExprContext::fromClass($this->getName(), $this->getFileName()));
@@ -972,7 +976,11 @@ public function getEnumCase(string $name): EnumCaseReflection
972976
$case = $this->reflection->getCase($name);
973977
$valueType = null;
974978
if ($case instanceof ReflectionEnumBackedCase) {
975-
$valueType = $this->initializerExprTypeResolver->getType($case->getValueExpression(), InitializerExprContext::fromClassReflection($this));
979+
try {
980+
$valueType = $this->initializerExprTypeResolver->getType($case->getValueExpression(), InitializerExprContext::fromClassReflection($this));
981+
} catch (\LogicException) {
982+
// Enum case does not have a value
983+
}
976984
}
977985

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

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

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

1166+
public function testBug7927(): void
1167+
{
1168+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-7927.php');
1169+
$this->assertCount(2, $errors);
1170+
$this->assertSame('Enum case Bug7927\Test::One does not have a value but the enum is backed with the "int" type.', $errors[0]->getMessage());
1171+
$this->assertSame('Enum case Bug7927\Test::Two does not have a value but the enum is backed with the "int" type.', $errors[1]->getMessage());
1172+
}
1173+
11661174
public function testBug8146(): void
11671175
{
11681176
$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)