-
-
Notifications
You must be signed in to change notification settings - Fork 708
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rectorphp/rector-src@6c713a7 [DeadCode] Handle always terminated TryCatch on RemoveUnreachableStatementRector (#2545)
- Loading branch information
1 parent
9550950
commit bb396cb
Showing
11 changed files
with
84 additions
and
49 deletions.
There are no files selected for viewing
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
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,48 @@ | ||
<?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; | ||
} | ||
\end($stmts); | ||
$lastKey = \key($stmts); | ||
$lastNode = $stmts[$lastKey]; | ||
if ($lastNode instanceof Expression) { | ||
return $lastNode->expr instanceof Exit_; | ||
} | ||
return \in_array(\get_class($lastNode), self::TERMINATED_NODES, \true); | ||
} | ||
} |
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
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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