Skip to content

Commit 29fcf80

Browse files
committed
Do not produce reflection error for unknown constants in class constants
1 parent 7af0c4e commit 29fcf80

File tree

5 files changed

+18
-3
lines changed

5 files changed

+18
-3
lines changed

src/Reflection/ClassConstantReflection.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Reflection;
44

5+
use PHPStan\BetterReflection\NodeCompiler\Exception\UnableToCompileNode;
56
use PHPStan\Php\PhpVersion;
67
use PHPStan\TrinaryLogic;
78
use PHPStan\Type\ConstantTypeHelper;
@@ -65,7 +66,11 @@ public function getFileName(): ?string
6566
*/
6667
public function getValue()
6768
{
68-
return $this->reflection->getValue();
69+
try {
70+
return $this->reflection->getValue();
71+
} catch (UnableToCompileNode $e) {
72+
return NAN;
73+
}
6974
}
7075

7176
public function hasPhpDocType(): bool

src/Type/ConstantTypeHelper.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public static function getTypeFromValue($value): Type
2020
if (is_int($value)) {
2121
return new ConstantIntegerType($value);
2222
} elseif (is_float($value)) {
23+
if (is_nan($value)) {
24+
return new MixedType();
25+
}
2326
return new ConstantFloatType($value);
2427
} elseif (is_bool($value)) {
2528
return new ConstantBooleanType($value);

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,8 @@ public function testBug3379(): void
278278
$this->markTestSkipped('Test requires static reflection');
279279
}
280280
$errors = $this->runAnalyse(__DIR__ . '/data/bug-3379.php');
281-
$this->assertCount(2, $errors);
281+
$this->assertCount(1, $errors);
282282
$this->assertSame('Constant SOME_UNKNOWN_CONST not found.', $errors[0]->getMessage());
283-
$this->assertSame('Reflection error: Could not locate constant "SOME_UNKNOWN_CONST" while evaluating expression in Bug3379\Foo at line 8', $errors[1]->getMessage());
284283
}
285284

286285
public function testBug3798(): void

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,11 @@ public function dataFileAsserts(): iterable
463463
}
464464

465465
yield from $this->gatherAssertTypes(__DIR__ . '/data/class-constant-types.php');
466+
467+
if (self::$useStaticReflectionProvider) {
468+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3379.php');
469+
}
470+
466471
yield from $this->gatherAssertTypes(__DIR__ . '/data/modulo-operator.php');
467472
}
468473

tests/PHPStan/Analyser/data/bug-3379.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Bug3379;
44

5+
use function PHPStan\Testing\assertType;
6+
57
class Foo
68
{
79

@@ -11,4 +13,5 @@ class Foo
1113

1214
function () {
1315
echo Foo::URL;
16+
assertType('mixed', Foo::URL);
1417
};

0 commit comments

Comments
 (0)