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 @@ -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
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 @@ -86,7 +86,8 @@ public function beforeTraverse(array $nodes): ?array
}

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

return [$this->nodeFactory->createDeclaresStrictType(), new Nop(), ...$nodes];
}
Expand Down
30 changes: 15 additions & 15 deletions src/PhpParser/NodeTraverser/AbstractImmutableNodeTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,21 @@ 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;
// foreach ($this->visitors as $visitor) {
// if (null !== $return = $visitor->beforeTraverse($nodes)) {
// $nodes = $return;
// }
// }

return $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;
}

/**
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);
}
}
41 changes: 28 additions & 13 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ public function autowire(
*/
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;
// // 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->getFile() = $file;

return null;
}
Expand All @@ -132,11 +132,12 @@ public function beforeTraverse(array $nodes): ?array
*/
final public function enterNode(Node $node): int|Node|null
{
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 @@ -172,7 +173,8 @@ final public function enterNode(Node $node): int|Node|null

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

// keep original node as node will be removed in leaveNode()
return $originalNode;
Expand Down Expand Up @@ -208,6 +210,18 @@ final public function leaveNode(Node $node): array|int|Node|null
return $this->nodesToReturn[$objectId] ?? $node;
}

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 @@ -281,7 +295,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