From e9da2548272603b57a90426cc4ecea102d4537aa Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 22 Jun 2026 17:52:36 +0700 Subject: [PATCH] [perf] Avoid call refactor() twice on AbstractRector --- src/Rector/AbstractRector.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Rector/AbstractRector.php b/src/Rector/AbstractRector.php index abb90e8f46f..fd94913fa18 100644 --- a/src/Rector/AbstractRector.php +++ b/src/Rector/AbstractRector.php @@ -145,19 +145,21 @@ final public function enterNode(Node $node): int|Node|null|array // whether it would actually have changed anything. Only a skip that prevents a real change // counts as used; the original node is left untouched, so the file stays skipped either way. $skipMatch = $this->skipper->matchSkip($this, $filePath); + $nodeToRefactor = $skipMatch instanceof SkipMatch ? $this->cloneNode($node) : $node; + + // ensure origNode pulled before refactor to avoid changed during refactor, ref https://3v4l.org/YMEGN + $originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE) ?? $node; + + $refactoredNodeOrState = $this->refactor($nodeToRefactor); + if ($skipMatch instanceof SkipMatch) { - if ($this->refactor($this->cloneNode($node)) !== null) { + if ($refactoredNodeOrState !== null) { $this->skipper->markSkipUsed($skipMatch); } return null; } - // ensure origNode pulled before refactor to avoid changed during refactor, ref https://3v4l.org/YMEGN - $originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE) ?? $node; - - $refactoredNodeOrState = $this->refactor($node); - // nothing to change → continue if ($refactoredNodeOrState === null) { return null;