From b5d73a7f3011077606ecd4d772d2fd1271219aec Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 21 Dec 2025 23:57:05 +0100 Subject: [PATCH 1/2] [experiment] skip beforeTraverse() and afterTraverse() as never used --- .../SimplifyEmptyCheckOnEmptyArrayRector.php | 2 +- .../CompleteMissingIfElseBracketRector.php | 2 +- .../Rector/NotEqual/CommonNotEqualRector.php | 2 +- ...rapEncapsedVariableInCurlyBracesRector.php | 2 +- .../Concat/RemoveConcatAutocastRector.php | 2 +- .../Rector/Ternary/TernaryToElvisRector.php | 2 +- .../Array_/LongArrayToShortArrayRector.php | 2 +- .../CurlyToSquareBracketArrayStringRector.php | 2 +- ...riableInStringInterpolationFixerRector.php | 2 +- .../NewMethodCallWithoutParenthesesRector.php | 2 +- .../Switch_/ColonAfterSwitchCaseRector.php | 2 +- .../TypedPropertyFromAssignsRector.php | 3 +- .../DeclareStrictTypesRector.php | 2 +- .../AbstractImmutableNodeTraverser.php | 30 +++++++++---------- src/PostRector/Rector/AbstractPostRector.php | 2 +- src/Rector/AbstractRector.php | 28 ++++++++++++----- 16 files changed, 50 insertions(+), 37 deletions(-) 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..2769252c79c 100644 --- a/rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php +++ b/rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php @@ -68,7 +68,7 @@ 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..dbe65e5dd53 100644 --- a/rules/CodeQuality/Rector/NotEqual/CommonNotEqualRector.php +++ b/rules/CodeQuality/Rector/NotEqual/CommonNotEqualRector.php @@ -62,7 +62,7 @@ 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..f4958eaf0cb 100644 --- a/rules/CodingStyle/Rector/Encapsed/WrapEncapsedVariableInCurlyBracesRector.php +++ b/rules/CodingStyle/Rector/Encapsed/WrapEncapsedVariableInCurlyBracesRector.php @@ -52,7 +52,7 @@ 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..695c59e30fd 100644 --- a/rules/DeadCode/Rector/Concat/RemoveConcatAutocastRector.php +++ b/rules/DeadCode/Rector/Concat/RemoveConcatAutocastRector.php @@ -79,7 +79,7 @@ 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..52528d35f30 100644 --- a/rules/Php53/Rector/Ternary/TernaryToElvisRector.php +++ b/rules/Php53/Rector/Ternary/TernaryToElvisRector.php @@ -77,7 +77,7 @@ 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..f1321ccf526 100644 --- a/rules/Php54/Rector/Array_/LongArrayToShortArrayRector.php +++ b/rules/Php54/Rector/Array_/LongArrayToShortArrayRector.php @@ -78,7 +78,7 @@ 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..074da797812 100644 --- a/rules/Php82/Rector/Encapsed/VariableInStringInterpolationFixerRector.php +++ b/rules/Php82/Rector/Encapsed/VariableInStringInterpolationFixerRector.php @@ -51,7 +51,7 @@ 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..e330fcbbc54 100644 --- a/rules/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector.php +++ b/rules/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector.php @@ -53,7 +53,7 @@ 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..f6c4f621a01 100644 --- a/rules/Php85/Rector/Switch_/ColonAfterSwitchCaseRector.php +++ b/rules/Php85/Rector/Switch_/ColonAfterSwitchCaseRector.php @@ -50,7 +50,7 @@ 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..2dd7fd5eb8f 100644 --- a/rules/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector.php +++ b/rules/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector.php @@ -157,8 +157,9 @@ 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..e9d5935e5d2 100644 --- a/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php +++ b/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php @@ -86,7 +86,7 @@ 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..3f313695d57 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..019261a0400 100644 --- a/src/PostRector/Rector/AbstractPostRector.php +++ b/src/PostRector/Rector/AbstractPostRector.php @@ -41,6 +41,6 @@ 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..8a02829e039 100644 --- a/src/Rector/AbstractRector.php +++ b/src/Rector/AbstractRector.php @@ -114,7 +114,21 @@ public function autowire( */ public function beforeTraverse(array $nodes): ?array { - // workaround for file around refactor() +// // 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; + } + + protected function getFile(): File + { $file = $this->currentFileProvider->getFile(); if (! $file instanceof File) { throw new ShouldNotHappenException( @@ -122,9 +136,7 @@ public function beforeTraverse(array $nodes): ?array ); } - $this->file = $file; - - return null; + return $file; } /** @@ -132,11 +144,11 @@ 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 +184,7 @@ 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; @@ -281,7 +293,7 @@ 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); From 41f3cd213867961cfd1f52f6d62b5e70f8d5fa0d Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 21 Dec 2025 23:57:10 +0100 Subject: [PATCH 2/2] wip --- .../CompleteMissingIfElseBracketRector.php | 3 +- .../Rector/NotEqual/CommonNotEqualRector.php | 3 +- ...rapEncapsedVariableInCurlyBracesRector.php | 3 +- .../Concat/RemoveConcatAutocastRector.php | 3 +- .../Rector/Ternary/TernaryToElvisRector.php | 3 +- .../Array_/LongArrayToShortArrayRector.php | 3 +- ...riableInStringInterpolationFixerRector.php | 3 +- .../NewMethodCallWithoutParenthesesRector.php | 3 +- .../Switch_/ColonAfterSwitchCaseRector.php | 3 +- .../TypedPropertyFromAssignsRector.php | 1 - .../DeclareStrictTypesRector.php | 3 +- .../AbstractImmutableNodeTraverser.php | 26 +++++----- src/PostRector/Rector/AbstractPostRector.php | 3 +- src/Rector/AbstractRector.php | 51 ++++++++++--------- 14 files changed, 62 insertions(+), 49 deletions(-) diff --git a/rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php b/rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php index 2769252c79c..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->getFile()->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 dbe65e5dd53..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->getFile()->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 f4958eaf0cb..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->getFile()->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 695c59e30fd..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->getFile()->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 52528d35f30..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->getFile()->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 f1321ccf526..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->getFile()->getOldTokens(); + $tokens = $this->getFile() + ->getOldTokens(); $startTokenPos = $node->getStartTokenPos(); $endTokenPos = $node->getEndTokenPos(); diff --git a/rules/Php82/Rector/Encapsed/VariableInStringInterpolationFixerRector.php b/rules/Php82/Rector/Encapsed/VariableInStringInterpolationFixerRector.php index 074da797812..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->getFile()->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 e330fcbbc54..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->getFile()->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 f6c4f621a01..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->getFile()->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 2dd7fd5eb8f..a315e117845 100644 --- a/rules/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector.php +++ b/rules/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector.php @@ -157,7 +157,6 @@ public function refactor(Node $node): ?Node $inferredType = $this->allAssignNodePropertyTypeInferer->inferProperty( $property, - $classReflection, $this->getFile() ); diff --git a/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php b/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php index e9d5935e5d2..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->getFile()->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 3f313695d57..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; -// } -// } + // 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; + // 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 019261a0400..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->getFile()->addRectorClassWithLine($rectorWithLineChange); + $this->getFile() + ->addRectorClassWithLine($rectorWithLineChange); } } diff --git a/src/Rector/AbstractRector.php b/src/Rector/AbstractRector.php index 8a02829e039..7c6d3b8344f 100644 --- a/src/Rector/AbstractRector.php +++ b/src/Rector/AbstractRector.php @@ -114,31 +114,19 @@ 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->getFile() = $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; } - 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; - } - /** * @return NodeTraverser::REMOVE_NODE|Node|null */ @@ -148,7 +136,8 @@ final public function enterNode(Node $node): int|Node|null return null; } - $filePath = $this->getFile()->getFilePath(); + $filePath = $this->getFile() + ->getFilePath(); if ($this->skipper->shouldSkipCurrentNode($this, $filePath, static::class, $node)) { return null; } @@ -184,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->getFile()->addRectorClassWithLine($rectorWithLineChange); + $this->getFile() + ->addRectorClassWithLine($rectorWithLineChange); // keep original node as node will be removed in leaveNode() return $originalNode; @@ -220,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); @@ -293,7 +295,8 @@ private function postRefactorProcess( $this->createdByRuleDecorator->decorate($refactoredNode, $originalNode, static::class); $rectorWithLineChange = new RectorWithLineChange(static::class, $originalNode->getStartLine()); - $this->getFile()->addRectorClassWithLine($rectorWithLineChange); + $this->getFile() + ->addRectorClassWithLine($rectorWithLineChange); /** @var MutatingScope|null $currentScope */ $currentScope = $node->getAttribute(AttributeKey::SCOPE);