Skip to content

Commit

Permalink
[CodeQuality] Skip Always return in try catch with finally on Explici…
Browse files Browse the repository at this point in the history
…tReturnNullRector (#5807)

* [CodeQuality] Skip Always return in try catch with finally on ExplicitReturnNullRector

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Apr 7, 2024
1 parent 145b616 commit 690fe89
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 54 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector\Fixture;

final class SkipAlwaysReturnInTryCatchWithFinally
{
public function run(int $number)
{
if (rand(0, 1)) {
try {
return execute();
} catch (\Exception $e) {
return 2;
} finally {
echo 'here executed after return';
}
} else {
return 2;
}
}
}
20 changes: 14 additions & 6 deletions rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,27 @@ private function isSwitchWithAlwaysReturnOrExit(Switch_ $switch): bool

private function isTryCatchAlwaysReturnOrExit(TryCatch $tryCatch): bool
{
$hasReturnOrExitInFinally = $tryCatch->finally instanceof Finally_ && $this->hasStmtsAlwaysReturnOrExit(
$tryCatch->finally->stmts
);

if (! $this->hasStmtsAlwaysReturnOrExit($tryCatch->stmts)) {
return false;
return $hasReturnOrExitInFinally;
}

foreach ($tryCatch->catches as $catch) {
if (! $this->hasStmtsAlwaysReturnOrExit($catch->stmts)) {
return false;
if ($this->hasStmtsAlwaysReturnOrExit($catch->stmts)) {
continue;
}

if ($hasReturnOrExitInFinally) {
continue;
}

return false;
}

return ! ($tryCatch->finally instanceof Finally_ && ! $this->hasStmtsAlwaysReturnOrExit(
$tryCatch->finally->stmts
));
return true;
}

private function resolveReturnOrExitCount(Switch_ $switch): int
Expand Down

0 comments on commit 690fe89

Please sign in to comment.