Skip to content

Commit e88903f

Browse files
authored
[Core] Improve UnreachableStmtAnalyzer: verify itself by recursive call (#2300)
1 parent 33cd52e commit e88903f

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

src/NodeAnalyzer/UnreachableStmtAnalyzer.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,23 @@
44

55
namespace Rector\Core\NodeAnalyzer;
66

7-
use PhpParser\Node;
87
use PhpParser\Node\Stmt;
98
use Rector\NodeTypeResolver\Node\AttributeKey;
109

1110
final class UnreachableStmtAnalyzer
1211
{
13-
public function isStmtPHPStanUnreachable(Stmt $stmt): bool
12+
public function isStmtPHPStanUnreachable(?Stmt $stmt): bool
1413
{
14+
if (! $stmt instanceof Stmt) {
15+
return false;
16+
}
17+
1518
if ($stmt->getAttribute(AttributeKey::IS_UNREACHABLE) === true) {
1619
// here the scope is never available for next stmt so we have nothing to refresh
1720
return true;
1821
}
1922

20-
$previousStmt = $stmt;
21-
while ($previousStmt = $previousStmt->getAttribute(AttributeKey::PREVIOUS_NODE)) {
22-
if (! $previousStmt instanceof Node) {
23-
break;
24-
}
25-
26-
if ($previousStmt->getAttribute(AttributeKey::IS_UNREACHABLE) === true) {
27-
return true;
28-
}
29-
}
30-
31-
return false;
23+
$previousStmt = $stmt->getAttribute(AttributeKey::PREVIOUS_NODE);
24+
return $this->isStmtPHPStanUnreachable($previousStmt);
3225
}
3326
}

0 commit comments

Comments
 (0)