Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;

class AlwaysTerminatedIfElseWithTrailingComment
{
public function run($a)
{
if ($a) {
return 'A';
// and that is all
} else {
return 'B';
}

echo 'never executed';
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;

class AlwaysTerminatedIfElseWithTrailingComment
{
public function run($a)
{
if ($a) {
return 'A';
// and that is all
} else {
return 'B';
}
}
}

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

namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;

use Exception;

class AlwaysTerminatedTryCatchWithInfiniteWhile
{
public function run($a)
{
try {
while (true) {
if ($a) {
return 'A';
}

$a = doSomething();
}
} catch (Exception $e) {
return null;
}

echo 'never executed';
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;

use Exception;

class AlwaysTerminatedTryCatchWithInfiniteWhile
{
public function run($a)
{
try {
while (true) {
if ($a) {
return 'A';
}

$a = doSomething();
}
} catch (Exception $e) {
return null;
}
}
}

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

namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;

use Exception;

class AlwaysTerminatedTryCatchWithTrailingComment
{
public function run()
{
try {
return something();
// nothing to do here
} catch (Exception $e) {
return null;
}

echo 'never executed';
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;

use Exception;

class AlwaysTerminatedTryCatchWithTrailingComment
{
public function run()
{
try {
return something();
// nothing to do here
} catch (Exception $e) {
return null;
}
}
}

?>
12 changes: 12 additions & 0 deletions src/NodeAnalyzer/TerminatedNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,25 @@ private function isTerminatedInLastStmtsIf(If_ $if): bool
*/
private function isTerminatedInLastStmts(array $stmts): bool
{
// a trailing comment is parsed as a Nop, which executes nothing and so
// must not hide the terminating stmt in front of it
while ($stmts !== [] && end($stmts) instanceof Nop) {
array_pop($stmts);
}

if ($stmts === []) {
return false;
}

$lastKey = array_key_last($stmts);
$lastNode = $stmts[$lastKey];

// an infinite loop with no break terminates its block as surely as a
// return does, the same way isAlwaysTerminated() reads it one level up
if ($lastNode instanceof While_ || $lastNode instanceof Do_ || $lastNode instanceof For_) {
return $this->isTerminatedInfiniteLoop($lastNode);
}

if ($lastNode instanceof Expression) {
return $lastNode->expr instanceof Exit_ || $lastNode->expr instanceof Throw_;
}
Expand Down