Skip to content

Commit

Permalink
ExportedNodeFetcher - use PHPStan's Parser interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 22, 2020
1 parent 06d02bc commit 22ef77a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 19 deletions.
4 changes: 0 additions & 4 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,6 @@ services:

-
class: PHPStan\Dependency\ExportedNodeFetcher
arguments:
phpParser: @phpParserDecorator

-
class: PHPStan\Dependency\ExportedNodeResolver
Expand Down Expand Up @@ -545,8 +543,6 @@ services:

-
class: PHPStan\Reflection\BetterReflection\SourceLocator\FileNodesFetcher
arguments:
phpParser: @phpParserDecorator

-
class: PHPStan\Reflection\BetterReflection\SourceLocator\AutoloadSourceLocator
Expand Down
14 changes: 6 additions & 8 deletions src/Dependency/ExportedNodeFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/PathRoutingParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
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
{

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
Expand All @@ -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);
Expand Down

0 comments on commit 22ef77a

Please sign in to comment.