Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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));
}

Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
26 changes: 26 additions & 0 deletions tests/PHPStan/Analyser/data/bug-7927.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types = 1);

namespace Bug7927;

enum Test : int
{
case One;
case Two;
}

$v = Test::One;

function doIt(Test $v) : string
{
switch($v)
{
case Test::One:
return "One";
case Test::Two:
return "Two";
default:
throw new \ErrorException("Unknown '{$v->name}'.");
}
}

echo doIt($v);
Loading