Skip to content

Commit

Permalink
Prevent internal error in case of unknown constant
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 5, 2020
1 parent 8188304 commit e964816
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Analyser/FileAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PHPStan\Rules\NonIgnorableRuleError;
use PHPStan\Rules\Registry;
use PHPStan\Rules\TipRuleError;
use Roave\BetterReflection\NodeCompiler\Exception\UnableToCompileNode;
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound;
use function array_fill_keys;
use function array_key_exists;
Expand Down Expand Up @@ -93,6 +94,9 @@ public function analyseFile(
} catch (IdentifierNotFound $e) {
$fileErrors[] = new Error(sprintf('Reflection error: %s not found.', $e->getIdentifier()->getName()), $file, $node->getLine(), $e, null, null, 'Learn more at https://phpstan.org/user-guide/discovering-symbols');
continue;
} catch (UnableToCompileNode $e) {
$fileErrors[] = new Error(sprintf('Reflection error: %s', $e->getMessage()), $file, $node->getLine(), $e, null, null, 'Learn more at https://phpstan.org/user-guide/discovering-symbols');
continue;
}

foreach ($ruleErrors as $ruleError) {
Expand Down Expand Up @@ -174,6 +178,8 @@ public function analyseFile(
// pass
} catch (IdentifierNotFound $e) {
// pass
} catch (UnableToCompileNode $e) {
// pass
}
};

Expand Down Expand Up @@ -233,6 +239,8 @@ public function analyseFile(
$fileErrors[] = new Error($e->getMessage(), $file, null, $e, null, null, $e->getTip());
} catch (IdentifierNotFound $e) {
$fileErrors[] = new Error(sprintf('Reflection error: %s not found.', $e->getIdentifier()->getName()), $file, null, $e, null, null, 'Learn more at https://phpstan.org/user-guide/discovering-symbols');
} catch (UnableToCompileNode $e) {
$fileErrors[] = new Error(sprintf('Reflection error: %s', $e->getMessage()), $file, null, $e, null, null, 'Learn more at https://phpstan.org/user-guide/discovering-symbols');
}
} elseif (is_dir($file)) {
$fileErrors[] = new Error(sprintf('File %s is a directory.', $file), $file, null, false);
Expand Down
11 changes: 11 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,17 @@ public function testBug3686(): void
$this->assertCount(0, $errors);
}

public function testBug3379(): void
{
if (!self::$useStaticReflectionProvider) {
$this->markTestSkipped('Test requires static reflection');
}
$errors = $this->runAnalyse(__DIR__ . '/data/bug-3379.php');
$this->assertCount(2, $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());
}

/**
* @param string $file
* @return \PHPStan\Analyser\Error[]
Expand Down
14 changes: 14 additions & 0 deletions tests/PHPStan/Analyser/data/bug-3379.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Bug3379;

class Foo
{

const URL = SOME_UNKNOWN_CONST . '/test';

}

function () {
echo Foo::URL;
};

0 comments on commit e964816

Please sign in to comment.