diff --git a/conf/config.neon b/conf/config.neon index 39da24dbd5..a67f8025a8 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -519,7 +519,7 @@ services: - class: PHPStan\Parser\CachedParser arguments: - originalParser: @currentPhpVersionParser + originalParser: @currentPhpVersionRichParser cachedNodesByStringCountMax: %cache.nodesByStringCountMax% - @@ -1173,13 +1173,19 @@ services: directory: %tmpDir%/cache/PHPStan autowired: no - currentPhpVersionParser: + currentPhpVersionRichParser: class: PHPStan\Parser\RichParser arguments: parser: @currentPhpVersionPhpParser lexer: @currentPhpVersionLexer autowired: no + currentPhpVersionSimpleParser: + class: PHPStan\Parser\SimpleParser + arguments: + parser: @currentPhpVersionPhpParser + autowired: no + phpParserDecorator: class: PHPStan\Parser\PhpParserDecorator arguments: diff --git a/src/Parser/SimpleParser.php b/src/Parser/SimpleParser.php new file mode 100644 index 0000000000..f20603457b --- /dev/null +++ b/src/Parser/SimpleParser.php @@ -0,0 +1,61 @@ +parser = $parser; + $this->nameResolver = $nameResolver; + } + + /** + * @param string $file path to a file to parse + * @return \PhpParser\Node\Stmt[] + */ + public function parseFile(string $file): array + { + try { + return $this->parseString(FileReader::read($file)); + } catch (\PHPStan\Parser\ParserErrorsException $e) { + throw new \PHPStan\Parser\ParserErrorsException($e->getErrors(), $file); + } + } + + /** + * @param string $sourceCode + * @return \PhpParser\Node\Stmt[] + */ + public function parseString(string $sourceCode): array + { + $errorHandler = new Collecting(); + $nodes = $this->parser->parse($sourceCode, $errorHandler); + if ($errorHandler->hasErrors()) { + throw new \PHPStan\Parser\ParserErrorsException($errorHandler->getErrors(), null); + } + if ($nodes === null) { + throw new \PHPStan\ShouldNotHappenException(); + } + + $nodeTraverser = new NodeTraverser(); + $nodeTraverser->addVisitor($this->nameResolver); + + /** @var array<\PhpParser\Node\Stmt> */ + return $nodeTraverser->traverse($nodes); + } + +}