diff --git a/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php b/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php index efb58b3dfd7..693523e6359 100644 --- a/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php +++ b/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php @@ -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; } diff --git a/rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php b/rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php index 1b950cc5ebb..0e5dfc673b2 100644 --- a/rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php +++ b/rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php @@ -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; } diff --git a/rules/CodeQuality/Rector/NotEqual/CommonNotEqualRector.php b/rules/CodeQuality/Rector/NotEqual/CommonNotEqualRector.php index f443f49b641..cea0b7c876d 100644 --- a/rules/CodeQuality/Rector/NotEqual/CommonNotEqualRector.php +++ b/rules/CodeQuality/Rector/NotEqual/CommonNotEqualRector.php @@ -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 = '!='; diff --git a/rules/CodingStyle/Rector/Encapsed/WrapEncapsedVariableInCurlyBracesRector.php b/rules/CodingStyle/Rector/Encapsed/WrapEncapsedVariableInCurlyBracesRector.php index 160abb10e17..3d039823d08 100644 --- a/rules/CodingStyle/Rector/Encapsed/WrapEncapsedVariableInCurlyBracesRector.php +++ b/rules/CodingStyle/Rector/Encapsed/WrapEncapsedVariableInCurlyBracesRector.php @@ -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) { diff --git a/rules/DeadCode/Rector/Concat/RemoveConcatAutocastRector.php b/rules/DeadCode/Rector/Concat/RemoveConcatAutocastRector.php index 2ab1a6b09f7..6205fe4c468 100644 --- a/rules/DeadCode/Rector/Concat/RemoveConcatAutocastRector.php +++ b/rules/DeadCode/Rector/Concat/RemoveConcatAutocastRector.php @@ -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(); diff --git a/rules/Php53/Rector/Ternary/TernaryToElvisRector.php b/rules/Php53/Rector/Ternary/TernaryToElvisRector.php index 15e04e21f06..83b5ead1a5b 100644 --- a/rules/Php53/Rector/Ternary/TernaryToElvisRector.php +++ b/rules/Php53/Rector/Ternary/TernaryToElvisRector.php @@ -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(); diff --git a/rules/Php54/Rector/Array_/LongArrayToShortArrayRector.php b/rules/Php54/Rector/Array_/LongArrayToShortArrayRector.php index 59a5d8b3c9c..a0409e3af0a 100644 --- a/rules/Php54/Rector/Array_/LongArrayToShortArrayRector.php +++ b/rules/Php54/Rector/Array_/LongArrayToShortArrayRector.php @@ -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(); diff --git a/rules/Php74/Rector/ArrayDimFetch/CurlyToSquareBracketArrayStringRector.php b/rules/Php74/Rector/ArrayDimFetch/CurlyToSquareBracketArrayStringRector.php index 9e3bed6a019..a83d0a2c69e 100644 --- a/rules/Php74/Rector/ArrayDimFetch/CurlyToSquareBracketArrayStringRector.php +++ b/rules/Php74/Rector/ArrayDimFetch/CurlyToSquareBracketArrayStringRector.php @@ -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; } diff --git a/rules/Php82/Rector/Encapsed/VariableInStringInterpolationFixerRector.php b/rules/Php82/Rector/Encapsed/VariableInStringInterpolationFixerRector.php index 354a989f4fe..3adf2a8cf47 100644 --- a/rules/Php82/Rector/Encapsed/VariableInStringInterpolationFixerRector.php +++ b/rules/Php82/Rector/Encapsed/VariableInStringInterpolationFixerRector.php @@ -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) { diff --git a/rules/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector.php b/rules/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector.php index bcc1d2ce61e..b559a7b1539 100644 --- a/rules/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector.php +++ b/rules/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector.php @@ -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])) { diff --git a/rules/Php85/Rector/Switch_/ColonAfterSwitchCaseRector.php b/rules/Php85/Rector/Switch_/ColonAfterSwitchCaseRector.php index 910f961640e..da74a4273ef 100644 --- a/rules/Php85/Rector/Switch_/ColonAfterSwitchCaseRector.php +++ b/rules/Php85/Rector/Switch_/ColonAfterSwitchCaseRector.php @@ -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; diff --git a/rules/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector.php b/rules/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector.php index 7d670e0c6a1..a315e117845 100644 --- a/rules/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector.php +++ b/rules/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector.php @@ -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; diff --git a/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php b/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php index 49b99186763..a5c6c3d4917 100644 --- a/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php +++ b/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php @@ -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]; } diff --git a/src/PhpParser/NodeTraverser/AbstractImmutableNodeTraverser.php b/src/PhpParser/NodeTraverser/AbstractImmutableNodeTraverser.php index c20c0a94499..59bf5b5197c 100644 --- a/src/PhpParser/NodeTraverser/AbstractImmutableNodeTraverser.php +++ b/src/PhpParser/NodeTraverser/AbstractImmutableNodeTraverser.php @@ -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; } /** diff --git a/src/PostRector/Rector/AbstractPostRector.php b/src/PostRector/Rector/AbstractPostRector.php index 341100666b9..18736f9fbb3 100644 --- a/src/PostRector/Rector/AbstractPostRector.php +++ b/src/PostRector/Rector/AbstractPostRector.php @@ -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); } } diff --git a/src/Rector/AbstractRector.php b/src/Rector/AbstractRector.php index 7e3c1c86c2e..7c6d3b8344f 100644 --- a/src/Rector/AbstractRector.php +++ b/src/Rector/AbstractRector.php @@ -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; } @@ -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; } @@ -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; @@ -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); @@ -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);