diff --git a/src/Reflection/ClassConstantReflection.php b/src/Reflection/ClassConstantReflection.php index 1c92d6119d..88f87bf8f7 100644 --- a/src/Reflection/ClassConstantReflection.php +++ b/src/Reflection/ClassConstantReflection.php @@ -2,6 +2,7 @@ namespace PHPStan\Reflection; +use PHPStan\BetterReflection\NodeCompiler\Exception\UnableToCompileNode; use PHPStan\Php\PhpVersion; use PHPStan\TrinaryLogic; use PHPStan\Type\ConstantTypeHelper; @@ -65,7 +66,11 @@ public function getFileName(): ?string */ public function getValue() { - return $this->reflection->getValue(); + try { + return $this->reflection->getValue(); + } catch (UnableToCompileNode $e) { + return NAN; + } } public function hasPhpDocType(): bool diff --git a/src/Type/ConstantTypeHelper.php b/src/Type/ConstantTypeHelper.php index 4c515e4a5a..287b95ea58 100644 --- a/src/Type/ConstantTypeHelper.php +++ b/src/Type/ConstantTypeHelper.php @@ -20,6 +20,9 @@ public static function getTypeFromValue($value): Type if (is_int($value)) { return new ConstantIntegerType($value); } elseif (is_float($value)) { + if (is_nan($value)) { + return new MixedType(); + } return new ConstantFloatType($value); } elseif (is_bool($value)) { return new ConstantBooleanType($value); diff --git a/tests/PHPStan/Analyser/AnalyserIntegrationTest.php b/tests/PHPStan/Analyser/AnalyserIntegrationTest.php index dffd663c06..4cff1f74e5 100644 --- a/tests/PHPStan/Analyser/AnalyserIntegrationTest.php +++ b/tests/PHPStan/Analyser/AnalyserIntegrationTest.php @@ -278,9 +278,8 @@ public function testBug3379(): void $this->markTestSkipped('Test requires static reflection'); } $errors = $this->runAnalyse(__DIR__ . '/data/bug-3379.php'); - $this->assertCount(2, $errors); + $this->assertCount(1, $errors); $this->assertSame('Constant SOME_UNKNOWN_CONST not found.', $errors[0]->getMessage()); - $this->assertSame('Reflection error: Could not locate constant "SOME_UNKNOWN_CONST" while evaluating expression in Bug3379\Foo at line 8', $errors[1]->getMessage()); } public function testBug3798(): void diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index b3dabbf74e..1599e5e069 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -463,6 +463,11 @@ public function dataFileAsserts(): iterable } yield from $this->gatherAssertTypes(__DIR__ . '/data/class-constant-types.php'); + + if (self::$useStaticReflectionProvider) { + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3379.php'); + } + yield from $this->gatherAssertTypes(__DIR__ . '/data/modulo-operator.php'); } diff --git a/tests/PHPStan/Analyser/data/bug-3379.php b/tests/PHPStan/Analyser/data/bug-3379.php index e6b0cb5df4..53d82890e3 100644 --- a/tests/PHPStan/Analyser/data/bug-3379.php +++ b/tests/PHPStan/Analyser/data/bug-3379.php @@ -2,6 +2,8 @@ namespace Bug3379; +use function PHPStan\Testing\assertType; + class Foo { @@ -11,4 +13,5 @@ class Foo function () { echo Foo::URL; + assertType('mixed', Foo::URL); };