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 @@ -152,7 +152,7 @@ private function isAllowedExpr(Expr $expr, Scope $scope): bool
return false;
}

$type = $this->allAssignNodePropertyTypeInferer->inferProperty($property, $classReflection, $this->file);
$type = $this->allAssignNodePropertyTypeInferer->inferProperty($property, $classReflection, $this->getFile());
if (! $type instanceof Type) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public function refactor(Node $node): ?Node
return null;
}

$oldTokens = $this->file->getOldTokens();
$oldTokens = $this->getFile()
->getOldTokens();
if ($this->isIfConditionFollowedByOpeningCurlyBracket($node, $oldTokens)) {
return null;
}
Expand Down
3 changes: 2 additions & 1 deletion rules/CodeQuality/Rector/NotEqual/CommonNotEqualRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public function refactor(Node $node): ?NotEqual
$tokenEndPos = $node->getEndTokenPos();

for ($i = $tokenStartPos; $i < $tokenEndPos; ++$i) {
$token = $this->file->getOldTokens()[$i];
$token = $this->getFile()
->getOldTokens()[$i];

if ((string) $token === '<>') {
$token->text = '!=';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public function getNodeTypes(): array
public function refactor(Node $node): ?Node
{
$hasVariableBeenWrapped = false;
$oldTokens = $this->file->getOldTokens();
$oldTokens = $this->getFile()
->getOldTokens();

foreach ($node->parts as $index => $nodePart) {
if ($nodePart instanceof Variable && $nodePart->getStartTokenPos() >= 0) {
Expand Down
3 changes: 2 additions & 1 deletion rules/DeadCode/Rector/Concat/RemoveConcatAutocastRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ private function removeStringCast(Expr $expr): Expr
}

$targetExpr = $expr->expr;
$tokens = $this->file->getOldTokens();
$tokens = $this->getFile()
->getOldTokens();

if ($expr->expr instanceof BinaryOp) {
$castStartTokenPos = $expr->getStartTokenPos();
Expand Down
3 changes: 2 additions & 1 deletion rules/Php53/Rector/Ternary/TernaryToElvisRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public function provideMinPhpVersion(): int

private function isParenthesized(Expr $ifExpr, Expr $elseExpr): bool
{
$tokens = $this->file->getOldTokens();
$tokens = $this->getFile()
->getOldTokens();

$ifExprTokenEnd = $ifExpr->getEndTokenPos();
$elseExprTokenStart = $elseExpr->getStartTokenPos();
Expand Down
3 changes: 2 additions & 1 deletion rules/Php54/Rector/Array_/LongArrayToShortArrayRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public function refactor(Node $node): ?Node

$node->setAttribute(AttributeKey::KIND, Array_::KIND_SHORT);

$tokens = $this->file->getOldTokens();
$tokens = $this->getFile()
->getOldTokens();

$startTokenPos = $node->getStartTokenPos();
$endTokenPos = $node->getEndTokenPos();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->isFollowedByCurlyBracket($this->file, $node)) {
if (! $this->isFollowedByCurlyBracket($this->getFile(), $node)) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function refactor(Node $node): ?Node
return null;
}

if ($this->parenthesizedNestedTernaryAnalyzer->isParenthesized($this->file, $node)) {
if ($this->parenthesizedNestedTernaryAnalyzer->isParenthesized($this->getFile(), $node)) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$oldTokens = $this->file->getOldTokens();
$oldTokens = $this->getFile()
->getOldTokens();
$hasChanged = false;

foreach ($node->parts as $part) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public function refactor(Node $node): ?Node
return null;
}

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

$loop = 1;
while (isset($oldTokens[$node->var->getStartTokenPos() + $loop])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Rector\Php85\Rector\FuncCall;

use PHPStan\Type\UnionType;
use PHPStan\Type\TypeCombinator;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use Rector\NodeAnalyzer\ArgsAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
Expand Down
3 changes: 2 additions & 1 deletion rules/Php85/Rector/Switch_/ColonAfterSwitchCaseRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public function getNodeTypes(): array
public function refactor(Node $node): ?Node
{
$hasChanged = false;
$oldTokens = $this->file->getOldTokens();
$oldTokens = $this->getFile()
->getOldTokens();

foreach ($node->cases as $key => $case) {
$cond = $case->cond;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function refactor(Node $node): ?Node
$inferredType = $this->allAssignNodePropertyTypeInferer->inferProperty(
$property,
$classReflection,
$this->file
$this->getFile()
);
if (! $inferredType instanceof Type) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function someFunction(int $number)
public function refactor(Node $node): ?FileNode
{
// shebang files cannot have declare strict types
if ($this->file->hasShebang()) {
if ($this->getFile()->hasShebang()) {
return null;
}

Expand Down
16 changes: 1 addition & 15 deletions src/PhpParser/NodeTraverser/AbstractImmutableNodeTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,7 @@ public function removeVisitor(NodeVisitor $visitor): void
public function traverse(array $nodes): array
{
$this->stopTraversal = false;
foreach ($this->visitors as $visitor) {
if (null !== $return = $visitor->beforeTraverse($nodes)) {
$nodes = $return;
}
}

$nodes = $this->traverseArray($nodes);
for ($i = \count($this->visitors) - 1; $i >= 0; --$i) {
$visitor = $this->visitors[$i];
if (null !== $return = $visitor->afterTraverse($nodes)) {
$nodes = $return;
}
}

return $nodes;
return $this->traverseArray($nodes);
}

/**
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);
}
}
37 changes: 22 additions & 15 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ abstract class AbstractRector extends NodeVisitorAbstract implements RectorInter

protected NodeComparator $nodeComparator;

/**
* @deprecated Use getFile() instead
*/
protected File $file;

protected Skipper $skipper;
Expand Down Expand Up @@ -99,23 +102,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 All @@ -124,11 +116,12 @@ public function beforeTraverse(array $nodes): ?array
*/
final public function enterNode(Node $node): int|Node|null|array
{
if (is_a($this, HTMLAverseRectorInterface::class, true) && $this->file->containsHTML()) {
if (is_a($this, HTMLAverseRectorInterface::class, true) && $this->getFile()->containsHTML()) {
return null;
}

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

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

return $refactoredNodeOrState;
}

return $this->postRefactorProcess($originalNode, $node, $refactoredNodeOrState, $filePath);
}

protected function getFile(): File
{
$file = $this->currentFileProvider->getFile();
if (! $file instanceof File) {
throw new ShouldNotHappenException(
'File object is missing. Make sure you call $this->currentFileProvider->setFile(...) before traversing.'
);
}

return $file;
}

protected function isName(Node $node, string $name): bool
{
return $this->nodeNameResolver->isName($node, $name);
Expand Down Expand Up @@ -243,7 +249,8 @@ private function postRefactorProcess(
$this->createdByRuleDecorator->decorate($refactoredNode, $originalNode, static::class);

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

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