Skip to content

Commit

Permalink
Report real file with a parse error
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 16, 2020
1 parent befb743 commit 8113f38
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Analyser/FileAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
19 changes: 18 additions & 1 deletion src/Parser/CachedParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/Parser/DirectParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand All @@ -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();
Expand Down
14 changes: 13 additions & 1 deletion src/Parser/ParserErrorsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -29,4 +36,9 @@ public function getErrors(): array
return $this->errors;
}

public function getParsedFile(): ?string
{
return $this->parsedFile;
}

}

0 comments on commit 8113f38

Please sign in to comment.