diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector/Fixture/skip_try_catch_finally_different_type.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector/Fixture/skip_try_catch_finally_different_type.php.inc new file mode 100644 index 00000000000..b07b1b55562 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector/Fixture/skip_try_catch_finally_different_type.php.inc @@ -0,0 +1,17 @@ + +----- + \ No newline at end of file diff --git a/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector.php index 6f1eeacf62b..f9d6e1ec866 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector.php @@ -8,6 +8,7 @@ use PhpParser\Node\Expr; use PhpParser\Node\Stmt\Catch_; use PhpParser\Node\Stmt\ClassMethod; +use PhpParser\Node\Stmt\Finally_; use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\TryCatch; use PHPStan\Type\Type; @@ -128,6 +129,13 @@ public function refactor(Node $node): ?Node $catchReturnTypes[] = $currentCatchType; } + + if ($tryCatch->finally instanceof Finally_) { + $finallyReturnType = $this->matchReturnType($tryCatch->finally); + if ($finallyReturnType instanceof Type) { + $catchReturnTypes[] = $finallyReturnType; + } + } } if (! $tryReturnType instanceof Type) { @@ -149,9 +157,9 @@ public function refactor(Node $node): ?Node return $node; } - private function matchReturnType(TryCatch|Catch_ $tryOrCatch): ?Type + private function matchReturnType(TryCatch|Catch_|Finally_ $tryOrCatchOrFinally): ?Type { - foreach ($tryOrCatch->stmts as $stmt) { + foreach ($tryOrCatchOrFinally->stmts as $stmt) { if (! $stmt instanceof Return_) { continue; }