diff --git a/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector.php index f9d6e1ec866..4e593fd945d 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector.php @@ -88,13 +88,13 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = ScopeFetcher::fetch($node); - if ($this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope)) { + // already known type + if ($node->returnType instanceof Node) { return null; } - // already known type - if ($node->returnType instanceof Node) { + $scope = ScopeFetcher::fetch($node); + if ($this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope)) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanConstAndStrictReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanConstAndStrictReturnsRector.php index 0aa0bcf9fa6..a0d921516f2 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanConstAndStrictReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanConstAndStrictReturnsRector.php @@ -91,6 +91,11 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + // already has the type, skip + if ($node->returnType instanceof Node) { + return null; + } + $scope = ScopeFetcher::fetch($node); if ($this->shouldSkip($node, $scope)) { return null; @@ -120,11 +125,6 @@ public function provideMinPhpVersion(): int */ private function shouldSkip(Node $node, Scope $scope): bool { - // already has the type, skip - if ($node->returnType instanceof Node) { - return true; - } - return $node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope); } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanConstReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanConstReturnsRector.php index f0ded98f930..8cb4e8c799b 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanConstReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanConstReturnsRector.php @@ -85,6 +85,11 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + // already has the type, skip + if ($node->returnType instanceof Node) { + return null; + } + $scope = ScopeFetcher::fetch($node); if ($this->shouldSkip($node, $scope)) { return null; @@ -114,11 +119,6 @@ public function provideMinPhpVersion(): int */ private function shouldSkip(Node $node, Scope $scope): bool { - // already has the type, skip - if ($node->returnType instanceof Node) { - return true; - } - return $node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope); } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector.php index e81297e81d5..b56c6727e67 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector.php @@ -85,6 +85,11 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + // already has the type, skip + if ($node->returnType instanceof Node) { + return null; + } + $scope = ScopeFetcher::fetch($node); if ($this->shouldSkip($node, $scope)) { return null; @@ -120,11 +125,6 @@ public function provideMinPhpVersion(): int */ private function shouldSkip(Node $node, Scope $scope): bool { - // already has the type, skip - if ($node->returnType instanceof Node) { - return true; - } - return $node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope); } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictReturnsRector.php index 89da603b8d0..f181fe07e9a 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictReturnsRector.php @@ -77,6 +77,16 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + // type is already known, skip + if ($node->returnType instanceof Node) { + return null; + } + + // empty, nothing to find + if ($node->stmts === null || $node->stmts === []) { + return null; + } + $scope = ScopeFetcher::fetch($node); if ($this->shouldSkip($node, $scope)) { return null; @@ -130,16 +140,6 @@ public function provideMinPhpVersion(): int private function shouldSkip(ClassMethod|Function_ $functionLike, Scope $scope): bool { - // type is already known, skip - if ($functionLike->returnType instanceof Node) { - return true; - } - - // empty, nothing to find - if ($functionLike->stmts === null || $functionLike->stmts === []) { - return true; - } - if (! $functionLike instanceof ClassMethod) { return false; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictScalarReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictScalarReturnsRector.php index af0bc81aecc..3af4bc50af3 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictScalarReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictScalarReturnsRector.php @@ -75,6 +75,16 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + // type is already known, skip + if ($node->returnType instanceof Node) { + return null; + } + + // empty, nothing to find + if ($node->stmts === null || $node->stmts === []) { + return null; + } + $scope = ScopeFetcher::fetch($node); if ($this->shouldSkip($node, $scope)) { return null; @@ -123,16 +133,6 @@ public function provideMinPhpVersion(): int private function shouldSkip(ClassMethod|Function_ $functionLike, Scope $scope): bool { - // type is already known, skip - if ($functionLike->returnType instanceof Node) { - return true; - } - - // empty, nothing to find - if ($functionLike->stmts === null || $functionLike->stmts === []) { - return true; - } - if (! $functionLike instanceof ClassMethod) { return false; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnNullableTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnNullableTypeRector.php index 580032d088b..b9440a7cd18 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnNullableTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnNullableTypeRector.php @@ -85,7 +85,6 @@ public function provideMinPhpVersion(): int */ public function refactor(Node $node): ?Node { - $scope = ScopeFetcher::fetch($node); // empty body, nothing to resolve if ($node->stmts === null || $node->stmts === []) { return null; @@ -96,6 +95,7 @@ public function refactor(Node $node): ?Node return null; } + $scope = ScopeFetcher::fetch($node); if ($node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod( $node, $scope diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromMockObjectRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromMockObjectRector.php index 5bcfa0f3f0d..4b7b5b312fa 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromMockObjectRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromMockObjectRector.php @@ -76,12 +76,12 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = ScopeFetcher::fetch($node); // type is already known if ($node->returnType instanceof Node) { return null; } + $scope = ScopeFetcher::fetch($node); if (! $this->isInsideTestCaseClass($scope)) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector.php index bfd15cd8eab..24a924bcac2 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector.php @@ -71,12 +71,12 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = ScopeFetcher::fetch($node); // already has return type, skip if ($node->returnType instanceof Node) { return null; } + $scope = ScopeFetcher::fetch($node); if ($node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod( $node, $scope diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php index 37019622852..48e7e2e7d83 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php @@ -100,12 +100,12 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = ScopeFetcher::fetch($node); // already filled if ($node->returnType instanceof Node) { return null; } + $scope = ScopeFetcher::fetch($node); if ($node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod( $node, $scope diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictConstantReturnRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictConstantReturnRector.php index f4776f5f0de..ec7845e9620 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictConstantReturnRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictConstantReturnRector.php @@ -81,11 +81,11 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = ScopeFetcher::fetch($node); if ($node->returnType instanceof Node) { return null; } + $scope = ScopeFetcher::fetch($node); $returns = $this->betterNodeFinder->findReturnsScoped($node); if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($node, $returns)) { return null; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictFluentReturnRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictFluentReturnRector.php index 7c7a4d8e4b7..590348be0be 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictFluentReturnRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictFluentReturnRector.php @@ -80,12 +80,12 @@ public function provideMinPhpVersion(): int */ public function refactor(Node $node): ?Node { - $scope = ScopeFetcher::fetch($node); // already typed → skip if ($node->returnType instanceof Node) { return null; } + $scope = ScopeFetcher::fetch($node); if ($this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope)) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php index 6da5b7df9d3..1b188b160ea 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php @@ -73,6 +73,15 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + if ($node->returnType instanceof Node) { + return null; + } + + // private methods are handled by PrivateMethodReturnTypeFromStrictNewArrayRector + if ($node instanceof ClassMethod && $node->isPrivate()) { + return null; + } + $scope = ScopeFetcher::fetch($node); if ($this->shouldSkip($node, $scope)) { return null; @@ -88,15 +97,6 @@ public function provideMinPhpVersion(): int private function shouldSkip(ClassMethod|Function_ $node, Scope $scope): bool { - if ($node->returnType instanceof Node) { - return true; - } - - // private methods are handled by PrivateMethodReturnTypeFromStrictNewArrayRector - if ($node instanceof ClassMethod && $node->isPrivate()) { - return true; - } - return $node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod( $node, $scope diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php index 00c5486844b..3fe8b85b8d1 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php @@ -106,7 +106,6 @@ public function provideMinPhpVersion(): int */ public function refactor(Node $node): ?Node { - $scope = ScopeFetcher::fetch($node); // already filled → skip if ($node->returnType instanceof Node) { return null; @@ -116,6 +115,7 @@ public function refactor(Node $node): ?Node return null; } + $scope = ScopeFetcher::fetch($node); if ($this->shouldSkip($node, $scope)) { return null; } diff --git a/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php b/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php index 86d6c9ddb50..f3eb9ae816f 100644 --- a/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php +++ b/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php @@ -81,6 +81,11 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + // type is already filled, skip + if ($node->returnType instanceof Node) { + return null; + } + $scope = ScopeFetcher::fetch($node); if ($this->shouldSkip($node, $scope)) { return null; @@ -133,11 +138,6 @@ public function provideMinPhpVersion(): int private function shouldSkip(ClassMethod|Function_ $functionLike, Scope $scope): bool { - // type is already filled, skip - if ($functionLike->returnType instanceof Node) { - return true; - } - $returnType = $this->returnTypeInferer->inferFunctionLike($functionLike); $returnType = TypeCombinator::removeNull($returnType); if ($returnType instanceof UnionType) { diff --git a/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php b/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php index 556532a1774..39594132825 100644 --- a/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php +++ b/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php @@ -75,8 +75,6 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $scope = ScopeFetcher::fetch($node); - $yieldNodes = $this->yieldNodeFinder->find($node); if ($yieldNodes === []) { return null; @@ -90,6 +88,7 @@ public function refactor(Node $node): ?Node return null; } + $scope = ScopeFetcher::fetch($node); if ($node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod( $node, $scope