From dc89acf94dabb3eaa8299baef21b0d00e7f6b596 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 30 Jul 2025 22:05:28 +0700 Subject: [PATCH 1/3] [TypeDeclaration] Skip try catch finally different type on AddReturnTypeFromTryCatchTypeRector --- ...kip_try_catch_finally_different_type.php.inc | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector/Fixture/skip_try_catch_finally_different_type.php.inc 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 @@ + Date: Wed, 30 Jul 2025 22:09:24 +0700 Subject: [PATCH 2/3] Fix --- .../AddReturnTypeFromTryCatchTypeRector.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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; } From cc539cf8e3449fdb8b5f32d64fe477699d262642 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 30 Jul 2025 22:11:53 +0700 Subject: [PATCH 3/3] add fixture same type --- .../try_catch_finally_same_type.php.inc | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector/Fixture/try_catch_finally_same_type.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector/Fixture/try_catch_finally_same_type.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector/Fixture/try_catch_finally_same_type.php.inc new file mode 100644 index 00000000000..b31edee97fe --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector/Fixture/try_catch_finally_same_type.php.inc @@ -0,0 +1,39 @@ + +----- + \ No newline at end of file