Skip to content
Open
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
3 changes: 2 additions & 1 deletion src/PostRector/Rector/AbstractPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
24 changes: 13 additions & 11 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
use Rector\Skipper\Skipper\Skipper;
use Rector\ValueObject\Application\File;

/**
* @property-read File $file @deprecated Use $this->getFile() instead
*/
abstract class AbstractRector extends NodeVisitorAbstract implements RectorInterface
{
private const string EMPTY_NODE_ARRAY_MESSAGE = <<<CODE_SAMPLE
Expand Down Expand Up @@ -74,6 +77,16 @@ abstract class AbstractRector extends NodeVisitorAbstract implements RectorInter

private CreatedByRuleDecorator $createdByRuleDecorator;

public function __get(string $name): mixed
{
if ($name === 'file') {
return $this->getFile();
}

// fallback to default behavior
return $this->{$name};
}

public function autowire(
NodeNameResolver $nodeNameResolver,
NodeTypeResolver $nodeTypeResolver,
Expand All @@ -100,23 +113,12 @@ public function autowire(

/**
* @final Avoid override to prevent unintended side-effects. Use enterNode() or @see \Rector\Contract\PhpParser\DecoratingNodeVisitorInterface instead.
*
* @internal
*
* @return Node[]|null
*/
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;
}

Expand Down
Loading