Skip to content

Commit 6354ce7

Browse files
phpstan-botondrejmirtes
authored andcommitted
Wrap NameResolver errors into ParserErrorsException so they are attributed to the file they come from
* `RichParser`, `SimpleParser` and `StubParser` now catch `PhpParser\Error` thrown from the name-resolving `NodeTraverser` run and rethrow it as `ParserErrorsException`, honouring the `@throws` contract of `PHPStan\Parser\Parser`. * `parseFile()` then attaches the parsed file name, so `FileNodesFetcher` and `ExportedNodeFetcher` degrade gracefully (symbol treated as not found) instead of letting the exception escape into the file that merely looks the symbol up. * Covers both errors `PhpParser\NameContext` can raise: the duplicate `use` alias ("Cannot use X as Y because the name is already in use", for `use`, `use function`, `use const` and group uses) and "'\self' is an invalid class name". * Also covers the analogous reach-another-file paths: class instantiation through the source locators, and trait resolution (`Class X uses unknown trait Y.`). * Parse errors of this kind reported for the analysed file itself now include the " on line N" suffix, matching how syntax errors collected by php-parser are reported.
1 parent 57093ed commit 6354ce7

10 files changed

Lines changed: 214 additions & 7 deletions

‎src/Parser/RichParser.php‎

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

33
namespace PHPStan\Parser;
44

5+
use PhpParser\Error;
56
use PhpParser\ErrorHandler\Collecting;
67
use PhpParser\Node;
78
use PhpParser\NodeTraverser;
@@ -102,8 +103,12 @@ public function parseString(string $sourceCode): array
102103
$nodeTraverser->addVisitor($visitor);
103104
}
104105

105-
/** @var array<Node\Stmt> */
106-
$nodes = $nodeTraverser->traverse($nodes);
106+
try {
107+
/** @var array<Node\Stmt> */
108+
$nodes = $nodeTraverser->traverse($nodes);
109+
} catch (Error $e) {
110+
throw new ParserErrorsException([$e], null);
111+
}
107112

108113
$reversePipeTransformer = new NodeTraverser(new ReversePipeTransformerVisitor());
109114
/** @var array<Node\Stmt> */

‎src/Parser/SimpleParser.php‎

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

33
namespace PHPStan\Parser;
44

5+
use PhpParser\Error;
56
use PhpParser\ErrorHandler\Collecting;
67
use PhpParser\Node;
78
use PhpParser\NodeTraverser;
@@ -49,8 +50,12 @@ public function parseString(string $sourceCode): array
4950
$nodeTraverser = new NodeTraverser();
5051
$nodeTraverser->addVisitor($this->nameResolver);
5152

52-
/** @var array<Node\Stmt> */
53-
return $nodeTraverser->traverse($nodes);
53+
try {
54+
/** @var array<Node\Stmt> */
55+
return $nodeTraverser->traverse($nodes);
56+
} catch (Error $e) {
57+
throw new ParserErrorsException([$e], null);
58+
}
5459
}
5560

5661
}

‎src/Parser/StubParser.php‎

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

33
namespace PHPStan\Parser;
44

5+
use PhpParser\Error;
56
use PhpParser\ErrorHandler\Collecting;
67
use PhpParser\Node;
78
use PhpParser\NodeTraverser;
@@ -49,8 +50,12 @@ public function parseString(string $sourceCode): array
4950
$nodeTraverser = new NodeTraverser();
5051
$nodeTraverser->addVisitor($this->nameResolver);
5152

52-
/** @var array<Node\Stmt> */
53-
return $nodeTraverser->traverse($nodes);
53+
try {
54+
/** @var array<Node\Stmt> */
55+
return $nodeTraverser->traverse($nodes);
56+
} catch (Error $e) {
57+
throw new ParserErrorsException([$e], null);
58+
}
5459
}
5560

5661
}

‎tests/PHPStan/Analyser/AnalyserIntegrationTest.php‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1318,7 +1318,7 @@ public function testBug10358(): void
13181318
// false positive
13191319
$errors = $this->runAnalyse(__DIR__ . '/data/bug-10358.php');
13201320
$this->assertCount(1, $errors);
1321-
$this->assertSame('Cannot use Ns\Foo2 as Foo because the name is already in use', $errors[0]->getMessage());
1321+
$this->assertSame('Cannot use Ns\Foo2 as Foo because the name is already in use on line 6', $errors[0]->getMessage());
13221322
$this->assertSame(6, $errors[0]->getLine());
13231323
}
13241324

@@ -1616,6 +1616,47 @@ public function testBug15003(): void
16161616
$this->assertNoErrors($errors);
16171617
}
16181618

