Skip to content

Commit

Permalink
Fix internal error when accessing enum case by class-string
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 15, 2024
1 parent e689e1b commit 1116e03
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1858,6 +1858,13 @@ function (Type $type, callable $traverse): Type {
);
}

if ($constantClassType->isClassStringType()->yes()) {
if ($constantClassType->isConstantScalarValue()->yes()) {
$isObject = false;
}
$constantClassType = $constantClassType->getClassStringObjectType();
}

$types = [];
foreach ($constantClassType->getObjectClassNames() as $referencedClass) {
if (!$this->getReflectionProvider()->hasClass($referencedClass)) {
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,16 @@ public function testBug10772(): void
$this->assertNoErrors($errors);
}

public function testBug10985(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$errors = $this->runAnalyse(__DIR__ . '/data/bug-10985.php');
$this->assertNoErrors($errors);
}

public function testBug10979(): void
{
if (PHP_VERSION_ID < 80100) {
Expand Down
15 changes: 15 additions & 0 deletions tests/PHPStan/Analyser/data/bug-10985.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php // lint >= 8.1

namespace Bug10985;

use function PHPStan\Testing\assertType;

enum Test {
case ORIGINAL;
}

function (): void {
$item = Test::class;
$result = ($item)::ORIGINAL;
assertType('Bug10985\\Test::ORIGINAL', $result);
};

0 comments on commit 1116e03

Please sign in to comment.