From 806cfb48b7eaf8ba94f6508d405a31b4c5d1e621 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 31 Oct 2026 08:27:28 +0700 Subject: [PATCH] [perf] Reduce double traverse by reusing existing File::containsHTML() on AddUseStatementGuard --- src/PostRector/Guard/AddUseStatementGuard.php | 17 +++++------------ .../Rector/ClassRenamingPostRector.php | 2 +- .../Rector/DocblockNameImportingPostRector.php | 2 +- .../Rector/NameImportingPostRector.php | 2 +- 4 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/PostRector/Guard/AddUseStatementGuard.php b/src/PostRector/Guard/AddUseStatementGuard.php index 0247857cd48..f7d47408428 100644 --- a/src/PostRector/Guard/AddUseStatementGuard.php +++ b/src/PostRector/Guard/AddUseStatementGuard.php @@ -5,10 +5,9 @@ namespace Rector\PostRector\Guard; use PhpParser\Node\Stmt; -use PhpParser\Node\Stmt\InlineHTML; use PhpParser\Node\Stmt\Namespace_; -use Rector\PhpParser\Node\BetterNodeFinder; use Rector\PhpParser\Node\FileNode; +use Rector\ValueObject\Application\File; final class AddUseStatementGuard { @@ -17,16 +16,13 @@ final class AddUseStatementGuard */ private array $shouldTraverseOnFiles = []; - public function __construct( - private readonly BetterNodeFinder $betterNodeFinder - ) { - } - /** * @param Stmt[] $stmts */ - public function shouldTraverse(array $stmts, string $filePath): bool + public function shouldTraverse(array $stmts, File $file): bool { + $filePath = $file->getFilePath(); + if (isset($this->shouldTraverseOnFiles[$filePath])) { return $this->shouldTraverseOnFiles[$filePath]; } @@ -50,9 +46,6 @@ public function shouldTraverse(array $stmts, string $filePath): bool } } - return $this->shouldTraverseOnFiles[$filePath] = ! $this->betterNodeFinder->hasInstancesOf( - $stmts, - [InlineHTML::class] - ); + return $this->shouldTraverseOnFiles[$filePath] = ! $file->containsHTML(); } } diff --git a/src/PostRector/Rector/ClassRenamingPostRector.php b/src/PostRector/Rector/ClassRenamingPostRector.php index 11f2aec189c..4d151fdb4d1 100644 --- a/src/PostRector/Rector/ClassRenamingPostRector.php +++ b/src/PostRector/Rector/ClassRenamingPostRector.php @@ -56,6 +56,6 @@ public function shouldTraverse(array $stmts): bool return false; } - return $this->addUseStatementGuard->shouldTraverse($stmts, $this->getFile()->getFilePath()); + return $this->addUseStatementGuard->shouldTraverse($stmts, $this->getFile()); } } diff --git a/src/PostRector/Rector/DocblockNameImportingPostRector.php b/src/PostRector/Rector/DocblockNameImportingPostRector.php index 407f8de47db..0222159e67e 100644 --- a/src/PostRector/Rector/DocblockNameImportingPostRector.php +++ b/src/PostRector/Rector/DocblockNameImportingPostRector.php @@ -49,6 +49,6 @@ public function enterNode(Node $node): Node|null */ public function shouldTraverse(array $stmts): bool { - return $this->addUseStatementGuard->shouldTraverse($stmts, $this->getFile()->getFilePath()); + return $this->addUseStatementGuard->shouldTraverse($stmts, $this->getFile()); } } diff --git a/src/PostRector/Rector/NameImportingPostRector.php b/src/PostRector/Rector/NameImportingPostRector.php index 7c63e54b217..c45a998e563 100644 --- a/src/PostRector/Rector/NameImportingPostRector.php +++ b/src/PostRector/Rector/NameImportingPostRector.php @@ -58,6 +58,6 @@ public function enterNode(Node $node): Name|null */ public function shouldTraverse(array $stmts): bool { - return $this->addUseStatementGuard->shouldTraverse($stmts, $this->getFile()->getFilePath()); + return $this->addUseStatementGuard->shouldTraverse($stmts, $this->getFile()); } }