Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function refactor(Node $node): ?Node

$oldTokens = $this->getFile()
->getOldTokens();

if ($this->isIfConditionFollowedByOpeningCurlyBracket($node, $oldTokens)) {
return null;
}
Expand Down
5 changes: 3 additions & 2 deletions src/PostRector/Rector/AbstractPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function setFile(File $file): void
$this->file = $file;
}

public function getFile(): File
protected function getFile(): File
{
Assert::isInstanceOf($this->file, File::class);

Expand All @@ -41,6 +41,7 @@ protected function addRectorClassWithLine(Node $node): void
Assert::isInstanceOf($this->file, File::class);

$rectorWithLineChange = new RectorWithLineChange(static::class, $node->getStartLine());
$this->file->addRectorClassWithLine($rectorWithLineChange);
$this->getFile()
->addRectorClassWithLine($rectorWithLineChange);
}
}
38 changes: 18 additions & 20 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,24 +99,22 @@ public function autowire(
}

/**
* @final Avoid override to prevent unintended side-effects. Use enterNode() or @see \Rector\Contract\PhpParser\DecoratingNodeVisitorInterface instead.
* @return Node[]|null
*
* @internal
*
* @return Node[]|null
*/
public function beforeTraverse(array $nodes): ?array
final public function beforeTraverse(array $nodes): ?array
{
// workaround for file around refactor()
$file = $this->currentFileProvider->getFile();
if (! $file instanceof File) {
throw new ShouldNotHappenException(
'File object is missing. Make sure you call $this->currentFileProvider->setFile(...) before traversing.'
);
}

$this->file = $file;
return null;
}

/**
* @return Node[]|null
*
* @internal
*/
final public function afterTraverse(array $nodes)
{
return null;
}

Expand All @@ -125,12 +123,14 @@ public function beforeTraverse(array $nodes): ?array
*/
final public function enterNode(Node $node): int|Node|null|array
{
if (is_a($this, HTMLAverseRectorInterface::class, true) && $this->getFile()->containsHTML()) {
// keep $this->file populated for BC; refactor() is only ever reached through here
$this->file = $this->getFile();

if (is_a($this, HTMLAverseRectorInterface::class, true) && $this->file->containsHTML()) {
return null;
}

$filePath = $this->getFile()
->getFilePath();
$filePath = $this->file->getFilePath();
if ($this->skipper->shouldSkipCurrentNode($this, $filePath, static::class, $node)) {
return null;
}
Expand Down Expand Up @@ -163,8 +163,7 @@ final public function enterNode(Node $node): int|Node|null|array

// notify this rule changed code
$rectorWithLineChange = new RectorWithLineChange(static::class, $originalNode->getStartLine());
$this->getFile()
->addRectorClassWithLine($rectorWithLineChange);
$this->file->addRectorClassWithLine($rectorWithLineChange);

return $refactoredNodeOrState;
}
Expand Down Expand Up @@ -274,8 +273,7 @@ private function postRefactorProcess(
$this->createdByRuleDecorator->decorate($refactoredNode, $originalNode, static::class);

$rectorWithLineChange = new RectorWithLineChange(static::class, $originalNode->getStartLine());
$this->getFile()
->addRectorClassWithLine($rectorWithLineChange);
$this->file->addRectorClassWithLine($rectorWithLineChange);

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