From a6da1ae14be33eaf2d693b499850f373967a72d4 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 12 Nov 2024 22:34:03 +0100 Subject: [PATCH 1/2] [deprecation] Deprecate AbstractScopeAwareRector in favor of single AbstractRector --- UPGRADING.md | 41 +++++++++++++++++++ phpstan.neon | 5 +++ .../Fixture/variable_reference.php | 17 ++++++++ .../OptionalParametersAfterRequiredRector.php | 13 +++--- .../SimplifyEmptyCheckOnEmptyArrayRector.php | 7 ++-- .../TernaryFalseExpressionToIfRector.php | 8 ++-- .../ForRepeatedCountToOwnVariableRector.php | 10 +++-- .../FunctionFirstClassCallableRector.php | 7 ++-- .../Assign/RemoveDoubleAssignRector.php | 8 ++-- .../RemoveUnusedVariableAssignRector.php | 9 ++-- ...RemoveUnusedPrivateClassConstantRector.php | 14 ++++--- .../RemoveUnusedPrivateMethodRector.php | 8 ++-- .../RemoveUnusedPromotedPropertyRector.php | 7 ++-- ...UnusedNonEmptyArrayBeforeForeachRector.php | 8 ++-- .../RemoveUnusedPrivatePropertyRector.php | 8 ++-- .../GetCalledClassToStaticClassRector.php | 8 ++-- .../ClassMethod/Php4ConstructorRector.php | 8 ++-- ...isCallOnStaticMethodToStaticCallRector.php | 8 ++-- ...ticCallOnNonStaticToInstanceCallRector.php | 7 ++-- .../Rector/FuncCall/GetClassOnNullRector.php | 8 ++-- .../ClassOnThisVariableObjectRector.php | 8 ++-- .../Array_/FirstClassCallableRector.php | 9 ++-- .../Property/ReadOnlyPropertyRector.php | 7 ++-- .../Rector/Class_/ReadOnlyClassRector.php | 7 ++-- .../PrivatizeFinalClassMethodRector.php | 8 ++-- .../Rector/MethodCall/RenameMethodRector.php | 7 ++-- .../AbstractFalsyScalarRuleFixerRector.php | 4 +- .../BooleanInBooleanNotRuleFixerRector.php | 6 ++- .../Empty_/DisallowedEmptyRuleFixerRector.php | 5 ++- .../BooleanInIfConditionRuleFixerRector.php | 6 ++- ...ooleanInTernaryOperatorRuleFixerRector.php | 5 ++- .../DisallowedShortTernaryRuleFixerRector.php | 5 ++- .../Class_/AddInterfaceByTraitRector.php | 8 ++-- .../StaticCallToMethodCallRector.php | 8 ++-- ...eturnTypeFromBooleanConstReturnsRector.php | 7 ++-- ...turnTypeFromBooleanStrictReturnsRector.php | 7 ++-- ...mericReturnTypeFromStrictReturnsRector.php | 7 ++-- ...eturnTypeFromStrictScalarReturnsRector.php | 7 ++-- .../ParamTypeByMethodCallTypeRector.php | 7 ++-- .../ParamTypeByParentCallTypeRector.php | 8 ++-- .../ClassMethod/ReturnNeverTypeRector.php | 8 ++-- .../ClassMethod/ReturnNullableTypeRector.php | 8 ++-- .../ReturnTypeFromMockObjectRector.php | 7 ++-- .../ReturnTypeFromReturnCastRector.php | 8 ++-- .../ReturnTypeFromReturnDirectArrayRector.php | 8 ++-- .../ReturnTypeFromReturnNewRector.php | 8 ++-- ...turnTypeFromStrictConstantReturnRector.php | 8 ++-- ...ReturnTypeFromStrictFluentReturnRector.php | 8 ++-- .../ReturnTypeFromStrictNativeCallRector.php | 8 ++-- .../ReturnTypeFromStrictNewArrayRector.php | 7 ++-- .../ReturnTypeFromStrictParamRector.php | 8 ++-- .../ReturnTypeFromStrictTypedCallRector.php | 7 ++-- ...eturnTypeFromStrictTypedPropertyRector.php | 8 ++-- .../ReturnTypeFromSymfonySerializerRector.php | 8 ++-- .../ClassMethod/ReturnUnionTypeRector.php | 8 ++-- ...eturnTypeFromStrictScalarReturnsRector.php | 7 ++-- ...eturnTypeFromStrictStringReturnsRector.php | 7 ++-- .../ReturnTypeFromStrictTernaryRector.php | 7 ++-- .../AddClosureNeverReturnTypeRector.php | 8 ++-- ...mptyOnNullableObjectToInstanceOfRector.php | 10 +++-- ...dReturnTypeDeclarationFromYieldsRector.php | 7 ++-- .../AddPropertyTypeDeclarationRector.php | 8 ++-- .../ChangeMethodVisibilityRector.php | 8 ++-- .../Rector/ScopeAwareRectorInterface.php | 4 ++ src/PHPStan/ScopeFetcher.php | 30 ++++++++++++++ src/Rector/AbstractScopeAwareRector.php | 4 ++ .../config/configured_rule.php | 7 +--- .../config/configured_rule.php | 6 +-- .../Variable/ArrayItemForeachValueRector.php | 7 ++-- 69 files changed, 361 insertions(+), 221 deletions(-) create mode 100644 UPGRADING.md create mode 100644 rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/variable_reference.php create mode 100644 src/PHPStan/ScopeFetcher.php diff --git a/UPGRADING.md b/UPGRADING.md new file mode 100644 index 00000000000..a9de1cb3f25 --- /dev/null +++ b/UPGRADING.md @@ -0,0 +1,41 @@ +# Upgrading from Rector 1.x to 2.0 + +## Upgrade for custom Rules writers + +### 1. `AbstractScopeAwareRector` is removed, use `AbstractRector` instead + +The `Rector\Rector\AbstractScopeAwareRector` was too granular to fetch single helper object. It made creating new custom rules ambiguous, one layer more complex and confusing. This class has been removed in favor of standard `AbstractRector`. The `Scope` object can be fetched via `ScopeFetcher`. + +**Before** + +```php +use Rector\Rector\AbstractScopeAwareRector; + +final class SimpleRector extends AbstractScopeAwareRector +{ + public function refactorWithScope(Node $node, Scope $scope): ?Node + { + // ... + } +} +``` + +**After** + +```php +use Rector\Rector\AbstractRector; +use Rector\PHPStan\ScopeFetcher; + +final class SimpleRector extends AbstractRector +{ + public function refactor(Node $node): ?Node + { + if (...) { + // this allow to fetch scope only when needed + $scope = ScopeFetcher::fetch($node); + } + + // ... + } +} +``` diff --git a/phpstan.neon b/phpstan.neon index 2efdb1655e3..aa9e752edc2 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -296,3 +296,8 @@ parameters: # more advanced usage, but not always working # see https://github.com/rectorphp/rector-src/actions/runs/11798721617/job/32865546672?pr=6422#step:5:110 - '#Doing instanceof PHPStan\\Type\\.+ is error\-prone and deprecated#' + + # allowed internally only + - + message: '#Fetching (deprecated )?class constant (.*?) of (deprecated )?class (Rector\\Set\\ValueObject\\DowngradeLevelSetList|Rector\\Symfony\\Set\\(.*?))#' + path: src/Configuration/RectorConfigBuilder.php diff --git a/rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/variable_reference.php b/rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/variable_reference.php new file mode 100644 index 00000000000..d100be2d6a0 --- /dev/null +++ b/rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/variable_reference.php @@ -0,0 +1,17 @@ + diff --git a/rules/CodeQuality/Rector/ClassMethod/OptionalParametersAfterRequiredRector.php b/rules/CodeQuality/Rector/ClassMethod/OptionalParametersAfterRequiredRector.php index f56f5d9323b..2cb9bdaf722 100644 --- a/rules/CodeQuality/Rector/ClassMethod/OptionalParametersAfterRequiredRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/OptionalParametersAfterRequiredRector.php @@ -19,7 +19,8 @@ use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper; use Rector\Php80\NodeResolver\ArgumentSorter; use Rector\Php80\NodeResolver\RequireOptionalParamResolver; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\PHPStan\ScopeFetcher; +use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -27,7 +28,7 @@ /** * @see \Rector\Tests\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector\OptionalParametersAfterRequiredRectorTest */ -final class OptionalParametersAfterRequiredRector extends AbstractScopeAwareRector +final class OptionalParametersAfterRequiredRector extends AbstractRector { /** * @var string @@ -86,10 +87,10 @@ public function getNodeTypes(): array /** * @param ClassMethod|Function_|New_|MethodCall|StaticCall|FuncCall $node */ - public function refactorWithScope( - Node $node, - Scope $scope - ): ClassMethod|Function_|null|New_|MethodCall|StaticCall|FuncCall { + public function refactor(Node $node): ClassMethod|Function_|null|New_|MethodCall|StaticCall|FuncCall + { + $scope = ScopeFetcher::fetch($node); + if ($node instanceof ClassMethod || $node instanceof Function_) { return $this->refactorClassMethodOrFunction($node, $scope); } diff --git a/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php b/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php index 6e34d6f6142..0876458ae70 100644 --- a/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php +++ b/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php @@ -23,7 +23,7 @@ use Rector\NodeAnalyzer\ExprAnalyzer; use Rector\Php\ReservedKeywordAnalyzer; use Rector\PhpParser\AstResolver; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\AllAssignNodePropertyTypeInferer; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -32,7 +32,7 @@ /** * @see \Rector\Tests\CodeQuality\Rector\Empty_\SimplifyEmptyCheckOnEmptyArrayRector\SimplifyEmptyCheckOnEmptyArrayRectorTest */ -final class SimplifyEmptyCheckOnEmptyArrayRector extends AbstractScopeAwareRector +final class SimplifyEmptyCheckOnEmptyArrayRector extends AbstractRector { public function __construct( private readonly ExprAnalyzer $exprAnalyzer, @@ -76,8 +76,9 @@ public function getNodeTypes(): array /** * @param Empty_|BooleanNot $node $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); if ($node instanceof BooleanNot) { if ($node->expr instanceof Empty_ && $this->isAllowedExpr($node->expr->expr, $scope)) { return new NotIdentical($node->expr->expr, new Array_()); diff --git a/rules/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector.php b/rules/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector.php index f564d7304c3..ce6790717f1 100644 --- a/rules/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector.php +++ b/rules/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector.php @@ -10,16 +10,15 @@ use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\If_; -use PHPStan\Analyser\Scope; use Rector\NodeAnalyzer\ExprAnalyzer; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** * @see \Rector\Tests\CodeQuality\Rector\Expression\TernaryFalseExpressionToIfRector\TernaryFalseExpressionToIfRectorTest */ -final class TernaryFalseExpressionToIfRector extends AbstractScopeAwareRector +final class TernaryFalseExpressionToIfRector extends AbstractRector { public function __construct( private readonly ExprAnalyzer $exprAnalyzer @@ -67,8 +66,9 @@ public function getNodeTypes(): array /** * @param Expression $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); if (! $node->expr instanceof Ternary) { return null; } diff --git a/rules/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector.php b/rules/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector.php index 90ac2c315aa..3d205ab88a3 100644 --- a/rules/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector.php +++ b/rules/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector.php @@ -14,15 +14,15 @@ use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\For_; -use PHPStan\Analyser\Scope; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\PHPStan\ScopeFetcher; +use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** * @see \Rector\Tests\CodeQuality\Rector\For_\ForRepeatedCountToOwnVariableRector\ForRepeatedCountToOwnVariableRectorTest */ -final class ForRepeatedCountToOwnVariableRector extends AbstractScopeAwareRector +final class ForRepeatedCountToOwnVariableRector extends AbstractRector { /** * @var string @@ -76,8 +76,10 @@ public function getNodeTypes(): array * @param For_ $node * @return Stmt[]|null */ - public function refactorWithScope(Node $node, Scope $scope): ?array + public function refactor(Node $node): ?array { + $scope = ScopeFetcher::fetch($node); + if ($scope->hasVariableType(self::COUNTER_NAME)->yes()) { return null; } diff --git a/rules/CodingStyle/Rector/FuncCall/FunctionFirstClassCallableRector.php b/rules/CodingStyle/Rector/FuncCall/FunctionFirstClassCallableRector.php index 12c18aa038b..905da135b3a 100644 --- a/rules/CodingStyle/Rector/FuncCall/FunctionFirstClassCallableRector.php +++ b/rules/CodingStyle/Rector/FuncCall/FunctionFirstClassCallableRector.php @@ -10,8 +10,7 @@ use PhpParser\Node\Name; use PhpParser\Node\Scalar\String_; use PhpParser\Node\VariadicPlaceholder; -use PHPStan\Analyser\Scope; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersion; use Rector\VersionBonding\Contract\MinPhpVersionInterface; use ReflectionException; @@ -23,7 +22,7 @@ /** * @see \Rector\Tests\CodingStyle\Rector\FuncCall\FunctionFirstClassCallableRector\FunctionFirstClassCallableRectorTest */ -final class FunctionFirstClassCallableRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class FunctionFirstClassCallableRector extends AbstractRector implements MinPhpVersionInterface { public function getRuleDefinition(): RuleDefinition { @@ -62,7 +61,7 @@ public function getNodeTypes(): array return [FuncCall::class]; } - public function refactorWithScope(Node $node, Scope $scope): ?FuncCall + public function refactor(Node $node): ?FuncCall { if (! $node instanceof FuncCall) { return null; diff --git a/rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php b/rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php index d57b0af8db5..d51b1cdd358 100644 --- a/rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php +++ b/rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php @@ -16,18 +16,17 @@ use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Namespace_; -use PHPStan\Analyser\Scope; use Rector\DeadCode\SideEffect\SideEffectNodeDetector; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** * @see \Rector\Tests\DeadCode\Rector\Assign\RemoveDoubleAssignRector\RemoveDoubleAssignRectorTest */ -final class RemoveDoubleAssignRector extends AbstractScopeAwareRector +final class RemoveDoubleAssignRector extends AbstractRector { public function __construct( private readonly SideEffectNodeDetector $sideEffectNodeDetector, @@ -68,8 +67,9 @@ public function getNodeTypes(): array /** * @param Foreach_|FileWithoutNamespace|If_|Namespace_|ClassMethod|Function_|Closure $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); $stmts = $node->stmts; if ($stmts === null) { return null; diff --git a/rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php b/rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php index ad7bb47cd5c..72114cb66c0 100644 --- a/rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php +++ b/rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php @@ -22,14 +22,15 @@ use Rector\NodeManipulator\StmtsManipulator; use Rector\Php\ReservedKeywordAnalyzer; use Rector\PhpParser\Node\BetterNodeFinder; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\PHPStan\ScopeFetcher; +use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** * @see \Rector\Tests\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector\RemoveUnusedVariableAssignRectorTest */ -final class RemoveUnusedVariableAssignRector extends AbstractScopeAwareRector +final class RemoveUnusedVariableAssignRector extends AbstractRector { public function __construct( private readonly ReservedKeywordAnalyzer $reservedKeywordAnalyzer, @@ -77,7 +78,7 @@ public function getNodeTypes(): array /** * @param ClassMethod|Function_ $node */ - public function refactorWithScope(Node $node, Scope $scope): null|ClassMethod|Function_ + public function refactor(Node $node): null|ClassMethod|Function_ { $stmts = $node->stmts; if ($stmts === null || $stmts === []) { @@ -104,6 +105,8 @@ public function refactorWithScope(Node $node, Scope $scope): null|ClassMethod|Fu /** @var Assign $assign */ $assign = $currentStmt->expr; + $scope = ScopeFetcher::fetch($node); + if ($this->hasCallLikeInAssignExpr($assign, $scope)) { // clean safely $cleanAssignedExpr = $this->cleanCastedExpr($assign->expr); diff --git a/rules/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector.php b/rules/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector.php index dd31ca55b61..bff6d9e63f1 100644 --- a/rules/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector.php +++ b/rules/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector.php @@ -7,10 +7,10 @@ use PhpParser\Node; use PhpParser\Node\Stmt\ClassConst; use PhpParser\NodeTraverser; -use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use Rector\NodeManipulator\ClassConstManipulator; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\PHPStan\ScopeFetcher; +use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -18,7 +18,7 @@ /** * @see \Rector\Tests\DeadCode\Rector\ClassConst\RemoveUnusedPrivateClassConstantRector\RemoveUnusedPrivateClassConstantRectorTest */ -final class RemoveUnusedPrivateClassConstantRector extends AbstractScopeAwareRector +final class RemoveUnusedPrivateClassConstantRector extends AbstractRector { public function __construct( private readonly ClassConstManipulator $classConstManipulator, @@ -64,9 +64,9 @@ public function getNodeTypes(): array /** * @param ClassConst $node */ - public function refactorWithScope(Node $node, Scope $scope): ?int + public function refactor(Node $node): ?int { - if ($this->shouldSkipClassConst($node, $scope)) { + if ($this->shouldSkipClassConst($node)) { return null; } @@ -82,7 +82,7 @@ public function refactorWithScope(Node $node, Scope $scope): ?int return NodeTraverser::REMOVE_NODE; } - private function shouldSkipClassConst(ClassConst $classConst, Scope $scope): bool + private function shouldSkipClassConst(ClassConst $classConst): bool { if (! $classConst->isPrivate()) { return true; @@ -92,6 +92,8 @@ private function shouldSkipClassConst(ClassConst $classConst, Scope $scope): boo return true; } + $scope = ScopeFetcher::fetch($classConst); + $classReflection = $scope->getClassReflection(); if (! $classReflection instanceof ClassReflection) { return false; diff --git a/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php b/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php index 231ceb0afca..6d80982aab9 100644 --- a/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php +++ b/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php @@ -9,12 +9,11 @@ use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; -use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use Rector\DeadCode\NodeAnalyzer\IsClassMethodUsedAnalyzer; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpParser\Node\BetterNodeFinder; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; use Rector\ValueObject\MethodName; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -23,7 +22,7 @@ /** * @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector\RemoveUnusedPrivateMethodRectorTest */ -final class RemoveUnusedPrivateMethodRector extends AbstractScopeAwareRector +final class RemoveUnusedPrivateMethodRector extends AbstractRector { public function __construct( private readonly IsClassMethodUsedAnalyzer $isClassMethodUsedAnalyzer, @@ -75,8 +74,9 @@ public function getNodeTypes(): array /** * @param Class_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); $classMethods = $node->getMethods(); if ($classMethods === []) { diff --git a/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector.php b/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector.php index d08451bf899..841f59eb6c6 100644 --- a/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector.php +++ b/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector.php @@ -20,7 +20,7 @@ use Rector\PhpParser\Node\BetterNodeFinder; use Rector\PhpParser\NodeFinder\PropertyFetchFinder; use Rector\Privatization\NodeManipulator\VisibilityManipulator; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; use Rector\ValueObject\MethodName; use Rector\ValueObject\PhpVersionFeature; @@ -32,7 +32,7 @@ /** * @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector\RemoveUnusedPromotedPropertyRectorTest */ -final class RemoveUnusedPromotedPropertyRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class RemoveUnusedPromotedPropertyRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly PropertyFetchFinder $propertyFetchFinder, @@ -96,8 +96,9 @@ public function getNodeTypes(): array /** * @param Class_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); $constructClassMethod = $node->getMethod(MethodName::CONSTRUCT); if (! $constructClassMethod instanceof ClassMethod) { return null; diff --git a/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php b/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php index 3e72c628921..0f200bfb639 100644 --- a/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php +++ b/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php @@ -21,14 +21,15 @@ use Rector\NodeManipulator\IfManipulator; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Php\ReservedKeywordAnalyzer; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\PHPStan\ScopeFetcher; +use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** * @see \Rector\Tests\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector\RemoveUnusedNonEmptyArrayBeforeForeachRectorTest */ -final class RemoveUnusedNonEmptyArrayBeforeForeachRector extends AbstractScopeAwareRector +final class RemoveUnusedNonEmptyArrayBeforeForeachRector extends AbstractRector { public function __construct( private readonly CountManipulator $countManipulator, @@ -89,9 +90,10 @@ public function getNodeTypes(): array * @param If_|StmtsAwareInterface $node * @return Stmt[]|Foreach_|StmtsAwareInterface|null */ - public function refactorWithScope(Node $node, Scope $scope): array|Node|null + public function refactor(Node $node): array|Node|null { if ($node instanceof If_) { + $scope = ScopeFetcher::fetch($node); return $this->refactorIf($node, $scope); } diff --git a/rules/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector.php b/rules/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector.php index 690222f185c..6f189de50b5 100644 --- a/rules/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector.php +++ b/rules/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector.php @@ -12,20 +12,19 @@ use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\TraitUse; use PhpParser\NodeTraverser; -use PHPStan\Analyser\Scope; use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\DeadCode\NodeAnalyzer\PropertyWriteonlyAnalyzer; use Rector\PhpParser\NodeFinder\PropertyFetchFinder; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** * @see \Rector\Tests\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector\RemoveUnusedPrivatePropertyRectorTest */ -final class RemoveUnusedPrivatePropertyRector extends AbstractScopeAwareRector +final class RemoveUnusedPrivatePropertyRector extends AbstractRector { public function __construct( private readonly PropertyFetchFinder $propertyFetchFinder, @@ -65,8 +64,9 @@ public function getNodeTypes(): array /** * @param Class_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); if ($this->shouldSkipClass($node)) { return null; } diff --git a/rules/Php55/Rector/FuncCall/GetCalledClassToStaticClassRector.php b/rules/Php55/Rector/FuncCall/GetCalledClassToStaticClassRector.php index 399dd4cc97e..8eb08e7370b 100644 --- a/rules/Php55/Rector/FuncCall/GetCalledClassToStaticClassRector.php +++ b/rules/Php55/Rector/FuncCall/GetCalledClassToStaticClassRector.php @@ -6,9 +6,8 @@ use PhpParser\Node; use PhpParser\Node\Expr\FuncCall; -use PHPStan\Analyser\Scope; use Rector\Enum\ObjectReference; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -17,7 +16,7 @@ /** * @see \Rector\Tests\Php55\Rector\FuncCall\GetCalledClassToStaticClassRector\GetCalledClassToStaticClassRectorTest */ -final class GetCalledClassToStaticClassRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class GetCalledClassToStaticClassRector extends AbstractRector implements MinPhpVersionInterface { public function getRuleDefinition(): RuleDefinition { @@ -57,8 +56,9 @@ public function getNodeTypes(): array /** * @param FuncCall $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); if (! $this->isName($node, 'get_called_class')) { return null; } diff --git a/rules/Php70/Rector/ClassMethod/Php4ConstructorRector.php b/rules/Php70/Rector/ClassMethod/Php4ConstructorRector.php index a8b73f6ea76..84f80501a5a 100644 --- a/rules/Php70/Rector/ClassMethod/Php4ConstructorRector.php +++ b/rules/Php70/Rector/ClassMethod/Php4ConstructorRector.php @@ -19,7 +19,8 @@ use Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Php70\NodeAnalyzer\Php4ConstructorClassMethodAnalyzer; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\PHPStan\ScopeFetcher; +use Rector\Rector\AbstractRector; use Rector\ValueObject\MethodName; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; @@ -29,7 +30,7 @@ /** * @see \Rector\Tests\Php70\Rector\ClassMethod\Php4ConstructorRector\Php4ConstructorRectorTest */ -final class Php4ConstructorRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class Php4ConstructorRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly Php4ConstructorClassMethodAnalyzer $php4ConstructorClassMethodAnalyzer, @@ -81,7 +82,7 @@ public function getNodeTypes(): array /** * @param Class_ $node */ - public function refactorWithScope(Node $node, Scope $scope): Class_|int|null + public function refactor(Node $node): Class_|int|null { $className = $this->getName($node); if (! is_string($className)) { @@ -93,6 +94,7 @@ public function refactorWithScope(Node $node, Scope $scope): Class_|int|null return null; } + $scope = ScopeFetcher::fetch($node); if (! $this->php4ConstructorClassMethodAnalyzer->detect($psr4ConstructorMethod, $scope)) { return null; } diff --git a/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php b/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php index 583df058969..ae4c46b20a2 100644 --- a/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php +++ b/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php @@ -12,12 +12,11 @@ use PhpParser\Node\Scalar\Encapsed; use PhpParser\Node\Stmt\Class_; use PhpParser\NodeTraverser; -use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\Php\PhpMethodReflection; use Rector\Enum\ObjectReference; use Rector\NodeCollector\StaticAnalyzer; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; @@ -27,7 +26,7 @@ /** * @see \Rector\Tests\Php70\Rector\MethodCall\ThisCallOnStaticMethodToStaticCallRector\ThisCallOnStaticMethodToStaticCallRectorTest */ -final class ThisCallOnStaticMethodToStaticCallRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class ThisCallOnStaticMethodToStaticCallRector extends AbstractRector implements MinPhpVersionInterface { private bool $hasChanged = false; @@ -91,8 +90,9 @@ public function getNodeTypes(): array /** * @param Class_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); if (! $scope->isInClass()) { return null; } diff --git a/rules/Php70/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php b/rules/Php70/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php index a5a4ea0a947..f23e336510b 100644 --- a/rules/Php70/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php +++ b/rules/Php70/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php @@ -19,7 +19,7 @@ use Rector\Enum\ObjectReference; use Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver; use Rector\NodeCollector\StaticAnalyzer; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; @@ -30,7 +30,7 @@ /** * @see \Rector\Tests\Php70\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector\StaticCallOnNonStaticToInstanceCallRectorTest */ -final class StaticCallOnNonStaticToInstanceCallRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class StaticCallOnNonStaticToInstanceCallRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly StaticAnalyzer $staticAnalyzer, @@ -100,8 +100,9 @@ public function getNodeTypes(): array /** * @param StaticCall $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); if ($node->name instanceof Expr) { return null; } diff --git a/rules/Php72/Rector/FuncCall/GetClassOnNullRector.php b/rules/Php72/Rector/FuncCall/GetClassOnNullRector.php index 7a49f87ebb5..7519333ec28 100644 --- a/rules/Php72/Rector/FuncCall/GetClassOnNullRector.php +++ b/rules/Php72/Rector/FuncCall/GetClassOnNullRector.php @@ -11,9 +11,8 @@ use PhpParser\Node\Expr\Ternary; use PhpParser\Node\Stmt\Class_; use PhpParser\NodeTraverser; -use PHPStan\Analyser\Scope; use Rector\NodeTypeResolver\Node\AttributeKey; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -22,7 +21,7 @@ /** * @see \Rector\Tests\Php72\Rector\FuncCall\GetClassOnNullRector\GetClassOnNullRectorTest */ -final class GetClassOnNullRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class GetClassOnNullRector extends AbstractRector implements MinPhpVersionInterface { public function provideMinPhpVersion(): int { @@ -69,8 +68,9 @@ public function getNodeTypes(): array /** * @param Class_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); $hasChanged = false; $this->traverseNodesWithCallable($node, function (Node $node) use (&$hasChanged): int|null|Ternary { diff --git a/rules/Php80/Rector/ClassConstFetch/ClassOnThisVariableObjectRector.php b/rules/Php80/Rector/ClassConstFetch/ClassOnThisVariableObjectRector.php index 03348d17a23..08a19284bcd 100644 --- a/rules/Php80/Rector/ClassConstFetch/ClassOnThisVariableObjectRector.php +++ b/rules/Php80/Rector/ClassConstFetch/ClassOnThisVariableObjectRector.php @@ -10,8 +10,7 @@ use PhpParser\Node\Identifier; use PhpParser\Node\Name; use PhpParser\Node\Stmt\Class_; -use PHPStan\Analyser\Scope; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -23,7 +22,7 @@ * * @see \Rector\Tests\Php80\Rector\ClassConstFetch\ClassOnThisVariableObjectRector\ClassOnThisVariableObjectRectorTest */ -final class ClassOnThisVariableObjectRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class ClassOnThisVariableObjectRector extends AbstractRector implements MinPhpVersionInterface { public function getRuleDefinition(): RuleDefinition { @@ -66,8 +65,9 @@ public function getNodeTypes(): array /** * @param Class_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); $className = $node->isFinal() ? 'self' : 'static'; $hasChanged = false; diff --git a/rules/Php81/Rector/Array_/FirstClassCallableRector.php b/rules/Php81/Rector/Array_/FirstClassCallableRector.php index 95bbd9b521c..5a6063f2490 100644 --- a/rules/Php81/Rector/Array_/FirstClassCallableRector.php +++ b/rules/Php81/Rector/Array_/FirstClassCallableRector.php @@ -20,7 +20,8 @@ use PHPStan\Reflection\ReflectionProvider; use Rector\NodeCollector\NodeAnalyzer\ArrayCallableMethodMatcher; use Rector\NodeCollector\ValueObject\ArrayCallable; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\PHPStan\ScopeFetcher; +use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType; use Rector\ValueObject\PhpVersion; @@ -31,7 +32,7 @@ /** * @see \Rector\Tests\Php81\Rector\Array_\FirstClassCallableRector\FirstClassCallableRectorTest */ -final class FirstClassCallableRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class FirstClassCallableRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly ArrayCallableMethodMatcher $arrayCallableMethodMatcher, @@ -88,12 +89,14 @@ public function getNodeTypes(): array /** * @param Property|ClassConst|Array_ $node */ - public function refactorWithScope(Node $node, Scope $scope): int|null|StaticCall|MethodCall + public function refactor(Node $node): int|null|StaticCall|MethodCall { if ($node instanceof Property || $node instanceof ClassConst) { return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } + $scope = ScopeFetcher::fetch($node); + $arrayCallable = $this->arrayCallableMethodMatcher->match($node, $scope); if (! $arrayCallable instanceof ArrayCallable) { return null; diff --git a/rules/Php81/Rector/Property/ReadOnlyPropertyRector.php b/rules/Php81/Rector/Property/ReadOnlyPropertyRector.php index df16087c850..11afa8212ed 100644 --- a/rules/Php81/Rector/Property/ReadOnlyPropertyRector.php +++ b/rules/Php81/Rector/Property/ReadOnlyPropertyRector.php @@ -26,7 +26,7 @@ use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\Privatization\NodeManipulator\VisibilityManipulator; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\ValueObject\MethodName; use Rector\ValueObject\PhpVersionFeature; use Rector\ValueObject\Visibility; @@ -37,7 +37,7 @@ /** * @see \Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\ReadOnlyPropertyRectorTest */ -final class ReadOnlyPropertyRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class ReadOnlyPropertyRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly PropertyManipulator $propertyManipulator, @@ -99,8 +99,9 @@ public function getNodeTypes(): array /** * @param Class_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); if ($this->shouldSkip($node)) { return null; } diff --git a/rules/Php82/Rector/Class_/ReadOnlyClassRector.php b/rules/Php82/Rector/Class_/ReadOnlyClassRector.php index 26b3bb58811..964c0805e34 100644 --- a/rules/Php82/Rector/Class_/ReadOnlyClassRector.php +++ b/rules/Php82/Rector/Class_/ReadOnlyClassRector.php @@ -19,7 +19,7 @@ use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer; use Rector\Php81\Enum\AttributeName; use Rector\Privatization\NodeManipulator\VisibilityManipulator; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\ValueObject\MethodName; use Rector\ValueObject\PhpVersionFeature; use Rector\ValueObject\Visibility; @@ -30,7 +30,7 @@ /** * @see \Rector\Tests\Php82\Rector\Class_\ReadOnlyClassRector\ReadOnlyClassRectorTest */ -final class ReadOnlyClassRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class ReadOnlyClassRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly ClassAnalyzer $classAnalyzer, @@ -79,8 +79,9 @@ public function getNodeTypes(): array /** * @param Class_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); if ($this->shouldSkip($node, $scope)) { return null; } diff --git a/rules/Privatization/Rector/ClassMethod/PrivatizeFinalClassMethodRector.php b/rules/Privatization/Rector/ClassMethod/PrivatizeFinalClassMethodRector.php index c22da25289c..f58be7f23f5 100644 --- a/rules/Privatization/Rector/ClassMethod/PrivatizeFinalClassMethodRector.php +++ b/rules/Privatization/Rector/ClassMethod/PrivatizeFinalClassMethodRector.php @@ -8,20 +8,19 @@ use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; -use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\Privatization\Guard\OverrideByParentClassGuard; use Rector\Privatization\NodeManipulator\VisibilityManipulator; use Rector\Privatization\VisibilityGuard\ClassMethodVisibilityGuard; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** * @see \Rector\Tests\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector\PrivatizeFinalClassMethodRectorTest */ -final class PrivatizeFinalClassMethodRector extends AbstractScopeAwareRector +final class PrivatizeFinalClassMethodRector extends AbstractRector { public function __construct( private readonly ClassMethodVisibilityGuard $classMethodVisibilityGuard, @@ -70,8 +69,9 @@ public function getNodeTypes(): array /** * @param Class_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); if (! $node->isFinal()) { return null; } diff --git a/rules/Renaming/Rector/MethodCall/RenameMethodRector.php b/rules/Renaming/Rector/MethodCall/RenameMethodRector.php index c71fe3438ed..ce8d6094a31 100644 --- a/rules/Renaming/Rector/MethodCall/RenameMethodRector.php +++ b/rules/Renaming/Rector/MethodCall/RenameMethodRector.php @@ -19,7 +19,7 @@ use PHPStan\Reflection\ReflectionProvider; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\NodeManipulator\ClassManipulator; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; use Rector\Renaming\Contract\MethodCallRenameInterface; use Rector\Renaming\ValueObject\MethodCallRename; @@ -31,7 +31,7 @@ /** * @see \Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\RenameMethodRectorTest */ -final class RenameMethodRector extends AbstractScopeAwareRector implements ConfigurableRectorInterface +final class RenameMethodRector extends AbstractRector implements ConfigurableRectorInterface { /** * @var MethodCallRenameInterface[] @@ -75,8 +75,9 @@ public function getNodeTypes(): array /** * @param MethodCall|NullsafeMethodCall|StaticCall|Class_|Interface_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); if ($node instanceof Class_ || $node instanceof Interface_) { return $this->refactorClass($node, $scope); } diff --git a/rules/Strict/Rector/AbstractFalsyScalarRuleFixerRector.php b/rules/Strict/Rector/AbstractFalsyScalarRuleFixerRector.php index 47369dfc51c..72509ce12a0 100644 --- a/rules/Strict/Rector/AbstractFalsyScalarRuleFixerRector.php +++ b/rules/Strict/Rector/AbstractFalsyScalarRuleFixerRector.php @@ -5,7 +5,7 @@ namespace Rector\Strict\Rector; use Rector\Contract\Rector\ConfigurableRectorInterface; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Webmozart\Assert\Assert; /** @@ -13,7 +13,7 @@ * * @internal */ -abstract class AbstractFalsyScalarRuleFixerRector extends AbstractScopeAwareRector implements ConfigurableRectorInterface +abstract class AbstractFalsyScalarRuleFixerRector extends AbstractRector implements ConfigurableRectorInterface { /** * @api diff --git a/rules/Strict/Rector/BooleanNot/BooleanInBooleanNotRuleFixerRector.php b/rules/Strict/Rector/BooleanNot/BooleanInBooleanNotRuleFixerRector.php index cf455e4051a..cd80d0fc4c9 100644 --- a/rules/Strict/Rector/BooleanNot/BooleanInBooleanNotRuleFixerRector.php +++ b/rules/Strict/Rector/BooleanNot/BooleanInBooleanNotRuleFixerRector.php @@ -7,7 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\BooleanNot; -use PHPStan\Analyser\Scope; +use Rector\PHPStan\ScopeFetcher; use Rector\Strict\NodeFactory\ExactCompareFactory; use Rector\Strict\Rector\AbstractFalsyScalarRuleFixerRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; @@ -80,8 +80,10 @@ public function getNodeTypes(): array /** * @param BooleanNot $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Expr + public function refactor(Node $node): ?Expr { + $scope = ScopeFetcher::fetch($node); + $exprType = $scope->getNativeType($node->expr); if ($exprType->isBoolean()->yes()) { return null; diff --git a/rules/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector.php b/rules/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector.php index 1eab4229a88..bfb3cac5479 100644 --- a/rules/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector.php +++ b/rules/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector.php @@ -15,6 +15,7 @@ use PHPStan\Analyser\Scope; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\NodeAnalyzer\ExprAnalyzer; +use Rector\PHPStan\ScopeFetcher; use Rector\Strict\NodeAnalyzer\UnitializedPropertyAnalyzer; use Rector\Strict\NodeFactory\ExactCompareFactory; use Rector\Strict\Rector\AbstractFalsyScalarRuleFixerRector; @@ -79,8 +80,10 @@ public function getNodeTypes(): array /** * @param Empty_|BooleanNot $node */ - public function refactorWithScope(Node $node, Scope $scope): Expr|null + public function refactor(Node $node): Expr|null { + $scope = ScopeFetcher::fetch($node); + if ($node instanceof BooleanNot) { return $this->refactorBooleanNot($node, $scope); } diff --git a/rules/Strict/Rector/If_/BooleanInIfConditionRuleFixerRector.php b/rules/Strict/Rector/If_/BooleanInIfConditionRuleFixerRector.php index 7d767a509ca..47feb2694f4 100644 --- a/rules/Strict/Rector/If_/BooleanInIfConditionRuleFixerRector.php +++ b/rules/Strict/Rector/If_/BooleanInIfConditionRuleFixerRector.php @@ -7,7 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Stmt\If_; -use PHPStan\Analyser\Scope; +use Rector\PHPStan\ScopeFetcher; use Rector\Strict\NodeFactory\ExactCompareFactory; use Rector\Strict\Rector\AbstractFalsyScalarRuleFixerRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; @@ -81,10 +81,12 @@ public function getNodeTypes(): array /** * @param If_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?If_ + public function refactor(Node $node): ?If_ { $hasChanged = false; + $scope = ScopeFetcher::fetch($node); + // 1. if $ifCondExprType = $scope->getNativeType($node->cond); $notIdentical = $this->exactCompareFactory->createNotIdenticalFalsyCompare( diff --git a/rules/Strict/Rector/Ternary/BooleanInTernaryOperatorRuleFixerRector.php b/rules/Strict/Rector/Ternary/BooleanInTernaryOperatorRuleFixerRector.php index 64c296c2de5..e647ad252c4 100644 --- a/rules/Strict/Rector/Ternary/BooleanInTernaryOperatorRuleFixerRector.php +++ b/rules/Strict/Rector/Ternary/BooleanInTernaryOperatorRuleFixerRector.php @@ -7,7 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Ternary; -use PHPStan\Analyser\Scope; +use Rector\PHPStan\ScopeFetcher; use Rector\Strict\NodeFactory\ExactCompareFactory; use Rector\Strict\Rector\AbstractFalsyScalarRuleFixerRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; @@ -72,13 +72,14 @@ public function getNodeTypes(): array /** * @param Ternary $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Ternary + public function refactor(Node $node): ?Ternary { // skip short ternary if (! $node->if instanceof Expr) { return null; } + $scope = ScopeFetcher::fetch($node); $exprType = $scope->getNativeType($node->cond); $expr = $this->exactCompareFactory->createNotIdenticalFalsyCompare( diff --git a/rules/Strict/Rector/Ternary/DisallowedShortTernaryRuleFixerRector.php b/rules/Strict/Rector/Ternary/DisallowedShortTernaryRuleFixerRector.php index 69f4bb18cc2..cdbfaaa9eb8 100644 --- a/rules/Strict/Rector/Ternary/DisallowedShortTernaryRuleFixerRector.php +++ b/rules/Strict/Rector/Ternary/DisallowedShortTernaryRuleFixerRector.php @@ -9,6 +9,7 @@ use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Ternary; use PHPStan\Analyser\Scope; +use Rector\PHPStan\ScopeFetcher; use Rector\Strict\NodeFactory\ExactCompareFactory; use Rector\Strict\Rector\AbstractFalsyScalarRuleFixerRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; @@ -75,7 +76,7 @@ public function getNodeTypes(): array /** * @param Ternary $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Ternary + public function refactor(Node $node): ?Ternary { $this->hasChanged = false; @@ -84,6 +85,8 @@ public function refactorWithScope(Node $node, Scope $scope): ?Ternary return null; } + $scope = ScopeFetcher::fetch($node); + // special case for reset() function if ($node->cond instanceof FuncCall && $this->isName($node->cond, 'reset')) { $this->refactorResetFuncCall($node, $node->cond, $scope); diff --git a/rules/Transform/Rector/Class_/AddInterfaceByTraitRector.php b/rules/Transform/Rector/Class_/AddInterfaceByTraitRector.php index 7003332b67a..55280c8441a 100644 --- a/rules/Transform/Rector/Class_/AddInterfaceByTraitRector.php +++ b/rules/Transform/Rector/Class_/AddInterfaceByTraitRector.php @@ -7,10 +7,9 @@ use PhpParser\Node; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Stmt\Class_; -use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use Rector\Contract\Rector\ConfigurableRectorInterface; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; use Webmozart\Assert\Assert; @@ -19,7 +18,7 @@ * @api used in rector-doctrine * @see \Rector\Tests\Transform\Rector\Class_\AddInterfaceByTraitRector\AddInterfaceByTraitRectorTest */ -final class AddInterfaceByTraitRector extends AbstractScopeAwareRector implements ConfigurableRectorInterface +final class AddInterfaceByTraitRector extends AbstractRector implements ConfigurableRectorInterface { /** * @var array @@ -62,8 +61,9 @@ public function getNodeTypes(): array /** * @param Class_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); $classReflection = $scope->getClassReflection(); if (! $classReflection instanceof ClassReflection) { return null; diff --git a/rules/Transform/Rector/StaticCall/StaticCallToMethodCallRector.php b/rules/Transform/Rector/StaticCall/StaticCallToMethodCallRector.php index c34ed4dac85..40eb7d85ae1 100644 --- a/rules/Transform/Rector/StaticCall/StaticCallToMethodCallRector.php +++ b/rules/Transform/Rector/StaticCall/StaticCallToMethodCallRector.php @@ -10,10 +10,9 @@ use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Stmt\Class_; -use PHPStan\Analyser\Scope; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\Exception\ShouldNotHappenException; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\Transform\NodeAnalyzer\FuncCallStaticCallToMethodCallAnalyzer; use Rector\Transform\ValueObject\StaticCallToMethodCall; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; @@ -23,7 +22,7 @@ /** * @see \Rector\Tests\Transform\Rector\StaticCall\StaticCallToMethodCallRector\StaticCallToMethodCallRectorTest */ -final class StaticCallToMethodCallRector extends AbstractScopeAwareRector implements ConfigurableRectorInterface +final class StaticCallToMethodCallRector extends AbstractRector implements ConfigurableRectorInterface { /** * @var StaticCallToMethodCall[] @@ -96,8 +95,9 @@ public function getNodeTypes(): array /** * @param Class_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); $class = $node; $hasChanged = false; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanConstReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanConstReturnsRector.php index f1f65602b3e..35aa5ad5eb8 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanConstReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanConstReturnsRector.php @@ -14,7 +14,7 @@ use PHPStan\Analyser\Scope; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\PhpParser\Node\Value\ValueResolver; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer; use Rector\ValueObject\PhpVersionFeature; use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard; @@ -25,7 +25,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\BoolReturnTypeFromBooleanConstReturnsRector\BoolReturnTypeFromBooleanConstReturnsRectorTest */ -final class BoolReturnTypeFromBooleanConstReturnsRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class BoolReturnTypeFromBooleanConstReturnsRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly ValueResolver $valueResolver, @@ -82,8 +82,9 @@ public function getNodeTypes(): array /** * @param ClassMethod|Function_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); if ($this->shouldSkip($node, $scope)) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector.php index 092751fa9fd..9f3e1821a31 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector.php @@ -31,7 +31,7 @@ use PHPStan\Reflection\ReflectionProvider; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\PhpParser\Node\Value\ValueResolver; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer; use Rector\ValueObject\PhpVersionFeature; use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard; @@ -42,7 +42,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\BoolReturnTypeFromBooleanStrictReturnsRector\BoolReturnTypeFromBooleanStrictReturnsRectorTest */ -final class BoolReturnTypeFromBooleanStrictReturnsRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class BoolReturnTypeFromBooleanStrictReturnsRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly ReflectionProvider $reflectionProvider, @@ -92,8 +92,9 @@ public function getNodeTypes(): array /** * @param ClassMethod|Function_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); if ($this->shouldSkip($node, $scope)) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictReturnsRector.php index b326cadb879..46410023d97 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictReturnsRector.php @@ -15,7 +15,7 @@ use PhpParser\Node\Stmt\Return_; use PHPStan\Analyser\Scope; use Rector\PhpParser\Node\BetterNodeFinder; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer; use Rector\ValueObject\PhpVersionFeature; use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard; @@ -26,7 +26,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\NumericReturnTypeFromStrictReturnsRector\NumericReturnTypeFromStrictReturnsRectorTest */ -final class NumericReturnTypeFromStrictReturnsRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class NumericReturnTypeFromStrictReturnsRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, @@ -74,8 +74,9 @@ public function getNodeTypes(): array /** * @param ClassMethod|Function_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); if ($this->shouldSkip($node, $scope)) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictScalarReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictScalarReturnsRector.php index 2bc9a0f3d48..ac66f243251 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictScalarReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictScalarReturnsRector.php @@ -13,7 +13,7 @@ use PhpParser\Node\Stmt\Function_; use PHPStan\Analyser\Scope; use Rector\PhpParser\Node\BetterNodeFinder; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer; use Rector\ValueObject\PhpVersionFeature; use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard; @@ -24,7 +24,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\NumericReturnTypeFromStrictScalarReturnsRector\NumericReturnTypeFromStrictScalarReturnsRectorTest */ -final class NumericReturnTypeFromStrictScalarReturnsRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class NumericReturnTypeFromStrictScalarReturnsRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, @@ -72,8 +72,9 @@ public function getNodeTypes(): array /** * @param ClassMethod|Function_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); if ($this->shouldSkip($node, $scope)) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php index b6335f87c3e..836c5114454 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php @@ -15,7 +15,7 @@ use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\StaticTypeMapper\Mapper\PhpParserNodeMapper; use Rector\StaticTypeMapper\StaticTypeMapper; use Rector\TypeDeclaration\Guard\ParamTypeAddGuard; @@ -27,7 +27,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector\ParamTypeByMethodCallTypeRectorTest */ -final class ParamTypeByMethodCallTypeRector extends AbstractScopeAwareRector +final class ParamTypeByMethodCallTypeRector extends AbstractRector { public function __construct( private readonly CallerParamMatcher $callerParamMatcher, @@ -102,8 +102,9 @@ public function getNodeTypes(): array /** * @param Class_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); $hasChanged = false; foreach ($node->getMethods() as $classMethod) { diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php index 15bd9317ab2..6b264314740 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php @@ -8,12 +8,11 @@ use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Param; use PhpParser\Node\Stmt\ClassMethod; -use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use Rector\Enum\ObjectReference; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpParser\Node\BetterNodeFinder; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; use Rector\TypeDeclaration\NodeAnalyzer\CallerParamMatcher; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -22,7 +21,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeByParentCallTypeRector\ParamTypeByParentCallTypeRectorTest */ -final class ParamTypeByParentCallTypeRector extends AbstractScopeAwareRector +final class ParamTypeByParentCallTypeRector extends AbstractRector { public function __construct( private readonly CallerParamMatcher $callerParamMatcher, @@ -84,8 +83,9 @@ public function getNodeTypes(): array /** * @param ClassMethod $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); if ($this->shouldSkip($node)) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php index 239cdd031b3..6d132b0d88c 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php @@ -7,8 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; -use PHPStan\Analyser\Scope; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeManipulator\AddNeverReturnType; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; @@ -18,7 +17,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\ReturnNeverTypeRectorTest */ -final class ReturnNeverTypeRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class ReturnNeverTypeRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly AddNeverReturnType $addNeverReturnType @@ -64,8 +63,9 @@ public function getNodeTypes(): array /** * @param ClassMethod|Function_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); return $this->addNeverReturnType->add($node, $scope); } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnNullableTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnNullableTypeRector.php index 72372252104..6f8312b0d9e 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnNullableTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnNullableTypeRector.php @@ -8,11 +8,10 @@ use PhpParser\Node\NullableType; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; -use PHPStan\Analyser\Scope; use PHPStan\Type\UnionType; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; use Rector\PHPStanStaticTypeMapper\TypeMapper\UnionTypeMapper; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer; use Rector\ValueObject\PhpVersionFeature; use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard; @@ -23,7 +22,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNullableTypeRector\ReturnNullableTypeRectorTest */ -final class ReturnNullableTypeRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class ReturnNullableTypeRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly UnionTypeMapper $unionTypeMapper, @@ -83,8 +82,9 @@ public function provideMinPhpVersion(): int /** * @param ClassMethod|Function_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); // empty body, nothing to resolve if ($node->stmts === null || $node->stmts === []) { return null; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromMockObjectRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromMockObjectRector.php index 1a44882802f..2f439ad347a 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromMockObjectRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromMockObjectRector.php @@ -15,7 +15,7 @@ use PHPStan\Type\Type; use Rector\Enum\ClassName; use Rector\PhpParser\Node\BetterNodeFinder; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer; use Rector\ValueObject\PhpVersionFeature; use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard; @@ -26,7 +26,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromMockObjectRector\ReturnTypeFromMockObjectRectorTest */ -final class ReturnTypeFromMockObjectRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class ReturnTypeFromMockObjectRector extends AbstractRector implements MinPhpVersionInterface { /** * @var string @@ -78,8 +78,9 @@ public function getNodeTypes(): array /** * @param ClassMethod $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); // type is already known if ($node->returnType instanceof Node) { return null; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnCastRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnCastRector.php index d7b2c29877e..e242daa989f 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnCastRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnCastRector.php @@ -7,8 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; -use PHPStan\Analyser\Scope; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeManipulator\AddReturnTypeFromCast; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; @@ -18,7 +17,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnCastRector\ReturnTypeFromReturnCastRectorTest */ -final class ReturnTypeFromReturnCastRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class ReturnTypeFromReturnCastRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly AddReturnTypeFromCast $addReturnTypeFromCast @@ -73,8 +72,9 @@ public function getNodeTypes(): array /** * @param ClassMethod|Function_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); return $this->addReturnTypeFromCast->add($node, $scope); } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector.php index a697a124510..21bfce9582d 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector.php @@ -10,8 +10,7 @@ use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Return_; -use PHPStan\Analyser\Scope; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer; use Rector\ValueObject\PhpVersionFeature; use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard; @@ -22,7 +21,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnDirectArrayRector\ReturnTypeFromReturnDirectArrayRectorTest */ -final class ReturnTypeFromReturnDirectArrayRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class ReturnTypeFromReturnDirectArrayRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, @@ -69,8 +68,9 @@ public function getNodeTypes(): array /** * @param ClassMethod|Function_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); // already has return type, skip if ($node->returnType instanceof Node) { return null; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php index ab0ea805dc7..c137aa58f15 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php @@ -11,7 +11,6 @@ use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Return_; -use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\ReflectionProvider; use PHPStan\Type\ObjectType; @@ -25,7 +24,7 @@ use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; use Rector\StaticTypeMapper\StaticTypeMapper; use Rector\StaticTypeMapper\ValueObject\Type\SelfStaticType; @@ -42,7 +41,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\ReturnTypeFromReturnNewRectorTest */ -final class ReturnTypeFromReturnNewRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class ReturnTypeFromReturnNewRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly TypeFactory $typeFactory, @@ -97,8 +96,9 @@ public function getNodeTypes(): array /** * @param ClassMethod|Function_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); // already filled if ($node->returnType instanceof Node) { return null; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictConstantReturnRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictConstantReturnRector.php index 4b71eacfb9c..6d357bfa1b2 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictConstantReturnRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictConstantReturnRector.php @@ -9,12 +9,11 @@ use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Return_; -use PHPStan\Analyser\Scope; use PHPStan\Type\Type; use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\StaticTypeMapper\StaticTypeMapper; use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer; use Rector\ValueObject\PhpVersion; @@ -26,7 +25,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictConstantReturnRector\ReturnTypeFromStrictConstantReturnRectorTest */ -final class ReturnTypeFromStrictConstantReturnRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class ReturnTypeFromStrictConstantReturnRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, @@ -79,8 +78,9 @@ public function getNodeTypes(): array /** * @param ClassMethod $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); if ($node->returnType instanceof Node) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictFluentReturnRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictFluentReturnRector.php index ae36bdda9cf..60cf5337bbf 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictFluentReturnRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictFluentReturnRector.php @@ -7,13 +7,12 @@ use PhpParser\Node; use PhpParser\Node\Name; use PhpParser\Node\Stmt\ClassMethod; -use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use PHPStan\Type\ObjectType; use PHPStan\Type\StaticType; use PHPStan\Type\ThisType; use Rector\Php\PhpVersionProvider; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer; use Rector\ValueObject\PhpVersionFeature; @@ -25,7 +24,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictFluentReturnRector\ReturnTypeFromStrictFluentReturnRectorTest */ -final class ReturnTypeFromStrictFluentReturnRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class ReturnTypeFromStrictFluentReturnRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, @@ -78,8 +77,9 @@ public function provideMinPhpVersion(): int /** * @param ClassMethod $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); // already typed → skip if ($node->returnType instanceof Node) { return null; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNativeCallRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNativeCallRector.php index 65fbd7de027..15c8a30a2c9 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNativeCallRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNativeCallRector.php @@ -7,8 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; -use PHPStan\Analyser\Scope; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeManipulator\AddReturnTypeFromStrictNativeCall; use Rector\ValueObject\PhpVersion; use Rector\VersionBonding\Contract\MinPhpVersionInterface; @@ -18,7 +17,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNativeCallRector\ReturnTypeFromStrictNativeCallRectorTest */ -final class ReturnTypeFromStrictNativeCallRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class ReturnTypeFromStrictNativeCallRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly AddReturnTypeFromStrictNativeCall $addReturnTypeFromStrictNativeCall @@ -64,8 +63,9 @@ public function getNodeTypes(): array /** * @param ClassMethod|Function_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); return $this->addReturnTypeFromStrictNativeCall->add($node, $scope); } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php index 6ae9b75e9c8..9ea51991478 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php @@ -24,7 +24,7 @@ use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger; use Rector\PhpParser\Node\BetterNodeFinder; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer; use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer; use Rector\ValueObject\PhpVersion; @@ -36,7 +36,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\ReturnTypeFromStrictNewArrayRectorTest */ -final class ReturnTypeFromStrictNewArrayRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class ReturnTypeFromStrictNewArrayRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly PhpDocTypeChanger $phpDocTypeChanger, @@ -91,8 +91,9 @@ public function getNodeTypes(): array /** * @param ClassMethod|Function_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); if ($this->shouldSkip($node, $scope)) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictParamRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictParamRector.php index 2b97c3c6040..381fb348650 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictParamRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictParamRector.php @@ -7,8 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; -use PHPStan\Analyser\Scope; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeManipulator\AddReturnTypeFromParam; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; @@ -18,7 +17,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\ReturnTypeFromStrictParamRectorTest */ -final class ReturnTypeFromStrictParamRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class ReturnTypeFromStrictParamRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly AddReturnTypeFromParam $addReturnTypeFromParam, @@ -69,8 +68,9 @@ public function provideMinPhpVersion(): int /** * @param ClassMethod|Function_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); return $this->addReturnTypeFromParam->add($node, $scope); } } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php index 61c8c2bfaba..ad505976afd 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php @@ -21,7 +21,7 @@ use Rector\Php\PhpVersionProvider; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\StaticTypeMapper\StaticTypeMapper; use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer; use Rector\TypeDeclaration\NodeAnalyzer\TypeNodeUnwrapper; @@ -36,7 +36,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector\ReturnTypeFromStrictTypedCallRectorTest */ -final class ReturnTypeFromStrictTypedCallRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class ReturnTypeFromStrictTypedCallRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly TypeNodeUnwrapper $typeNodeUnwrapper, @@ -103,8 +103,9 @@ public function provideMinPhpVersion(): int /** * @param ClassMethod|Function_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); // already filled → skip if ($node->returnType instanceof Node) { return null; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php index 08e955c9a1a..4db4b695c76 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php @@ -9,14 +9,13 @@ use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\StaticPropertyFetch; use PhpParser\Node\Stmt\ClassMethod; -use PHPStan\Analyser\Scope; use PHPStan\Reflection\Php\PhpPropertyReflection; use PHPStan\Type\MixedType; use PHPStan\Type\Type; use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; use Rector\StaticTypeMapper\StaticTypeMapper; use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer; @@ -29,7 +28,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedPropertyRector\ReturnTypeFromStrictTypedPropertyRectorTest */ -final class ReturnTypeFromStrictTypedPropertyRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class ReturnTypeFromStrictTypedPropertyRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly TypeFactory $typeFactory, @@ -84,8 +83,9 @@ public function getNodeTypes(): array /** * @param ClassMethod $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); if ($node->returnType instanceof Node) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php index 093a1b22e89..7aa25b48ec9 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php @@ -9,11 +9,10 @@ use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Return_; -use PHPStan\Analyser\Scope; use PHPStan\Type\ObjectType; use Rector\NodeAnalyzer\ArgsAnalyzer; use Rector\PhpParser\Node\Value\ValueResolver; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard; use Rector\VersionBonding\Contract\MinPhpVersionInterface; @@ -23,7 +22,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromSymfonySerializerRector\ReturnTypeFromSymfonySerializerRectorTest */ -final class ReturnTypeFromSymfonySerializerRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class ReturnTypeFromSymfonySerializerRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, @@ -79,8 +78,9 @@ public function provideMinPhpVersion(): int /** * @param ClassMethod $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); if ($node->stmts === null) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector.php index 79d681f8b6c..3e1e8a9c742 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector.php @@ -7,8 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; -use PHPStan\Analyser\Scope; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeManipulator\AddUnionReturnType; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; @@ -18,7 +17,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\ReturnUnionTypeRectorTest */ -final class ReturnUnionTypeRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class ReturnUnionTypeRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly AddUnionReturnType $addUnionReturnType, @@ -84,8 +83,9 @@ public function provideMinPhpVersion(): int /** * @param ClassMethod|Function_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); return $this->addUnionReturnType->add($node, $scope); } } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/StringReturnTypeFromStrictScalarReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/StringReturnTypeFromStrictScalarReturnsRector.php index b4970e872f0..ed7be17f130 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/StringReturnTypeFromStrictScalarReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/StringReturnTypeFromStrictScalarReturnsRector.php @@ -12,7 +12,7 @@ use PhpParser\Node\Stmt\Function_; use PHPStan\Analyser\Scope; use Rector\PhpParser\Node\BetterNodeFinder; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer; use Rector\ValueObject\PhpVersion; use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard; @@ -23,7 +23,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\StringReturnTypeFromStrictScalarReturnsRector\StringReturnTypeFromStrictScalarReturnsRectorTest */ -final class StringReturnTypeFromStrictScalarReturnsRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class StringReturnTypeFromStrictScalarReturnsRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, @@ -79,8 +79,9 @@ public function getNodeTypes(): array /** * @param ClassMethod|Function_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); // already added → skip if ($node->returnType instanceof Node) { return null; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/StringReturnTypeFromStrictStringReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/StringReturnTypeFromStrictStringReturnsRector.php index d51b81e42ea..c4b3e27d35d 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/StringReturnTypeFromStrictStringReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/StringReturnTypeFromStrictStringReturnsRector.php @@ -14,7 +14,7 @@ use PhpParser\Node\Stmt\Return_; use PHPStan\Analyser\Scope; use Rector\PhpParser\Node\BetterNodeFinder; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer; use Rector\ValueObject\PhpVersion; use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard; @@ -25,7 +25,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\StringReturnTypeFromStrictStringReturnsRector\StringReturnTypeFromStrictStringReturnsRectorTest */ -final class StringReturnTypeFromStrictStringReturnsRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class StringReturnTypeFromStrictStringReturnsRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, @@ -81,8 +81,9 @@ public function getNodeTypes(): array /** * @param ClassMethod|Function_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); // already added → skip if ($node->returnType instanceof Node) { return null; diff --git a/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php b/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php index d8ffd17e623..ecdbe62ea41 100644 --- a/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php +++ b/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php @@ -15,7 +15,7 @@ use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\StaticTypeMapper\StaticTypeMapper; use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer; use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer; @@ -28,7 +28,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\Class_\ReturnTypeFromStrictTernaryRector\ReturnTypeFromStrictTernaryRectorTest */ -final class ReturnTypeFromStrictTernaryRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class ReturnTypeFromStrictTernaryRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, @@ -78,8 +78,9 @@ public function getNodeTypes(): array /** * @param ClassMethod|Function_ $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); if ($this->shouldSkip($node, $scope)) { return null; } diff --git a/rules/TypeDeclaration/Rector/Closure/AddClosureNeverReturnTypeRector.php b/rules/TypeDeclaration/Rector/Closure/AddClosureNeverReturnTypeRector.php index 7c9f4703d5e..ed8e7ef9840 100644 --- a/rules/TypeDeclaration/Rector/Closure/AddClosureNeverReturnTypeRector.php +++ b/rules/TypeDeclaration/Rector/Closure/AddClosureNeverReturnTypeRector.php @@ -6,8 +6,7 @@ use PhpParser\Node; use PhpParser\Node\Expr\Closure; -use PHPStan\Analyser\Scope; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeManipulator\AddNeverReturnType; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; @@ -17,7 +16,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\Closure\AddClosureNeverReturnTypeRector\AddClosureNeverReturnTypeRectorTest */ -final class AddClosureNeverReturnTypeRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class AddClosureNeverReturnTypeRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly AddNeverReturnType $addNeverReturnType @@ -55,8 +54,9 @@ public function getNodeTypes(): array /** * @param Closure $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); return $this->addNeverReturnType->add($node, $scope); } diff --git a/rules/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector.php b/rules/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector.php index 231399e93b8..c0a75e66399 100644 --- a/rules/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector.php +++ b/rules/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector.php @@ -10,12 +10,12 @@ use PhpParser\Node\Expr\Empty_; use PhpParser\Node\Expr\Instanceof_; use PhpParser\Node\Name; -use PHPStan\Analyser\Scope; use PHPStan\Type\ObjectType; use PHPStan\Type\TypeCombinator; use PHPStan\Type\UnionType; +use Rector\PHPStan\ScopeFetcher; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\StaticTypeMapper\StaticTypeMapper; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -23,7 +23,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\EmptyOnNullableObjectToInstanceOfRectorTest */ -final class EmptyOnNullableObjectToInstanceOfRector extends AbstractScopeAwareRector +final class EmptyOnNullableObjectToInstanceOfRector extends AbstractRector { public function __construct( private readonly StaticTypeMapper $staticTypeMapper @@ -77,7 +77,7 @@ public function getNodeTypes(): array /** * @param Empty_|BooleanNot $node */ - public function refactorWithScope(Node $node, Scope $scope): null|Instanceof_|BooleanNot + public function refactor(Node $node): null|Instanceof_|BooleanNot { if ($node instanceof BooleanNot) { if (! $node->expr instanceof Empty_) { @@ -95,6 +95,8 @@ public function refactorWithScope(Node $node, Scope $scope): null|Instanceof_|Bo return null; } + $scope = ScopeFetcher::fetch($node); + $exprType = $scope->getNativeType($empty->expr); if (! $exprType instanceof UnionType) { return null; diff --git a/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php b/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php index 244b9cd26e8..7fd1b042ad8 100644 --- a/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php +++ b/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php @@ -24,7 +24,7 @@ use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory; use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\StaticTypeMapper\StaticTypeMapper; use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedGenericObjectType; use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType; @@ -37,7 +37,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddReturnTypeDeclarationFromYieldsRector\AddReturnTypeDeclarationFromYieldsRectorTest */ -final class AddReturnTypeDeclarationFromYieldsRector extends AbstractScopeAwareRector implements MinPhpVersionInterface +final class AddReturnTypeDeclarationFromYieldsRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly TypeFactory $typeFactory, @@ -88,8 +88,9 @@ public function getNodeTypes(): array /** * @param Function_|ClassMethod $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); $yieldNodes = $this->findCurrentScopeYieldNodes($node); if ($yieldNodes === []) { return null; diff --git a/rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php b/rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php index e2f1b9c7aee..8715a45ee52 100644 --- a/rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php +++ b/rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php @@ -6,13 +6,12 @@ use PhpParser\Node; use PhpParser\Node\Stmt\Property; -use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use PHPStan\Type\StringType; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\Exception\ShouldNotHappenException; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\StaticTypeMapper\StaticTypeMapper; use Rector\TypeDeclaration\ValueObject\AddPropertyTypeDeclaration; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; @@ -22,7 +21,7 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\Property\AddPropertyTypeDeclarationRector\AddPropertyTypeDeclarationRectorTest */ -final class AddPropertyTypeDeclarationRector extends AbstractScopeAwareRector implements ConfigurableRectorInterface +final class AddPropertyTypeDeclarationRector extends AbstractRector implements ConfigurableRectorInterface { /** * @var AddPropertyTypeDeclaration[] @@ -69,8 +68,9 @@ public function getNodeTypes(): array /** * @param Property $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); // type is already known if ($node->type !== null) { return null; diff --git a/rules/Visibility/Rector/ClassMethod/ChangeMethodVisibilityRector.php b/rules/Visibility/Rector/ClassMethod/ChangeMethodVisibilityRector.php index 756c43715dd..d77a3e25225 100644 --- a/rules/Visibility/Rector/ClassMethod/ChangeMethodVisibilityRector.php +++ b/rules/Visibility/Rector/ClassMethod/ChangeMethodVisibilityRector.php @@ -6,11 +6,10 @@ use PhpParser\Node; use PhpParser\Node\Stmt\ClassMethod; -use PHPStan\Analyser\Scope; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver; use Rector\Privatization\NodeManipulator\VisibilityManipulator; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Rector\ValueObject\Visibility; use Rector\Visibility\ValueObject\ChangeMethodVisibility; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; @@ -20,7 +19,7 @@ /** * @see \Rector\Tests\Visibility\Rector\ClassMethod\ChangeMethodVisibilityRector\ChangeMethodVisibilityRectorTest */ -final class ChangeMethodVisibilityRector extends AbstractScopeAwareRector implements ConfigurableRectorInterface +final class ChangeMethodVisibilityRector extends AbstractRector implements ConfigurableRectorInterface { /** * @var ChangeMethodVisibility[] @@ -88,8 +87,9 @@ public function getNodeTypes(): array /** * @param ClassMethod $node */ - public function refactorWithScope(Node $node, Scope $scope): ?Node + public function refactor(Node $node): ?Node { + $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); if ($this->methodVisibilities === []) { return null; } diff --git a/src/Contract/Rector/ScopeAwareRectorInterface.php b/src/Contract/Rector/ScopeAwareRectorInterface.php index 73744d0a3ab..ceb76c25d31 100644 --- a/src/Contract/Rector/ScopeAwareRectorInterface.php +++ b/src/Contract/Rector/ScopeAwareRectorInterface.php @@ -8,6 +8,10 @@ use PhpParser\NodeTraverser; use PHPStan\Analyser\Scope; +/** + * @deprecated This class is deprecated, as too granular. + * Use \Rector\Rector\AbstractRector instead with help of \Rector\PHPStan\ScopeFetcher + */ interface ScopeAwareRectorInterface extends RectorInterface { /** diff --git a/src/PHPStan/ScopeFetcher.php b/src/PHPStan/ScopeFetcher.php new file mode 100644 index 00000000000..fccb890b3e8 --- /dev/null +++ b/src/PHPStan/ScopeFetcher.php @@ -0,0 +1,30 @@ +getAttribute(AttributeKey::SCOPE); + + if (! $currentScope instanceof Scope) { + $errorMessage = sprintf( + 'Scope not available on "%s" node. Fix scope refresh on changed nodes first', + $node::class, + ); + + throw new ShouldNotHappenException($errorMessage); + } + + return $currentScope; + } +} diff --git a/src/Rector/AbstractScopeAwareRector.php b/src/Rector/AbstractScopeAwareRector.php index 3c3dfa81238..f9798c178d6 100644 --- a/src/Rector/AbstractScopeAwareRector.php +++ b/src/Rector/AbstractScopeAwareRector.php @@ -12,6 +12,10 @@ use Rector\Exception\ShouldNotHappenException; use Rector\NodeTypeResolver\Node\AttributeKey; +/** + * @deprecated This class is deprecated, as too granular. + * Use \Rector\Rector\AbstractRector instead with help of \Rector\PHPStan\ScopeFetcher + */ abstract class AbstractScopeAwareRector extends AbstractRector implements ScopeAwareRectorInterface { /** diff --git a/tests/Issues/FqcnAnnotationToAttribute/config/configured_rule.php b/tests/Issues/FqcnAnnotationToAttribute/config/configured_rule.php index 89c51c785e6..9fa47f3eea3 100644 --- a/tests/Issues/FqcnAnnotationToAttribute/config/configured_rule.php +++ b/tests/Issues/FqcnAnnotationToAttribute/config/configured_rule.php @@ -3,9 +3,6 @@ declare(strict_types=1); use Rector\Config\RectorConfig; -use Rector\Doctrine\Set\DoctrineSetList; -use Rector\Symfony\Set\SymfonySetList; -return static function (RectorConfig $rectorConfig): void { - $rectorConfig->sets([SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES, DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES]); -}; +return RectorConfig::configure() + ->withAttributesSets(symfony: true, doctrine: true); diff --git a/tests/Issues/PrintMatchPhpstan/config/configured_rule.php b/tests/Issues/PrintMatchPhpstan/config/configured_rule.php index e132df5672e..f48c064b5d4 100644 --- a/tests/Issues/PrintMatchPhpstan/config/configured_rule.php +++ b/tests/Issues/PrintMatchPhpstan/config/configured_rule.php @@ -3,8 +3,6 @@ declare(strict_types=1); use Rector\Config\RectorConfig; -use Rector\Set\ValueObject\DowngradeLevelSetList; -return static function (RectorConfig $rectorConfig): void { - $rectorConfig->import(DowngradeLevelSetList::DOWN_TO_PHP_81); -}; +return RectorConfig::configure() + ->withDowngradeSets(php81: true); diff --git a/tests/Issues/ScopeNotAvailable/Variable/ArrayItemForeachValueRector.php b/tests/Issues/ScopeNotAvailable/Variable/ArrayItemForeachValueRector.php index 9080113e3b6..2b0bb0a9a87 100644 --- a/tests/Issues/ScopeNotAvailable/Variable/ArrayItemForeachValueRector.php +++ b/tests/Issues/ScopeNotAvailable/Variable/ArrayItemForeachValueRector.php @@ -7,12 +7,11 @@ use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Variable; -use PHPStan\Analyser\Scope; -use Rector\Rector\AbstractScopeAwareRector; +use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; -final class ArrayItemForeachValueRector extends AbstractScopeAwareRector +final class ArrayItemForeachValueRector extends AbstractRector { public function getRuleDefinition(): RuleDefinition { @@ -27,7 +26,7 @@ public function getNodeTypes(): array return [Variable::class]; } - public function refactorWithScope(Node $node, Scope $scope): Node + public function refactor(Node $node): Node { return $node; } From e30c7afeeb261468b993a6fcb1bd01fad8b0323e Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 12 Nov 2024 22:52:57 +0100 Subject: [PATCH 2/2] remove places, where scope is actually never used --- .../Fixture/variable_reference.php | 17 ----------------- .../SimplifyEmptyCheckOnEmptyArrayRector.php | 3 ++- .../TernaryFalseExpressionToIfRector.php | 1 - .../Rector/Assign/RemoveDoubleAssignRector.php | 3 ++- .../RemoveUnusedPrivateMethodRector.php | 3 ++- .../RemoveUnusedPromotedPropertyRector.php | 2 -- .../RemoveUnusedPrivatePropertyRector.php | 1 - .../GetCalledClassToStaticClassRector.php | 3 ++- ...ThisCallOnStaticMethodToStaticCallRector.php | 3 ++- ...taticCallOnNonStaticToInstanceCallRector.php | 3 ++- .../Rector/FuncCall/GetClassOnNullRector.php | 1 - .../ClassOnThisVariableObjectRector.php | 1 - .../Rector/Property/ReadOnlyPropertyRector.php | 3 ++- .../Php82/Rector/Class_/ReadOnlyClassRector.php | 3 ++- .../PrivatizeFinalClassMethodRector.php | 3 ++- .../Rector/MethodCall/RenameMethodRector.php | 3 ++- .../Rector/Class_/AddInterfaceByTraitRector.php | 3 ++- .../StaticCall/StaticCallToMethodCallRector.php | 1 - ...lReturnTypeFromBooleanConstReturnsRector.php | 3 ++- ...ReturnTypeFromBooleanStrictReturnsRector.php | 3 ++- ...NumericReturnTypeFromStrictReturnsRector.php | 3 ++- ...cReturnTypeFromStrictScalarReturnsRector.php | 3 ++- .../ParamTypeByMethodCallTypeRector.php | 3 ++- .../ParamTypeByParentCallTypeRector.php | 3 ++- .../ClassMethod/ReturnNeverTypeRector.php | 3 ++- .../ClassMethod/ReturnNullableTypeRector.php | 3 ++- .../ReturnTypeFromMockObjectRector.php | 3 ++- .../ReturnTypeFromReturnCastRector.php | 3 ++- .../ReturnTypeFromReturnDirectArrayRector.php | 3 ++- .../ReturnTypeFromReturnNewRector.php | 3 ++- ...ReturnTypeFromStrictConstantReturnRector.php | 3 ++- .../ReturnTypeFromStrictFluentReturnRector.php | 3 ++- .../ReturnTypeFromStrictNativeCallRector.php | 3 ++- .../ReturnTypeFromStrictNewArrayRector.php | 3 ++- .../ReturnTypeFromStrictParamRector.php | 3 ++- .../ReturnTypeFromStrictTypedCallRector.php | 3 ++- .../ReturnTypeFromStrictTypedPropertyRector.php | 3 ++- .../ReturnTypeFromSymfonySerializerRector.php | 3 ++- .../ClassMethod/ReturnUnionTypeRector.php | 3 ++- ...gReturnTypeFromStrictScalarReturnsRector.php | 3 ++- ...gReturnTypeFromStrictStringReturnsRector.php | 3 ++- .../ReturnTypeFromStrictTernaryRector.php | 3 ++- .../Closure/AddClosureNeverReturnTypeRector.php | 3 ++- ...AddReturnTypeDeclarationFromYieldsRector.php | 4 ++-- .../AddPropertyTypeDeclarationRector.php | 3 ++- .../ChangeMethodVisibilityRector.php | 3 ++- src/PHPStan/ScopeFetcher.php | 3 ++- 47 files changed, 80 insertions(+), 65 deletions(-) delete mode 100644 rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/variable_reference.php diff --git a/rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/variable_reference.php b/rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/variable_reference.php deleted file mode 100644 index d100be2d6a0..00000000000 --- a/rules-tests/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture/variable_reference.php +++ /dev/null @@ -1,17 +0,0 @@ - diff --git a/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php b/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php index 0876458ae70..efb58b3dfd7 100644 --- a/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php +++ b/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php @@ -23,6 +23,7 @@ use Rector\NodeAnalyzer\ExprAnalyzer; use Rector\Php\ReservedKeywordAnalyzer; use Rector\PhpParser\AstResolver; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\AllAssignNodePropertyTypeInferer; @@ -78,7 +79,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); if ($node instanceof BooleanNot) { if ($node->expr instanceof Empty_ && $this->isAllowedExpr($node->expr->expr, $scope)) { return new NotIdentical($node->expr->expr, new Array_()); diff --git a/rules/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector.php b/rules/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector.php index ce6790717f1..c2f27a6f74c 100644 --- a/rules/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector.php +++ b/rules/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector.php @@ -68,7 +68,6 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); if (! $node->expr instanceof Ternary) { return null; } diff --git a/rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php b/rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php index d51b1cdd358..282a7e00e7c 100644 --- a/rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php +++ b/rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php @@ -19,6 +19,7 @@ use Rector\DeadCode\SideEffect\SideEffectNodeDetector; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -69,7 +70,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); $stmts = $node->stmts; if ($stmts === null) { return null; diff --git a/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php b/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php index 6d80982aab9..d070978b510 100644 --- a/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php +++ b/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php @@ -13,6 +13,7 @@ use Rector\DeadCode\NodeAnalyzer\IsClassMethodUsedAnalyzer; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpParser\Node\BetterNodeFinder; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; use Rector\ValueObject\MethodName; @@ -76,7 +77,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); $classMethods = $node->getMethods(); if ($classMethods === []) { diff --git a/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector.php b/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector.php index 841f59eb6c6..9ac7d4bf2ef 100644 --- a/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector.php +++ b/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector.php @@ -9,7 +9,6 @@ use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\TraitUse; -use PHPStan\Analyser\Scope; use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode; use PHPStan\Reflection\ClassReflection; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; @@ -98,7 +97,6 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); $constructClassMethod = $node->getMethod(MethodName::CONSTRUCT); if (! $constructClassMethod instanceof ClassMethod) { return null; diff --git a/rules/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector.php b/rules/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector.php index 6f189de50b5..20cdc527bda 100644 --- a/rules/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector.php +++ b/rules/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector.php @@ -66,7 +66,6 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); if ($this->shouldSkipClass($node)) { return null; } diff --git a/rules/Php55/Rector/FuncCall/GetCalledClassToStaticClassRector.php b/rules/Php55/Rector/FuncCall/GetCalledClassToStaticClassRector.php index 8eb08e7370b..2b3df53c4d0 100644 --- a/rules/Php55/Rector/FuncCall/GetCalledClassToStaticClassRector.php +++ b/rules/Php55/Rector/FuncCall/GetCalledClassToStaticClassRector.php @@ -7,6 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Expr\FuncCall; use Rector\Enum\ObjectReference; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; @@ -58,7 +59,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); if (! $this->isName($node, 'get_called_class')) { return null; } diff --git a/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php b/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php index ae4c46b20a2..c9fc03e55e5 100644 --- a/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php +++ b/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php @@ -16,6 +16,7 @@ use PHPStan\Reflection\Php\PhpMethodReflection; use Rector\Enum\ObjectReference; use Rector\NodeCollector\StaticAnalyzer; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; use Rector\ValueObject\PhpVersionFeature; @@ -92,7 +93,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); if (! $scope->isInClass()) { return null; } diff --git a/rules/Php70/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php b/rules/Php70/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php index f23e336510b..9658288999e 100644 --- a/rules/Php70/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php +++ b/rules/Php70/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php @@ -19,6 +19,7 @@ use Rector\Enum\ObjectReference; use Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver; use Rector\NodeCollector\StaticAnalyzer; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; use Rector\ValueObject\PhpVersionFeature; @@ -102,7 +103,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); if ($node->name instanceof Expr) { return null; } diff --git a/rules/Php72/Rector/FuncCall/GetClassOnNullRector.php b/rules/Php72/Rector/FuncCall/GetClassOnNullRector.php index 7519333ec28..3b7afd83ecf 100644 --- a/rules/Php72/Rector/FuncCall/GetClassOnNullRector.php +++ b/rules/Php72/Rector/FuncCall/GetClassOnNullRector.php @@ -70,7 +70,6 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); $hasChanged = false; $this->traverseNodesWithCallable($node, function (Node $node) use (&$hasChanged): int|null|Ternary { diff --git a/rules/Php80/Rector/ClassConstFetch/ClassOnThisVariableObjectRector.php b/rules/Php80/Rector/ClassConstFetch/ClassOnThisVariableObjectRector.php index 08a19284bcd..46483a99e8a 100644 --- a/rules/Php80/Rector/ClassConstFetch/ClassOnThisVariableObjectRector.php +++ b/rules/Php80/Rector/ClassConstFetch/ClassOnThisVariableObjectRector.php @@ -67,7 +67,6 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); $className = $node->isFinal() ? 'self' : 'static'; $hasChanged = false; diff --git a/rules/Php81/Rector/Property/ReadOnlyPropertyRector.php b/rules/Php81/Rector/Property/ReadOnlyPropertyRector.php index 11afa8212ed..7e75cbc9743 100644 --- a/rules/Php81/Rector/Property/ReadOnlyPropertyRector.php +++ b/rules/Php81/Rector/Property/ReadOnlyPropertyRector.php @@ -25,6 +25,7 @@ use Rector\NodeManipulator\PropertyManipulator; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpParser\Node\BetterNodeFinder; +use Rector\PHPStan\ScopeFetcher; use Rector\Privatization\NodeManipulator\VisibilityManipulator; use Rector\Rector\AbstractRector; use Rector\ValueObject\MethodName; @@ -101,7 +102,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); if ($this->shouldSkip($node)) { return null; } diff --git a/rules/Php82/Rector/Class_/ReadOnlyClassRector.php b/rules/Php82/Rector/Class_/ReadOnlyClassRector.php index 964c0805e34..127d8927f5f 100644 --- a/rules/Php82/Rector/Class_/ReadOnlyClassRector.php +++ b/rules/Php82/Rector/Class_/ReadOnlyClassRector.php @@ -18,6 +18,7 @@ use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer; use Rector\Php81\Enum\AttributeName; +use Rector\PHPStan\ScopeFetcher; use Rector\Privatization\NodeManipulator\VisibilityManipulator; use Rector\Rector\AbstractRector; use Rector\ValueObject\MethodName; @@ -81,7 +82,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); if ($this->shouldSkip($node, $scope)) { return null; } diff --git a/rules/Privatization/Rector/ClassMethod/PrivatizeFinalClassMethodRector.php b/rules/Privatization/Rector/ClassMethod/PrivatizeFinalClassMethodRector.php index f58be7f23f5..4fb5c6bc28e 100644 --- a/rules/Privatization/Rector/ClassMethod/PrivatizeFinalClassMethodRector.php +++ b/rules/Privatization/Rector/ClassMethod/PrivatizeFinalClassMethodRector.php @@ -10,6 +10,7 @@ use PhpParser\Node\Stmt\ClassMethod; use PHPStan\Reflection\ClassReflection; use Rector\PhpParser\Node\BetterNodeFinder; +use Rector\PHPStan\ScopeFetcher; use Rector\Privatization\Guard\OverrideByParentClassGuard; use Rector\Privatization\NodeManipulator\VisibilityManipulator; use Rector\Privatization\VisibilityGuard\ClassMethodVisibilityGuard; @@ -71,7 +72,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); if (! $node->isFinal()) { return null; } diff --git a/rules/Renaming/Rector/MethodCall/RenameMethodRector.php b/rules/Renaming/Rector/MethodCall/RenameMethodRector.php index ce8d6094a31..b0e8942263f 100644 --- a/rules/Renaming/Rector/MethodCall/RenameMethodRector.php +++ b/rules/Renaming/Rector/MethodCall/RenameMethodRector.php @@ -19,6 +19,7 @@ use PHPStan\Reflection\ReflectionProvider; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\NodeManipulator\ClassManipulator; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; use Rector\Renaming\Contract\MethodCallRenameInterface; @@ -77,7 +78,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); if ($node instanceof Class_ || $node instanceof Interface_) { return $this->refactorClass($node, $scope); } diff --git a/rules/Transform/Rector/Class_/AddInterfaceByTraitRector.php b/rules/Transform/Rector/Class_/AddInterfaceByTraitRector.php index 55280c8441a..e44a0c60a85 100644 --- a/rules/Transform/Rector/Class_/AddInterfaceByTraitRector.php +++ b/rules/Transform/Rector/Class_/AddInterfaceByTraitRector.php @@ -9,6 +9,7 @@ use PhpParser\Node\Stmt\Class_; use PHPStan\Reflection\ClassReflection; use Rector\Contract\Rector\ConfigurableRectorInterface; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -63,7 +64,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); $classReflection = $scope->getClassReflection(); if (! $classReflection instanceof ClassReflection) { return null; diff --git a/rules/Transform/Rector/StaticCall/StaticCallToMethodCallRector.php b/rules/Transform/Rector/StaticCall/StaticCallToMethodCallRector.php index 40eb7d85ae1..5ab93315a32 100644 --- a/rules/Transform/Rector/StaticCall/StaticCallToMethodCallRector.php +++ b/rules/Transform/Rector/StaticCall/StaticCallToMethodCallRector.php @@ -97,7 +97,6 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); $class = $node; $hasChanged = false; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanConstReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanConstReturnsRector.php index 35aa5ad5eb8..f0ded98f930 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanConstReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanConstReturnsRector.php @@ -14,6 +14,7 @@ use PHPStan\Analyser\Scope; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\PhpParser\Node\Value\ValueResolver; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer; use Rector\ValueObject\PhpVersionFeature; @@ -84,7 +85,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); if ($this->shouldSkip($node, $scope)) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector.php index 9f3e1821a31..9888506775c 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector.php @@ -31,6 +31,7 @@ use PHPStan\Reflection\ReflectionProvider; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\PhpParser\Node\Value\ValueResolver; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer; use Rector\ValueObject\PhpVersionFeature; @@ -94,7 +95,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); if ($this->shouldSkip($node, $scope)) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictReturnsRector.php index 46410023d97..e8f76a2a267 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictReturnsRector.php @@ -15,6 +15,7 @@ use PhpParser\Node\Stmt\Return_; use PHPStan\Analyser\Scope; use Rector\PhpParser\Node\BetterNodeFinder; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer; use Rector\ValueObject\PhpVersionFeature; @@ -76,7 +77,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); if ($this->shouldSkip($node, $scope)) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictScalarReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictScalarReturnsRector.php index ac66f243251..b5a5c2efaaf 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictScalarReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictScalarReturnsRector.php @@ -13,6 +13,7 @@ use PhpParser\Node\Stmt\Function_; use PHPStan\Analyser\Scope; use Rector\PhpParser\Node\BetterNodeFinder; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer; use Rector\ValueObject\PhpVersionFeature; @@ -74,7 +75,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); if ($this->shouldSkip($node, $scope)) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php index 836c5114454..141b56d0560 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php @@ -14,6 +14,7 @@ use PHPStan\Analyser\Scope; use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory; use Rector\PhpParser\Node\BetterNodeFinder; +use Rector\PHPStan\ScopeFetcher; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; use Rector\Rector\AbstractRector; use Rector\StaticTypeMapper\Mapper\PhpParserNodeMapper; @@ -104,7 +105,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); $hasChanged = false; foreach ($node->getMethods() as $classMethod) { diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php index 6b264314740..bb5a765be93 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php @@ -12,6 +12,7 @@ use Rector\Enum\ObjectReference; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpParser\Node\BetterNodeFinder; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; use Rector\TypeDeclaration\NodeAnalyzer\CallerParamMatcher; @@ -85,7 +86,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); if ($this->shouldSkip($node)) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php index 6d132b0d88c..5208c7e5fd3 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php @@ -7,6 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeManipulator\AddNeverReturnType; use Rector\ValueObject\PhpVersionFeature; @@ -65,7 +66,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); return $this->addNeverReturnType->add($node, $scope); } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnNullableTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnNullableTypeRector.php index 6f8312b0d9e..580032d088b 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnNullableTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnNullableTypeRector.php @@ -9,6 +9,7 @@ use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PHPStan\Type\UnionType; +use Rector\PHPStan\ScopeFetcher; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; use Rector\PHPStanStaticTypeMapper\TypeMapper\UnionTypeMapper; use Rector\Rector\AbstractRector; @@ -84,7 +85,7 @@ public function provideMinPhpVersion(): int */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); // empty body, nothing to resolve if ($node->stmts === null || $node->stmts === []) { return null; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromMockObjectRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromMockObjectRector.php index 2f439ad347a..47323dbd285 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromMockObjectRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromMockObjectRector.php @@ -15,6 +15,7 @@ use PHPStan\Type\Type; use Rector\Enum\ClassName; use Rector\PhpParser\Node\BetterNodeFinder; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer; use Rector\ValueObject\PhpVersionFeature; @@ -80,7 +81,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); // type is already known if ($node->returnType instanceof Node) { return null; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnCastRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnCastRector.php index e242daa989f..ef38170132d 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnCastRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnCastRector.php @@ -7,6 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeManipulator\AddReturnTypeFromCast; use Rector\ValueObject\PhpVersionFeature; @@ -74,7 +75,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); return $this->addReturnTypeFromCast->add($node, $scope); } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector.php index 21bfce9582d..bfd15cd8eab 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector.php @@ -10,6 +10,7 @@ use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Return_; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer; use Rector\ValueObject\PhpVersionFeature; @@ -70,7 +71,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); // already has return type, skip if ($node->returnType instanceof Node) { return null; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php index c137aa58f15..5637a34c4e6 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php @@ -23,6 +23,7 @@ use Rector\NodeTypeResolver\NodeTypeResolver\NewTypeResolver; use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory; use Rector\PhpParser\Node\BetterNodeFinder; +use Rector\PHPStan\ScopeFetcher; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; @@ -98,7 +99,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); // already filled if ($node->returnType instanceof Node) { return null; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictConstantReturnRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictConstantReturnRector.php index 6d357bfa1b2..a1aadb061b0 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictConstantReturnRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictConstantReturnRector.php @@ -12,6 +12,7 @@ use PHPStan\Type\Type; use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory; use Rector\PhpParser\Node\BetterNodeFinder; +use Rector\PHPStan\ScopeFetcher; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; use Rector\Rector\AbstractRector; use Rector\StaticTypeMapper\StaticTypeMapper; @@ -80,7 +81,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); if ($node->returnType instanceof Node) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictFluentReturnRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictFluentReturnRector.php index 60cf5337bbf..7c7a4d8e4b7 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictFluentReturnRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictFluentReturnRector.php @@ -12,6 +12,7 @@ use PHPStan\Type\StaticType; use PHPStan\Type\ThisType; use Rector\Php\PhpVersionProvider; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer; @@ -79,7 +80,7 @@ public function provideMinPhpVersion(): int */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); // already typed → skip if ($node->returnType instanceof Node) { return null; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNativeCallRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNativeCallRector.php index 15c8a30a2c9..dc110215b7d 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNativeCallRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNativeCallRector.php @@ -7,6 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeManipulator\AddReturnTypeFromStrictNativeCall; use Rector\ValueObject\PhpVersion; @@ -65,7 +66,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); return $this->addReturnTypeFromStrictNativeCall->add($node, $scope); } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php index 9ea51991478..760d5369a4c 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php @@ -24,6 +24,7 @@ use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger; use Rector\PhpParser\Node\BetterNodeFinder; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer; use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer; @@ -93,7 +94,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); if ($this->shouldSkip($node, $scope)) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictParamRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictParamRector.php index 381fb348650..4b253c834d3 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictParamRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictParamRector.php @@ -7,6 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeManipulator\AddReturnTypeFromParam; use Rector\ValueObject\PhpVersionFeature; @@ -70,7 +71,7 @@ public function provideMinPhpVersion(): int */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); return $this->addReturnTypeFromParam->add($node, $scope); } } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php index ad505976afd..5d0c483d83f 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php @@ -20,6 +20,7 @@ use PHPStan\Type\UnionType; use Rector\Php\PhpVersionProvider; use Rector\PhpParser\Node\BetterNodeFinder; +use Rector\PHPStan\ScopeFetcher; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; use Rector\Rector\AbstractRector; use Rector\StaticTypeMapper\StaticTypeMapper; @@ -105,7 +106,7 @@ public function provideMinPhpVersion(): int */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); // already filled → skip if ($node->returnType instanceof Node) { return null; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php index 4db4b695c76..2bb9d8b951c 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php @@ -14,6 +14,7 @@ use PHPStan\Type\Type; use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory; use Rector\PhpParser\Node\BetterNodeFinder; +use Rector\PHPStan\ScopeFetcher; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; @@ -85,7 +86,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); if ($node->returnType instanceof Node) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php index 7aa25b48ec9..5859a060f34 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php @@ -12,6 +12,7 @@ use PHPStan\Type\ObjectType; use Rector\NodeAnalyzer\ArgsAnalyzer; use Rector\PhpParser\Node\Value\ValueResolver; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard; @@ -80,7 +81,7 @@ public function provideMinPhpVersion(): int */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); if ($node->stmts === null) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector.php index 3e1e8a9c742..0e560c7ae00 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector.php @@ -7,6 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeManipulator\AddUnionReturnType; use Rector\ValueObject\PhpVersionFeature; @@ -85,7 +86,7 @@ public function provideMinPhpVersion(): int */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); return $this->addUnionReturnType->add($node, $scope); } } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/StringReturnTypeFromStrictScalarReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/StringReturnTypeFromStrictScalarReturnsRector.php index ed7be17f130..33fcc53cd3a 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/StringReturnTypeFromStrictScalarReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/StringReturnTypeFromStrictScalarReturnsRector.php @@ -12,6 +12,7 @@ use PhpParser\Node\Stmt\Function_; use PHPStan\Analyser\Scope; use Rector\PhpParser\Node\BetterNodeFinder; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer; use Rector\ValueObject\PhpVersion; @@ -81,7 +82,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); // already added → skip if ($node->returnType instanceof Node) { return null; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/StringReturnTypeFromStrictStringReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/StringReturnTypeFromStrictStringReturnsRector.php index c4b3e27d35d..d467d0a8f7d 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/StringReturnTypeFromStrictStringReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/StringReturnTypeFromStrictStringReturnsRector.php @@ -14,6 +14,7 @@ use PhpParser\Node\Stmt\Return_; use PHPStan\Analyser\Scope; use Rector\PhpParser\Node\BetterNodeFinder; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer; use Rector\ValueObject\PhpVersion; @@ -83,7 +84,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); // already added → skip if ($node->returnType instanceof Node) { return null; diff --git a/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php b/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php index ecdbe62ea41..86d6c9ddb50 100644 --- a/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php +++ b/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php @@ -14,6 +14,7 @@ use PHPStan\Type\UnionType; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpParser\Node\BetterNodeFinder; +use Rector\PHPStan\ScopeFetcher; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; use Rector\Rector\AbstractRector; use Rector\StaticTypeMapper\StaticTypeMapper; @@ -80,7 +81,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); if ($this->shouldSkip($node, $scope)) { return null; } diff --git a/rules/TypeDeclaration/Rector/Closure/AddClosureNeverReturnTypeRector.php b/rules/TypeDeclaration/Rector/Closure/AddClosureNeverReturnTypeRector.php index ed8e7ef9840..6d93f496932 100644 --- a/rules/TypeDeclaration/Rector/Closure/AddClosureNeverReturnTypeRector.php +++ b/rules/TypeDeclaration/Rector/Closure/AddClosureNeverReturnTypeRector.php @@ -6,6 +6,7 @@ use PhpParser\Node; use PhpParser\Node\Expr\Closure; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeManipulator\AddNeverReturnType; use Rector\ValueObject\PhpVersionFeature; @@ -56,7 +57,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); return $this->addNeverReturnType->add($node, $scope); } diff --git a/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php b/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php index 7fd1b042ad8..fdb1a6c0aee 100644 --- a/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php +++ b/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php @@ -18,11 +18,11 @@ use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Function_; use PhpParser\NodeTraverser; -use PHPStan\Analyser\Scope; use PHPStan\Type\MixedType; use PHPStan\Type\Type; use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory; use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser; +use Rector\PHPStan\ScopeFetcher; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; use Rector\Rector\AbstractRector; use Rector\StaticTypeMapper\StaticTypeMapper; @@ -90,7 +90,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); $yieldNodes = $this->findCurrentScopeYieldNodes($node); if ($yieldNodes === []) { return null; diff --git a/rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php b/rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php index 8715a45ee52..6906c5df198 100644 --- a/rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php +++ b/rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php @@ -10,6 +10,7 @@ use PHPStan\Type\StringType; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\Exception\ShouldNotHappenException; +use Rector\PHPStan\ScopeFetcher; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; use Rector\Rector\AbstractRector; use Rector\StaticTypeMapper\StaticTypeMapper; @@ -70,7 +71,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); // type is already known if ($node->type !== null) { return null; diff --git a/rules/Visibility/Rector/ClassMethod/ChangeMethodVisibilityRector.php b/rules/Visibility/Rector/ClassMethod/ChangeMethodVisibilityRector.php index d77a3e25225..6a08f0fdcc0 100644 --- a/rules/Visibility/Rector/ClassMethod/ChangeMethodVisibilityRector.php +++ b/rules/Visibility/Rector/ClassMethod/ChangeMethodVisibilityRector.php @@ -8,6 +8,7 @@ use PhpParser\Node\Stmt\ClassMethod; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver; +use Rector\PHPStan\ScopeFetcher; use Rector\Privatization\NodeManipulator\VisibilityManipulator; use Rector\Rector\AbstractRector; use Rector\ValueObject\Visibility; @@ -89,7 +90,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = \Rector\PHPStan\ScopeFetcher::fetch($node); + $scope = ScopeFetcher::fetch($node); if ($this->methodVisibilities === []) { return null; } diff --git a/src/PHPStan/ScopeFetcher.php b/src/PHPStan/ScopeFetcher.php index fccb890b3e8..1c1338f0353 100644 --- a/src/PHPStan/ScopeFetcher.php +++ b/src/PHPStan/ScopeFetcher.php @@ -4,6 +4,7 @@ namespace Rector\PHPStan; +use PhpParser\Node; use PHPStan\Analyser\MutatingScope; use PHPStan\Analyser\Scope; use Rector\Exception\ShouldNotHappenException; @@ -11,7 +12,7 @@ final class ScopeFetcher { - public static function fetch(\PhpParser\Node $node): Scope + public static function fetch(Node $node): Scope { /** @var MutatingScope|null $currentScope */ $currentScope = $node->getAttribute(AttributeKey::SCOPE);