Skip to content

Commit 24964e2

Browse files
committed
Cache file-level invariants per file in AbstractRector::enterNode()
1 parent af55291 commit 24964e2

1 file changed

Lines changed: 25 additions & 9 deletions

File tree

‎src/Rector/AbstractRector.php‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ abstract class AbstractRector implements RectorInterface
7777

7878
private CreatedByRuleDecorator $createdByRuleDecorator;
7979

80+
// file-level cache, recomputed once per file (see enterNode())
81+
private ?File $cachedFile = null;
82+
83+
private string $cachedFilePath = '';
84+
85+
private bool $cachedHtmlAverseSkip = false;
86+
87+
private ?SkipMatch $cachedSkipMatch = null;
88+
8089
public function autowire(
8190
NodeNameResolver $nodeNameResolver,
8291
NodeTypeResolver $nodeTypeResolver,
@@ -106,15 +115,23 @@ public function autowire(
106115
*/
107116
final public function enterNode(Node $node): int|Node|null|array
108117
{
109-
// keep $this->file populated for BC; refactor() is only ever reached through here
110-
$this->file = $this->getFile();
118+
$file = $this->getFile();
119+
120+
// file-level invariants are constant across all nodes of a file; compute once per file
121+
if ($this->cachedFile !== $file) {
122+
$this->cachedFile = $file;
123+
// keep $this->file populated for BC; refactor() is only ever reached through here
124+
$this->file = $file;
125+
$this->cachedFilePath = $file->getFilePath();
126+
$this->cachedHtmlAverseSkip = is_a($this, HTMLAverseRectorInterface::class, true)
127+
&& $file->containsHTML();
128+
$this->cachedSkipMatch = $this->skipper->matchSkip($this, $this->cachedFilePath);
129+
}
111130

112-
if (is_a($this, HTMLAverseRectorInterface::class, true) && $this->file->containsHTML()) {
131+
if ($this->cachedHtmlAverseSkip) {
113132
return null;
114133
}
115134

116-
$filePath = $this->file->getFilePath();
117-
118135
// node already changed by this rule in a previous pass → hard skip
119136
if ($this->skipper->shouldSkipCurrentNode(static::class, $node)) {
120137
return null;
@@ -123,10 +140,9 @@ final public function enterNode(Node $node): int|Node|null|array
123140
// class/path skip is configured for this rule and file: run the rule on a deep clone to learn
124141
// whether it would actually have changed anything. Only a skip that prevents a real change
125142
// counts as used; the original node is left untouched, so the file stays skipped either way.
126-
$skipMatch = $this->skipper->matchSkip($this, $filePath);
127-
if ($skipMatch instanceof SkipMatch) {
143+
if ($this->cachedSkipMatch instanceof SkipMatch) {
128144
if ($this->refactor($this->cloneNode($node)) !== null) {
129-
$this->skipper->markSkipUsed($skipMatch);
145+
$this->skipper->markSkipUsed($this->cachedSkipMatch);
130146
}
131147

132148
return null;
@@ -165,7 +181,7 @@ final public function enterNode(Node $node): int|Node|null|array
165181
return $refactoredNodeOrState;
166182
}
167183

168-
return $this->postRefactorProcess($originalNode, $node, $refactoredNodeOrState, $filePath);
184+
return $this->postRefactorProcess($originalNode, $node, $refactoredNodeOrState, $this->cachedFilePath);
169185
}
170186

171187
protected function getFile(): File

0 commit comments

Comments
 (0)