diff --git a/src/Reflection/ClassReflection.php b/src/Reflection/ClassReflection.php index a6404b7253..256dcdd69b 100644 --- a/src/Reflection/ClassReflection.php +++ b/src/Reflection/ClassReflection.php @@ -964,7 +964,7 @@ public function getEnumCases(): array $initializerExprContext = InitializerExprContext::fromClassReflection($this); foreach ($this->reflection->getCases() as $case) { $valueType = null; - if ($case instanceof ReflectionEnumBackedCase) { + if ($case instanceof ReflectionEnumBackedCase && $case->hasBackingValue()) { $valueType = $this->initializerExprTypeResolver->getType($case->getValueExpression(), $initializerExprContext); } $caseName = $case->getName(); @@ -991,7 +991,7 @@ public function getEnumCase(string $name): EnumCaseReflection $case = $this->reflection->getCase($name); $valueType = null; - if ($case instanceof ReflectionEnumBackedCase) { + if ($case instanceof ReflectionEnumBackedCase && $case->hasBackingValue()) { $valueType = $this->initializerExprTypeResolver->getType($case->getValueExpression(), InitializerExprContext::fromClassReflection($this)); } diff --git a/tests/PHPStan/Analyser/AnalyserIntegrationTest.php b/tests/PHPStan/Analyser/AnalyserIntegrationTest.php index e7f26496b8..ec88d167bd 100644 --- a/tests/PHPStan/Analyser/AnalyserIntegrationTest.php +++ b/tests/PHPStan/Analyser/AnalyserIntegrationTest.php @@ -1163,6 +1163,15 @@ public function testBug8537(): void $this->assertNoErrors($errors); } + #[RequiresPhp('>= 8.1')] + public function testBug7927(): void + { + $errors = $this->runAnalyse(__DIR__ . '/data/bug-7927.php'); + $this->assertCount(2, $errors); + $this->assertSame('Enum case Bug7927\Test::One does not have a value but the enum is backed with the "int" type.', $errors[0]->getMessage()); + $this->assertSame('Enum case Bug7927\Test::Two does not have a value but the enum is backed with the "int" type.', $errors[1]->getMessage()); + } + public function testBug8146(): void { $errors = $this->runAnalyse(__DIR__ . '/data/bug-8146b.php'); diff --git a/tests/PHPStan/Analyser/data/bug-7927.php b/tests/PHPStan/Analyser/data/bug-7927.php new file mode 100644 index 0000000000..b5ed5d572c --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-7927.php @@ -0,0 +1,26 @@ +name}'."); + } +} + +echo doIt($v);