From 8113f3840dbd298bbb5fb5311c6f1f85ea5f4d59 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Fri, 16 Oct 2020 11:58:36 +0200 Subject: [PATCH] Report real file with a parse error --- src/Analyser/FileAnalyser.php | 2 +- src/Parser/CachedParser.php | 19 ++++++++++++++++++- src/Parser/DirectParser.php | 8 ++++++-- src/Parser/ParserErrorsException.php | 14 +++++++++++++- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/Analyser/FileAnalyser.php b/src/Analyser/FileAnalyser.php index 144804e1df..2e87e9075c 100644 --- a/src/Analyser/FileAnalyser.php +++ b/src/Analyser/FileAnalyser.php @@ -233,7 +233,7 @@ public function analyseFile( $fileErrors[] = new Error($e->getMessage(), $file, $e->getStartLine() !== -1 ? $e->getStartLine() : null, $e); } catch (\PHPStan\Parser\ParserErrorsException $e) { foreach ($e->getErrors() as $error) { - $fileErrors[] = new Error($error->getMessage(), $file, $error->getStartLine() !== -1 ? $error->getStartLine() : null, $e); + $fileErrors[] = new Error($error->getMessage(), $e->getParsedFile() ?? $file, $error->getStartLine() !== -1 ? $error->getStartLine() : null, $e); } } catch (\PHPStan\AnalysedCodeException $e) { $fileErrors[] = new Error($e->getMessage(), $file, null, $e, null, null, $e->getTip()); diff --git a/src/Parser/CachedParser.php b/src/Parser/CachedParser.php index 4b27a9c4bd..9a789330b1 100644 --- a/src/Parser/CachedParser.php +++ b/src/Parser/CachedParser.php @@ -31,7 +31,24 @@ public function __construct( */ public function parseFile(string $file): array { - return $this->parseString(FileReader::read($file)); + if ($this->cachedNodesByStringCountMax !== 0 && $this->cachedNodesByStringCount >= $this->cachedNodesByStringCountMax) { + $this->cachedNodesByString = array_slice( + $this->cachedNodesByString, + 1, + null, + true + ); + + --$this->cachedNodesByStringCount; + } + + $sourceCode = FileReader::read($file); + if (!isset($this->cachedNodesByString[$sourceCode])) { + $this->cachedNodesByString[$sourceCode] = $this->originalParser->parseFile($file); + $this->cachedNodesByStringCount++; + } + + return $this->cachedNodesByString[$sourceCode]; } /** diff --git a/src/Parser/DirectParser.php b/src/Parser/DirectParser.php index f195462056..54389920f7 100644 --- a/src/Parser/DirectParser.php +++ b/src/Parser/DirectParser.php @@ -48,7 +48,11 @@ public function __construct( */ public function parseFile(string $file): array { - return $this->parseString(FileReader::read($file)); + try { + return $this->parseString(FileReader::read($file)); + } catch (\PHPStan\Parser\ParserErrorsException $e) { + throw new \PHPStan\Parser\ParserErrorsException($e->getErrors(), $file); + } } /** @@ -61,7 +65,7 @@ public function parseString(string $sourceCode): array $nodes = $this->parser->parse($sourceCode, $errorHandler); $tokens = $this->lexer->getTokens(); if ($errorHandler->hasErrors()) { - throw new \PHPStan\Parser\ParserErrorsException($errorHandler->getErrors()); + throw new \PHPStan\Parser\ParserErrorsException($errorHandler->getErrors(), null); } if ($nodes === null) { throw new \PHPStan\ShouldNotHappenException(); diff --git a/src/Parser/ParserErrorsException.php b/src/Parser/ParserErrorsException.php index 2ab34f8039..5be367a751 100644 --- a/src/Parser/ParserErrorsException.php +++ b/src/Parser/ParserErrorsException.php @@ -10,15 +10,22 @@ class ParserErrorsException extends \Exception /** @var \PhpParser\Error[] */ private array $errors; + private ?string $parsedFile; + /** * @param \PhpParser\Error[] $errors + * @param string|null $parsedFile */ - public function __construct(array $errors) + public function __construct( + array $errors, + ?string $parsedFile + ) { parent::__construct(implode(', ', array_map(static function (Error $error): string { return $error->getMessage(); }, $errors))); $this->errors = $errors; + $this->parsedFile = $parsedFile; } /** @@ -29,4 +36,9 @@ public function getErrors(): array return $this->errors; } + public function getParsedFile(): ?string + { + return $this->parsedFile; + } + }