diff --git a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanConstAndStrictReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanConstAndStrictReturnsRector.php index 91963352223..92e8263cd34 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanConstAndStrictReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanConstAndStrictReturnsRector.php @@ -96,13 +96,13 @@ public function refactor(Node $node): ?Node return null; } - $scope = ScopeFetcher::fetch($node); - if ($this->shouldSkip($node, $scope)) { + $returns = $this->betterNodeFinder->findReturnsScoped($node); + if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($node, $returns)) { return null; } - $returns = $this->betterNodeFinder->findReturnsScoped($node); - if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($node, $returns)) { + $scope = ScopeFetcher::fetch($node); + if ($this->shouldSkip($node, $scope)) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanConstReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanConstReturnsRector.php index b61a33bc5ec..0b016ec467d 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanConstReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanConstReturnsRector.php @@ -90,13 +90,13 @@ public function refactor(Node $node): ?Node return null; } - $scope = ScopeFetcher::fetch($node); - if ($this->shouldSkip($node, $scope)) { + $returns = $this->betterNodeFinder->findReturnsScoped($node); + if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($node, $returns)) { return null; } - $returns = $this->betterNodeFinder->findReturnsScoped($node); - if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($node, $returns)) { + $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 c1efa5f71ff..b1dbef0a810 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector.php @@ -90,13 +90,13 @@ public function refactor(Node $node): ?Node return null; } - $scope = ScopeFetcher::fetch($node); - if ($this->shouldSkip($node, $scope)) { + $returns = $this->betterNodeFinder->findReturnsScoped($node); + if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($node, $returns)) { return null; } - $returns = $this->betterNodeFinder->findReturnsScoped($node); - if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($node, $returns)) { + $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 a9341104bfe..af1e7568fee 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictReturnsRector.php @@ -87,13 +87,13 @@ public function refactor(Node $node): ?Node return null; } - $scope = ScopeFetcher::fetch($node); - if ($this->shouldSkip($node, $scope)) { + $returns = $this->betterNodeFinder->findReturnsScoped($node); + if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($node, $returns)) { return null; } - $returns = $this->betterNodeFinder->findReturnsScoped($node); - if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($node, $returns)) { + $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 c9b94389fcf..2ffd56d9016 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictScalarReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/NumericReturnTypeFromStrictScalarReturnsRector.php @@ -85,13 +85,13 @@ public function refactor(Node $node): ?Node return null; } - $scope = ScopeFetcher::fetch($node); - if ($this->shouldSkip($node, $scope)) { + $returns = $this->betterNodeFinder->findReturnsScoped($node); + if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($node, $returns)) { return null; } - $returns = $this->betterNodeFinder->findReturnsScoped($node); - if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($node, $returns)) { + $scope = ScopeFetcher::fetch($node); + if ($this->shouldSkip($node, $scope)) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromMockObjectRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromMockObjectRector.php index eddc9dc2238..ee5e5cbcc87 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromMockObjectRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromMockObjectRector.php @@ -81,22 +81,22 @@ public function refactor(Node $node): ?Node return null; } - $scope = ScopeFetcher::fetch($node); - if (! $this->isInsideTestCaseClass($scope)) { + // we need exactly 1 return + $returns = $this->betterNodeFinder->findReturnsScoped($node); + if (count($returns) !== 1) { return null; } - if ($this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope)) { + if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($node, $returns)) { return null; } - // we need exactly 1 return - $returns = $this->betterNodeFinder->findReturnsScoped($node); - if (count($returns) !== 1) { + $scope = ScopeFetcher::fetch($node); + if (! $this->isInsideTestCaseClass($scope)) { return null; } - if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($node, $returns)) { + if ($this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope)) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php index 26fd39d39ff..138f38060aa 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php @@ -105,6 +105,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 ($node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod( $node, @@ -113,11 +118,6 @@ public function refactor(Node $node): ?Node return null; } - $returns = $this->betterNodeFinder->findReturnsScoped($node); - if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($node, $returns)) { - return null; - } - $returnedNewClassName = $this->strictReturnNewAnalyzer->matchAlwaysReturnVariableNew($node); if ($returnedNewClassName === 'object') { diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php index bcdaba9486c..610c0e650ea 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php @@ -115,22 +115,21 @@ public function refactor(Node $node): ?Node return null; } + $currentScopeReturns = $this->betterNodeFinder->findReturnsScoped($node); + if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($node, $currentScopeReturns)) { + return null; + } + $scope = ScopeFetcher::fetch($node); if ($this->shouldSkip($node, $scope)) { return null; } - $currentScopeReturns = $this->betterNodeFinder->findReturnsScoped($node); - $returnedStrictTypes = $this->returnStrictTypeAnalyzer->collectStrictReturnTypes($currentScopeReturns, $scope); if ($returnedStrictTypes === []) { return null; } - if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($node, $currentScopeReturns)) { - return null; - } - if (count($returnedStrictTypes) === 1) { return $this->refactorSingleReturnType($currentScopeReturns[0], $returnedStrictTypes[0], $node); }