From 22ef77a33ec2b36d3878ad17b4a0769be40eab1b Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Thu, 22 Oct 2020 23:45:26 +0200 Subject: [PATCH] ExportedNodeFetcher - use PHPStan's Parser interface --- conf/config.neon | 4 ---- src/Dependency/ExportedNodeFetcher.php | 14 ++++++-------- src/Parser/PathRoutingParser.php | 2 +- .../SourceLocator/FileNodesFetcher.php | 12 ++++++------ 4 files changed, 13 insertions(+), 19 deletions(-) diff --git a/conf/config.neon b/conf/config.neon index 063c68b2de..d96296ed9f 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -436,8 +436,6 @@ services: - class: PHPStan\Dependency\ExportedNodeFetcher - arguments: - phpParser: @phpParserDecorator - class: PHPStan\Dependency\ExportedNodeResolver @@ -545,8 +543,6 @@ services: - class: PHPStan\Reflection\BetterReflection\SourceLocator\FileNodesFetcher - arguments: - phpParser: @phpParserDecorator - class: PHPStan\Reflection\BetterReflection\SourceLocator\AutoloadSourceLocator diff --git a/src/Dependency/ExportedNodeFetcher.php b/src/Dependency/ExportedNodeFetcher.php index f7ea6fa5c7..8a4c6beb79 100644 --- a/src/Dependency/ExportedNodeFetcher.php +++ b/src/Dependency/ExportedNodeFetcher.php @@ -3,21 +3,21 @@ namespace PHPStan\Dependency; use PhpParser\NodeTraverser; -use PHPStan\File\FileReader; +use PHPStan\Parser\Parser; class ExportedNodeFetcher { - private \PhpParser\Parser $phpParser; + private Parser $parser; private ExportedNodeVisitor $visitor; public function __construct( - \PhpParser\Parser $phpParser, + Parser $parser, ExportedNodeVisitor $visitor ) { - $this->phpParser = $phpParser; + $this->parser = $parser; $this->visitor = $visitor; } @@ -30,12 +30,10 @@ public function fetchNodes(string $fileName): array $nodeTraverser = new NodeTraverser(); $nodeTraverser->addVisitor($this->visitor); - $contents = FileReader::read($fileName); - try { /** @var \PhpParser\Node[] $ast */ - $ast = $this->phpParser->parse($contents); - } catch (\PhpParser\Error $e) { + $ast = $this->parser->parseFile($fileName); + } catch (\PHPStan\Parser\ParserErrorsException $e) { return []; } $this->visitor->reset($fileName); diff --git a/src/Parser/PathRoutingParser.php b/src/Parser/PathRoutingParser.php index dfb8db602d..7fa5f6aba3 100644 --- a/src/Parser/PathRoutingParser.php +++ b/src/Parser/PathRoutingParser.php @@ -40,7 +40,7 @@ public function parseFile(string $file): array public function parseString(string $sourceCode): array { - return $this->currentPhpVersionRichParser->parseString($sourceCode); + return $this->currentPhpVersionSimpleParser->parseString($sourceCode); } } diff --git a/src/Reflection/BetterReflection/SourceLocator/FileNodesFetcher.php b/src/Reflection/BetterReflection/SourceLocator/FileNodesFetcher.php index fe1b748d05..49ac33571d 100644 --- a/src/Reflection/BetterReflection/SourceLocator/FileNodesFetcher.php +++ b/src/Reflection/BetterReflection/SourceLocator/FileNodesFetcher.php @@ -3,8 +3,8 @@ namespace PHPStan\Reflection\BetterReflection\SourceLocator; use PhpParser\NodeTraverser; -use PhpParser\Parser; use PHPStan\File\FileReader; +use PHPStan\Parser\Parser; use Roave\BetterReflection\SourceLocator\Located\LocatedSource; class FileNodesFetcher @@ -12,15 +12,15 @@ class FileNodesFetcher private \PHPStan\Reflection\BetterReflection\SourceLocator\CachingVisitor $cachingVisitor; - private Parser $phpParser; + private Parser $parser; public function __construct( CachingVisitor $cachingVisitor, - Parser $phpParser + Parser $parser ) { $this->cachingVisitor = $cachingVisitor; - $this->phpParser = $phpParser; + $this->parser = $parser; } public function fetchNodes(string $fileName): FetchedNodesResult @@ -33,8 +33,8 @@ public function fetchNodes(string $fileName): FetchedNodesResult try { /** @var \PhpParser\Node[] $ast */ - $ast = $this->phpParser->parse($contents); - } catch (\PhpParser\Error $e) { + $ast = $this->parser->parseFile($fileName); + } catch (\PHPStan\Parser\ParserErrorsException $e) { return new FetchedNodesResult([], [], [], $locatedSource); } $this->cachingVisitor->reset($fileName);