Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6bdce5d

Browse files
Avoid crashing on enum without value (#4336)
1 parent 71f7b78 commit 6bdce5d

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

‎src/Reflection/ClassReflection.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ public function getEnumCases(): array
964964
$initializerExprContext = InitializerExprContext::fromClassReflection($this);
965965
foreach ($this->reflection->getCases() as $case) {
966966
$valueType = null;
967-
if ($case instanceof ReflectionEnumBackedCase) {
967+
if ($case instanceof ReflectionEnumBackedCase && $case->hasBackingValue()) {
968968
$valueType = $this->initializerExprTypeResolver->getType($case->getValueExpression(), $initializerExprContext);
969969
}
970970
$caseName = $case->getName();
@@ -991,7 +991,7 @@ public function getEnumCase(string $name): EnumCaseReflection
991991

992992
$case = $this->reflection->getCase($name);
993993
$valueType = null;
994-
if ($case instanceof ReflectionEnumBackedCase) {
994+
if ($case instanceof ReflectionEnumBackedCase && $case->hasBackingValue()) {
995995
$valueType = $this->initializerExprTypeResolver->getType($case->getValueExpression(), InitializerExprContext::fromClassReflection($this));
996996
}
997997

‎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.1')]
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)

AltStyle によって変換されたページ (->オリジナル) /