Skip to content

Commit

Permalink
[AstResolver][Performance] Remove unnecessary double File read on Ast…
Browse files Browse the repository at this point in the history
…Resolver::parseFileNameToDecoratedNodes() (#4508)

* [AstResolver] Remove unnecessary double File read on AstResolver::parseFileNameToDecoratedNodes()

* [AstResolver] Remove unnecessary double File read on AstResolver::parseFileNameToDecoratedNodes()

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Jul 13, 2023
1 parent d64a761 commit a12242d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
10 changes: 7 additions & 3 deletions packages/NodeTypeResolver/NodeScopeAndMetadataDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,19 @@ public function __construct(
* @param Stmt[] $stmts
* @return Stmt[]
*/
public function decorateNodesFromFile(File $file, array $stmts): array
public function decorateNodesFromFile(File|string $file, array $stmts): array
{
$filePath = $file instanceof File ? $file->getFilePath() : $file;
$stmts = $this->fileWithoutNamespaceNodeTraverser->traverse($stmts);
$stmts = $this->phpStanNodeScopeResolver->processNodes($stmts, $file->getFilePath());
$stmts = $this->phpStanNodeScopeResolver->processNodes(
$stmts,
$filePath
);

if ($this->phpStanNodeScopeResolver->hasUnreachableStatementNode()) {
$unreachableStatementNodeVisitor = new UnreachableStatementNodeVisitor(
$this->phpStanNodeScopeResolver,
$file->getFilePath(),
$filePath,
$this->scopeFactory
);
$this->nodeTraverser->addVisitor($unreachableStatementNodeVisitor);
Expand Down
5 changes: 1 addition & 4 deletions src/PhpParser/AstResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\Core\PhpParser;

use Nette\Utils\FileSystem;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
Expand All @@ -29,7 +28,6 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\TypeWithClassName;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\Application\File;
use Rector\Core\ValueObject\MethodName;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
Expand Down Expand Up @@ -318,9 +316,8 @@ public function parseFileNameToDecoratedNodes(string $fileName): array
return $this->parsedFileNodes[$fileName] = [];
}

$file = new File($fileName, FileSystem::read($fileName));
return $this->parsedFileNodes[$fileName] = $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile(
$file,
$fileName,
$stmts
);
}
Expand Down

0 comments on commit a12242d

Please sign in to comment.