diff --git a/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector.php index e02c693d8a8..89fe27e17f7 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector.php @@ -93,6 +93,11 @@ public function refactor(Node $node): ?Node return null; } + $returns = $this->betterNodeFinder->findReturnsScoped($node); + if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($node, $returns)) { + return null; + } + $scope = ScopeFetcher::fetch($node); if ($this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope)) { return null; @@ -101,11 +106,6 @@ public function refactor(Node $node): ?Node $tryReturnType = null; $catchReturnTypes = []; - $returns = $this->betterNodeFinder->findReturnsScoped($node); - if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($node, $returns)) { - return null; - } - foreach ((array) $node->stmts as $classMethodStmt) { if (! $classMethodStmt instanceof TryCatch) { continue; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector.php index a264478c2fc..b2acc31a8a1 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector.php @@ -76,6 +76,10 @@ public function refactor(Node $node): ?Node return null; } + if (! $this->hasReturnArray($node)) { + return null; + } + $scope = ScopeFetcher::fetch($node); if ($node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod( $node, @@ -84,10 +88,6 @@ public function refactor(Node $node): ?Node return null; } - if (! $this->hasReturnArray($node)) { - return null; - } - $type = $this->returnTypeInferer->inferFunctionLike($node); if (! $type->isArray()->yes()) { return null; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php index 632fdcb12a3..091c601d464 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php @@ -89,11 +89,6 @@ public function refactor(Node $node): ?Node return null; } - $scope = ScopeFetcher::fetch($node); - if ($this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope)) { - return null; - } - if (count($node->stmts) !== 1) { return null; } @@ -111,6 +106,11 @@ public function refactor(Node $node): ?Node return null; } + $scope = ScopeFetcher::fetch($node); + if ($this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope)) { + return null; + } + if (! $this->isObjectType($returnExpr->var, new ObjectType('Symfony\Component\Serializer\Serializer'))) { return null; } diff --git a/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php b/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php index ca61272ee5b..1d52a3a8800 100644 --- a/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php +++ b/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php @@ -86,11 +86,6 @@ public function refactor(Node $node): ?Node return null; } - $scope = ScopeFetcher::fetch($node); - if ($this->shouldSkip($node, $scope)) { - return null; - } - if ($node->stmts === null) { return null; } @@ -109,6 +104,11 @@ public function refactor(Node $node): ?Node return null; } + $scope = ScopeFetcher::fetch($node); + if ($this->shouldSkip($node, $scope)) { + return null; + } + $ternary = $return->expr; $returnScope = $return->expr->getAttribute(AttributeKey::SCOPE); diff --git a/rules/TypeDeclaration/Rector/Closure/ClosureReturnTypeFromAssertInstanceOfRector.php b/rules/TypeDeclaration/Rector/Closure/ClosureReturnTypeFromAssertInstanceOfRector.php index 28a0738da13..f611bb80b6b 100644 --- a/rules/TypeDeclaration/Rector/Closure/ClosureReturnTypeFromAssertInstanceOfRector.php +++ b/rules/TypeDeclaration/Rector/Closure/ClosureReturnTypeFromAssertInstanceOfRector.php @@ -95,13 +95,13 @@ public function refactor(Node $node): ?Node return null; } - $scope = ScopeFetcher::fetch($node); - if (! $this->isInsideTestCaseClass($scope)) { + // only handle closures narrowed by assertInstanceOf(); plain returns are handled by ClosureReturnTypeRector + if (! $this->hasAssertInstanceOf($node)) { return null; } - // only handle closures narrowed by assertInstanceOf(); plain returns are handled by ClosureReturnTypeRector - if (! $this->hasAssertInstanceOf($node)) { + $scope = ScopeFetcher::fetch($node); + if (! $this->isInsideTestCaseClass($scope)) { return null; }