-
-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DeadCode] Handle always terminated TryCatch on RemoveUnreachableStat…
…ementRector (#2545) * [DeadCode] Handle always terminated TryCatch on RemoveUnreachableStatementRector * implemented 🎉
- Loading branch information
1 parent
a16bc1b
commit 6c713a7
Showing
5 changed files
with
140 additions
and
38 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
.../Rector/Stmt/RemoveUnreachableStatementRector/Fixture/always_terminated_try_catch.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture; | ||
|
||
use Exception; | ||
|
||
class AlwaysTerminatedTryCatch | ||
{ | ||
public function run() | ||
{ | ||
try { | ||
return something(); | ||
} catch (Exception $e) { | ||
return null; | ||
} | ||
|
||
echo 'never executed'; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture; | ||
|
||
use Exception; | ||
|
||
class AlwaysTerminatedTryCatch | ||
{ | ||
public function run() | ||
{ | ||
try { | ||
return something(); | ||
} catch (Exception $e) { | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
?> |
17 changes: 0 additions & 17 deletions
17
...ector/Stmt/RemoveUnreachableStatementRector/Fixture/skip_try_finally_only_comment.php.inc
This file was deleted.
Oops, something went wrong.
37 changes: 37 additions & 0 deletions
37
...e/Rector/Stmt/RemoveUnreachableStatementRector/Fixture/try_return_finally_comment.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture; | ||
|
||
class TryFinallyOnlyCommentStmt | ||
{ | ||
public function setMultiple($values, $ttl = null): bool | ||
{ | ||
try { | ||
return true; | ||
} finally { | ||
// only comment | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture; | ||
|
||
class TryFinallyOnlyCommentStmt | ||
{ | ||
public function setMultiple($values, $ttl = null): bool | ||
{ | ||
try { | ||
return true; | ||
} finally { | ||
// only comment | ||
} | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\NodeAnalyzer; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Exit_; | ||
use PhpParser\Node\Stmt; | ||
use PhpParser\Node\Stmt\Expression; | ||
use PhpParser\Node\Stmt\Finally_; | ||
use PhpParser\Node\Stmt\Return_; | ||
use PhpParser\Node\Stmt\Throw_; | ||
use PhpParser\Node\Stmt\TryCatch; | ||
|
||
final class TryCatchAnalyzer | ||
{ | ||
/** | ||
* @var array<class-string<Node>> | ||
*/ | ||
private const TERMINATED_NODES = [Return_::class, Throw_::class]; | ||
|
||
public function isAlwaysTerminated(TryCatch $tryCatch): bool | ||
{ | ||
if ($tryCatch->finally instanceof Finally_ && $this->isTerminated($tryCatch->finally->stmts)) { | ||
return true; | ||
} | ||
|
||
foreach ($tryCatch->catches as $catch) { | ||
if (! $this->isTerminated($catch->stmts)) { | ||
return false; | ||
} | ||
} | ||
|
||
return $this->isTerminated($tryCatch->stmts); | ||
} | ||
|
||
/** | ||
* @param Stmt[] $stmts | ||
*/ | ||
private function isTerminated(array $stmts): bool | ||
{ | ||
if ($stmts === []) { | ||
return false; | ||
} | ||
|
||
$lastKey = array_key_last($stmts); | ||
$lastNode = $stmts[$lastKey]; | ||
|
||
if ($lastNode instanceof Expression) { | ||
return $lastNode->expr instanceof Exit_; | ||
} | ||
|
||
return in_array($lastNode::class, self::TERMINATED_NODES, true); | ||
} | ||
} |