1619+
public function testBug15252(): void
1620+
{
1621+
// name resolution errors in another file must not be reported as parse errors of this file
1622+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-15252.php');
1623+
$this->assertCount(4, $errors);
1624+
$this->assertSame('Instantiated class Bug15252\Helper not found.', $errors[0]->getMessage());
1625+
$this->assertSame(10, $errors[0]->getLine());
1626+
$this->assertSame('Call to method name() on an unknown class Bug15252\Helper.', $errors[1]->getMessage());
1627+
$this->assertSame(12, $errors[1]->getLine());
1628+
$this->assertSame('Instantiated class Bug15252\InvalidName not found.', $errors[2]->getMessage());
1629+
$this->assertSame(17, $errors[2]->getLine());
1630+
$this->assertSame('Call to method name() on an unknown class Bug15252\InvalidName.', $errors[3]->getMessage());
1631+
$this->assertSame(19, $errors[3]->getLine());
1632+
}
1633+
1634+
public function testBug15252Trait(): void
1635+
{
1636+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-15252-trait-user.php');
1637+
$this->assertCount(2, $errors);
1638+
$this->assertSame('Class Bug15252\TraitUser uses unknown trait Bug15252\HelperTrait.', $errors[0]->getMessage());
1639+
$this->assertSame(8, $errors[0]->getLine());
1640+
$this->assertSame('Call to an undefined method Bug15252\TraitUser::name().', $errors[1]->getMessage());
1641+
$this->assertSame(12, $errors[1]->getLine());
1642+
}
1643+
1644+
public function testBug15252AnalysedFileWithDuplicateAlias(): void
1645+
{
1646+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-15252-helper.php');
1647+
$this->assertCount(1, $errors);
1648+
$this->assertSame('Cannot use Ns\Foo2 as Foo because the name is already in use on line 6', $errors[0]->getMessage());
1649+
$this->assertSame(6, $errors[0]->getLine());
1650+
}
1651+
1652+
public function testBug15252AnalysedFileWithInvalidClassName(): void
1653+
{
1654+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-15252-invalid-name.php');
1655+
$this->assertCount(1, $errors);
1656+
$this->assertSame("'\\self' is an invalid class name on line 10", $errors[0]->getMessage());
1657+
$this->assertSame(10, $errors[0]->getLine());
1658+
}
1659+
16191660
/**
16201661
* @param string[]|null $allAnalysedFiles
16211662
* @return list<Error>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15252;
4+
5+
use Ns\Bar;
6+
use Ns\Bar2 as Bar;
7+
8+
trait HelperTrait
9+
{
10+
11+
public function name(): string
12+
{
13+
return 'trait';
14+
}
15+
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15252;
4+
5+
use Ns\Foo;
6+
use Ns\Foo2 as Foo;
7+
8+
class Helper
9+
{
10+
11+
public function name(): string
12+
{
13+
return 'helper';
14+
}
15+
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15252;
4+
5+
class InvalidName
6+
{
7+
8+
public function name(): string
9+
{
10+
$self = new \self();
11+
12+
return 'invalid';
13+
}
14+
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15252;
4+
5+
class TraitUser
6+
{
7+
8+
use HelperTrait;
9+
10+
public function run(): string
11+
{
12+
return $this->name();
13+
}
14+
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15252;
4+
5+
class Consumer
6+
{
7+
8+
public function run(): string
9+
{
10+
$helper = new Helper();
11+
12+
return $helper->name();
13+
}
14+
15+
public function runInvalidName(): string
16+
{
17+
$invalidName = new InvalidName();
18+
19+
return $invalidName->name();
20+
}
21+
22+
}

‎tests/PHPStan/Parser/RichParserTest.php‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPStan\Analyser\FileAnalyserResult;
66
use PHPStan\Testing\PHPStanTestCase;
77
use PHPUnit\Framework\Attributes\DataProvider;
8+
use function sprintf;
89
use const PHP_EOL;
910

1011
/**
@@ -548,4 +549,70 @@ public function testLinesToIgnoreParseErrors(string $code, array $expectedErrors
548549
$this->assertCount(0, $lines);
549550
}
550551

552+
public static function dataNameResolutionErrors(): iterable
553+
{
554+
yield 'duplicate class alias' => [
555+
'<?php' . PHP_EOL .
556+
'use Ns\Foo;' . PHP_EOL .
557+
'use Ns\Foo2 as Foo;',
558+
'Cannot use Ns\Foo2 as Foo because the name is already in use',
559+
3,
560+
];
561+
562+
yield 'duplicate function alias' => [
563+
'<?php' . PHP_EOL .
564+
'use function Ns\foo;' . PHP_EOL .
565+
'use function Ns\foo2 as foo;',
566+
'Cannot use function Ns\foo2 as foo because the name is already in use',
567+
3,
568+
];
569+
570+
yield 'duplicate constant alias' => [
571+
'<?php' . PHP_EOL .
572+
'use const Ns\FOO;' . PHP_EOL .
573+
'use const Ns\FOO2 as FOO;',
574+
'Cannot use const Ns\FOO2 as FOO because the name is already in use',
575+
3,
576+
];
577+
578+
yield 'duplicate alias in group use' => [
579+
'<?php' . PHP_EOL .
580+
'use Ns\Foo;' . PHP_EOL .
581+
'use Ns\{Foo2 as Foo};',
582+
'Cannot use Ns\Foo2 as Foo because the name is already in use',
583+
3,
584+
];
585+
586+
yield 'invalid class name' => [
587+
'<?php' . PHP_EOL .
588+
'new \self();',
589+
"'\\self' is an invalid class name",
590+
2,
591+
];
592+
}
593+
594+
/**
595+
* Name resolution errors must be reported as ParserErrorsException so that
596+
* callers looking up symbols in other files can degrade gracefully instead of
597+
* having a PhpParser\Error attributed to the file they are analysing.
598+
*/
599+
#[DataProvider('dataNameResolutionErrors')]
600+
public function testNameResolutionErrors(string $code, string $expectedMessage, int $expectedLine): void
601+
{
602+
foreach (['currentPhpVersionRichParser', 'currentPhpVersionSimpleDirectParser', 'freshStubParser'] as $serviceName) {
603+
/** @var Parser $parser */
604+
$parser = self::getContainer()->getService($serviceName);
605+
606+
try {
607+
$parser->parseString($code);
608+
$this->fail(sprintf('%s did not throw ParserErrorsException', $serviceName));
609+
} catch (ParserErrorsException $e) {
610+
$errors = $e->getErrors();
611+
$this->assertCount(1, $errors);
612+
$this->assertSame($expectedMessage, $errors[0]->getRawMessage());
613+
$this->assertSame($expectedLine, $errors[0]->getStartLine());
614+
}
615+
}
616+
}
617+
551618
}

0 commit comments

Comments
 (0)