Skip to content

Commit

Permalink
Do not produce reflection error for unknown constants in class constants
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 20, 2021
1 parent 7af0c4e commit 29fcf80
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/Reflection/ClassConstantReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Reflection;

use PHPStan\BetterReflection\NodeCompiler\Exception\UnableToCompileNode;
use PHPStan\Php\PhpVersion;
use PHPStan\TrinaryLogic;
use PHPStan\Type\ConstantTypeHelper;
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/Type/ConstantTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
3 changes: 3 additions & 0 deletions tests/PHPStan/Analyser/data/bug-3379.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Bug3379;

use function PHPStan\Testing\assertType;

class Foo
{

Expand All @@ -11,4 +13,5 @@ class Foo

function () {
echo Foo::URL;
assertType('mixed', Foo::URL);
};

0 comments on commit 29fcf80

Please sign in to comment.