From 1116e03389f0f47297c2638b5b67c84f4d339478 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Tue, 14 May 2024 14:18:00 +0200 Subject: [PATCH] Fix internal error when accessing enum case by class-string --- src/Reflection/InitializerExprTypeResolver.php | 7 +++++++ .../PHPStan/Analyser/AnalyserIntegrationTest.php | 10 ++++++++++ tests/PHPStan/Analyser/data/bug-10985.php | 15 +++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 tests/PHPStan/Analyser/data/bug-10985.php diff --git a/src/Reflection/InitializerExprTypeResolver.php b/src/Reflection/InitializerExprTypeResolver.php index e5a6e58aed..b3fcce1c6a 100644 --- a/src/Reflection/InitializerExprTypeResolver.php +++ b/src/Reflection/InitializerExprTypeResolver.php @@ -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)) { diff --git a/tests/PHPStan/Analyser/AnalyserIntegrationTest.php b/tests/PHPStan/Analyser/AnalyserIntegrationTest.php index c7a9c0ede4..a64bfde5a8 100644 --- a/tests/PHPStan/Analyser/AnalyserIntegrationTest.php +++ b/tests/PHPStan/Analyser/AnalyserIntegrationTest.php @@ -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) { diff --git a/tests/PHPStan/Analyser/data/bug-10985.php b/tests/PHPStan/Analyser/data/bug-10985.php new file mode 100644 index 0000000000..7c03d9456c --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-10985.php @@ -0,0 +1,15 @@ += 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); +};