From 10e6e0e91c580d1a79d0c706dce1463858d320b9 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 7 Apr 2026 18:59:16 +0200 Subject: [PATCH] [deprecation] deprecate file, use getFile() instead --- phpstan.neon | 7 ++++- .../Coalesce/CoalesceToTernaryRector.php | 6 ++--- .../SimplifyEmptyCheckOnEmptyArrayRector.php | 2 +- .../CompleteMissingIfElseBracketRector.php | 3 ++- .../Rector/NotEqual/CommonNotEqualRector.php | 3 ++- ...rapEncapsedVariableInCurlyBracesRector.php | 3 ++- .../Concat/RemoveConcatAutocastRector.php | 3 ++- .../RemoveDeadZeroAndOneOperationRector.php | 2 +- .../Rector/Ternary/TernaryToElvisRector.php | 3 ++- .../Array_/LongArrayToShortArrayRector.php | 3 ++- .../Ternary/TernaryToNullCoalescingRector.php | 2 +- .../CurlyToSquareBracketArrayStringRector.php | 2 +- .../ParenthesizeNestedTernaryRector.php | 2 +- ...riableInStringInterpolationFixerRector.php | 3 ++- .../NewMethodCallWithoutParenthesesRector.php | 3 ++- .../Switch_/ColonAfterSwitchCaseRector.php | 3 ++- .../TypedPropertyFromAssignsRector.php | 2 +- .../DeclareStrictTypesRector.php | 2 +- src/Console/Command/WorkerCommand.php | 11 ++++++-- src/Rector/AbstractRector.php | 26 ++++++++++++++++--- 20 files changed, 65 insertions(+), 26 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 92f196a7291..9d964bd4225 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -478,4 +478,9 @@ parameters: message: '#Unable to resolve the template type T in call to static method Webmozart\\Assert\\Assert\:\:isInstanceOfAny\(\)#' path: rules/DeadCode/Rector/FunctionLike/NarrowWideUnionReturnTypeRector.php - - '#Provide more specific return type "Iterator|PhpParser\\Node" over abstract one#' \ No newline at end of file + - '#Provide more specific return type "Iterator|PhpParser\\Node" over abstract one#' + + # BC layer + - + message: '#Access to deprecated property \$file of class Rector\\Rector\\AbstractRector#' + path: src/Rector/AbstractRector.php diff --git a/rules/CodeQuality/Rector/Coalesce/CoalesceToTernaryRector.php b/rules/CodeQuality/Rector/Coalesce/CoalesceToTernaryRector.php index e582396dcbb..2c8c95f00e2 100644 --- a/rules/CodeQuality/Rector/Coalesce/CoalesceToTernaryRector.php +++ b/rules/CodeQuality/Rector/Coalesce/CoalesceToTernaryRector.php @@ -4,18 +4,18 @@ namespace Rector\CodeQuality\Rector\Coalesce; -use PHPStan\Type\UnionType; -use PHPStan\Type\NullType; use PhpParser\Node; use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\BinaryOp\Coalesce; use PhpParser\Node\Expr\Ternary; use PHPStan\Type\ErrorType; use PHPStan\Type\MixedType; +use PHPStan\Type\NullType; +use PHPStan\Type\UnionType; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; -use Rector\PHPStan\ScopeFetcher; /** * @see \Rector\Tests\CodeQuality\Rector\Coalesce\CoalesceToTernaryRector\CoalesceToTernaryRectorTest diff --git a/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php b/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php index 0bed5b73a6c..0f36b480e0f 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 243b71cc2b8..be8e73a7458 100644 --- a/rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php +++ b/rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php @@ -69,7 +69,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/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php b/rules/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php index a08d084aacc..f68584a58da 100644 --- a/rules/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php +++ b/rules/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php @@ -214,7 +214,7 @@ private function processBinaryMulAndDiv(Mul | Div $binaryOp): ?Expr if ($binaryOp instanceof Mul && $this->isLiteralOne($binaryOp->left) && $this->nodeTypeResolver->isNumberType( $binaryOp->right )) { - if ($this->isMulParenthesized($this->file, $binaryOp)) { + if ($this->isMulParenthesized($this->getFile(), $binaryOp)) { $binaryOp->right->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, true); } 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/Php70/Rector/Ternary/TernaryToNullCoalescingRector.php b/rules/Php70/Rector/Ternary/TernaryToNullCoalescingRector.php index 5a12eff76ba..681fc708e99 100644 --- a/rules/Php70/Rector/Ternary/TernaryToNullCoalescingRector.php +++ b/rules/Php70/Rector/Ternary/TernaryToNullCoalescingRector.php @@ -115,7 +115,7 @@ private function processTernaryWithIsset(Ternary $ternary, Isset_ $isset): ?Coal } if (($ternary->else instanceof Ternary || $ternary->else instanceof BinaryOp) && $this->isTernaryParenthesized( - $this->file, + $this->getFile(), $ternary->cond, $ternary )) { 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/Php74/Rector/Ternary/ParenthesizeNestedTernaryRector.php b/rules/Php74/Rector/Ternary/ParenthesizeNestedTernaryRector.php index 2752fad0cfc..f84e0e31f24 100644 --- a/rules/Php74/Rector/Ternary/ParenthesizeNestedTernaryRector.php +++ b/rules/Php74/Rector/Ternary/ParenthesizeNestedTernaryRector.php @@ -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; } 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 bbfe3fbf536..076f5a00b35 100644 --- a/rules/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector.php +++ b/rules/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector.php @@ -157,7 +157,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 7cb04dfe14e..db8e89ffd3a 100644 --- a/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php +++ b/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php @@ -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; } diff --git a/src/Console/Command/WorkerCommand.php b/src/Console/Command/WorkerCommand.php index 5bffbae23a6..b86320a9cd8 100644 --- a/src/Console/Command/WorkerCommand.php +++ b/src/Console/Command/WorkerCommand.php @@ -131,7 +131,12 @@ private function runWorker( $encoder->on(ReactEvent::ERROR, $handleErrorCallback); // 2. collect diffs + errors from file processor - $decoder->on(ReactEvent::DATA, function (array $json) use ($preFileCallback, $encoder, $configuration, $input): void { + $decoder->on(ReactEvent::DATA, function (array $json) use ( + $preFileCallback, + $encoder, + $configuration, + $input + ): void { $action = $json[ReactCommand::ACTION]; if ($action !== Action::MAIN) { return; @@ -154,7 +159,9 @@ private function runWorker( $encoder->write([ ReactCommand::ACTION => Action::RESULT, self::RESULT => [ - Bridge::FILE_DIFFS => $processResult->getFileDiffs($input->getOption(Option::OUTPUT_FORMAT) !== 'json'), + Bridge::FILE_DIFFS => $processResult->getFileDiffs( + $input->getOption(Option::OUTPUT_FORMAT) !== 'json' + ), Bridge::FILES_COUNT => count($filePaths), Bridge::SYSTEM_ERRORS => $processResult->getSystemErrors(), Bridge::SYSTEM_ERRORS_COUNT => count($processResult->getSystemErrors()), diff --git a/src/Rector/AbstractRector.php b/src/Rector/AbstractRector.php index 57ec3ea5774..63770a565f5 100644 --- a/src/Rector/AbstractRector.php +++ b/src/Rector/AbstractRector.php @@ -57,6 +57,9 @@ abstract class AbstractRector extends NodeVisitorAbstract implements RectorInter protected NodeComparator $nodeComparator; + /** + * @deprecated Use getFile() instead. + */ protected File $file; protected Skipper $skipper; @@ -122,11 +125,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; } @@ -159,7 +163,8 @@ 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; } @@ -175,6 +180,18 @@ final public function leaveNode(Node $node): array|int|Node|null 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; + } + protected function isName(Node $node, string $name): bool { return $this->nodeNameResolver->isName($node, $name); @@ -257,7 +274,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);