Skip to content

Commit e964816

Browse files
committed
Prevent internal error in case of unknown constant
1 parent 8188304 commit e964816

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

src/Analyser/FileAnalyser.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPStan\Rules\NonIgnorableRuleError;
1616
use PHPStan\Rules\Registry;
1717
use PHPStan\Rules\TipRuleError;
18+
use Roave\BetterReflection\NodeCompiler\Exception\UnableToCompileNode;
1819
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound;
1920
use function array_fill_keys;
2021
use function array_key_exists;
@@ -93,6 +94,9 @@ public function analyseFile(
9394
} catch (IdentifierNotFound $e) {
9495
$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');
9596
continue;
97+
} catch (UnableToCompileNode $e) {
98+
$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');
99+
continue;
96100
}
97101

98102
foreach ($ruleErrors as $ruleError) {
@@ -174,6 +178,8 @@ public function analyseFile(
174178
// pass
175179
} catch (IdentifierNotFound $e) {
176180
// pass
181+
} catch (UnableToCompileNode $e) {
182+
// pass
177183
}
178184
};
179185

@@ -233,6 +239,8 @@ public function analyseFile(
233239
$fileErrors[] = new Error($e->getMessage(), $file, null, $e, null, null, $e->getTip());
234240
} catch (IdentifierNotFound $e) {
235241
$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');
242+
} catch (UnableToCompileNode $e) {
243+
$fileErrors[] = new Error(sprintf('Reflection error: %s', $e->getMessage()), $file, null, $e, null, null, 'Learn more at https://phpstan.org/user-guide/discovering-symbols');
236244
}
237245
} elseif (is_dir($file)) {
238246
$fileErrors[] = new Error(sprintf('File %s is a directory.', $file), $file, null, false);

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,17 @@ public function testBug3686(): void
262262
$this->assertCount(0, $errors);
263263
}
264264

265+
public function testBug3379(): void
266+
{
267+
if (!self::$useStaticReflectionProvider) {
268+
$this->markTestSkipped('Test requires static reflection');
269+
}
270+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-3379.php');
271+
$this->assertCount(2, $errors);
272+
$this->assertSame('Constant SOME_UNKNOWN_CONST not found.', $errors[0]->getMessage());
273+
$this->assertSame('Reflection error: Could not locate constant "SOME_UNKNOWN_CONST" while evaluating expression in Bug3379\Foo at line 8', $errors[1]->getMessage());
274+
}
275+
265276
/**
266277
* @param string $file
267278
* @return \PHPStan\Analyser\Error[]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Bug3379;
4+
5+
class Foo
6+
{
7+
8+
const URL = SOME_UNKNOWN_CONST . '/test';
9+
10+
}
11+
12+
function () {
13+
echo Foo::URL;
14+
};

0 commit comments

Comments
 (0)