Skip to content

Commit 889e5d5

Browse files
committed
make beforeTraverse() and afterTraverse() final, to avoid hacking rector rule internals, use refactor() instead
1 parent 5bd619e commit 889e5d5

3 files changed

Lines changed: 18 additions & 11 deletions

File tree

‎rules/CodingStyle/Application/UseImportsAdder.php‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ private function createUses(
242242
continue;
243243
}
244244

245-
// already imported in previous cycle
246245
$newUses[] = $importType->getUseNode($type);
247246
}
248247
}

‎src/PostRector/Rector/AbstractPostRector.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function setFile(File $file): void
2929
$this->file = $file;
3030
}
3131

32-
public function getFile(): File
32+
protected function getFile(): File
3333
{
3434
Assert::isInstanceOf($this->file, File::class);
3535

‎src/Rector/AbstractRector.php‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,21 @@ public function autowire(
9999
}
100100

101101
/**
102-
* @final Avoid override to prevent unintended side-effects. Use enterNode() or @see \Rector\Contract\PhpParser\DecoratingNodeVisitorInterface instead.
103-
* @internal
102+
* @return Node[]|null
104103
*
104+
* @internal
105+
*/
106+
final public function beforeTraverse(array $nodes): ?array
107+
{
108+
return null;
109+
}
110+
111+
/**
105112
* @return Node[]|null
113+
*
114+
* @internal
106115
*/
107-
public function beforeTraverse(array $nodes): ?array
116+
final public function afterTraverse(array $nodes)
108117
{
109118
return null;
110119
}
@@ -115,13 +124,13 @@ public function beforeTraverse(array $nodes): ?array
115124
final public function enterNode(Node $node): int|Node|null|array
116125
{
117126
// keep $this->file populated for BC; refactor() is only ever reached through here
118-
$file = $this->file = $this->getFile();
127+
$this->file = $this->getFile();
119128

120-
if (is_a($this, HTMLAverseRectorInterface::class, true) && $file->containsHTML()) {
129+
if (is_a($this, HTMLAverseRectorInterface::class, true) && $this->file->containsHTML()) {
121130
return null;
122131
}
123132

124-
$filePath = $file->getFilePath();
133+
$filePath = $this->file->getFilePath();
125134
if ($this->skipper->shouldSkipCurrentNode($this, $filePath, static::class, $node)) {
126135
return null;
127136
}
@@ -154,7 +163,7 @@ final public function enterNode(Node $node): int|Node|null|array
154163

155164
// notify this rule changed code
156165
$rectorWithLineChange = new RectorWithLineChange(static::class, $originalNode->getStartLine());
157-
$file->addRectorClassWithLine($rectorWithLineChange);
166+
$this->file->addRectorClassWithLine($rectorWithLineChange);
158167

159168
return $refactoredNodeOrState;
160169
}
@@ -264,8 +273,7 @@ private function postRefactorProcess(
264273
$this->createdByRuleDecorator->decorate($refactoredNode, $originalNode, static::class);
265274

266275
$rectorWithLineChange = new RectorWithLineChange(static::class, $originalNode->getStartLine());
267-
$this->getFile()
268-
->addRectorClassWithLine($rectorWithLineChange);
276+
$this->file->addRectorClassWithLine($rectorWithLineChange);
269277

270278
/** @var MutatingScope|null $currentScope */
271279
$currentScope = $node->getAttribute(AttributeKey::SCOPE);

0 commit comments

Comments
 (0)