Skip to content

Commit da0e370

Browse files
VincentLangletondrejmirtes
authored andcommitted
Fix false positive unreachable
1 parent 0497f56 commit da0e370

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6765,7 +6765,7 @@ private function getNextUnreachableStatements(array $nodes, bool $earlyBinding):
67656765
$stmts[] = $node;
67666766
continue;
67676767
}
6768-
if ($node instanceof Node\Stmt\Nop) {
6768+
if ($node instanceof Node\Stmt\Nop || $node instanceof Node\Stmt\InlineHTML) {
67696769
continue;
67706770
}
67716771
if (!$node instanceof Node\Stmt) {

tests/PHPStan/Rules/DeadCode/UnreachableStatementRuleTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,17 @@ public function testBug11992(): void
229229
$this->analyse([__DIR__ . '/data/bug-11992.php'], []);
230230
}
231231

232+
public function testBug7531(): void
233+
{
234+
$this->treatPhpDocTypesAsCertain = false;
235+
$this->analyse([__DIR__ . '/data/bug-7531.php'], [
236+
[
237+
'Unreachable statement - code above always terminates.',
238+
22,
239+
],
240+
]);
241+
}
242+
232243
public function testMultipleUnreachable(): void
233244
{
234245
$this->treatPhpDocTypesAsCertain = true;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug7531;
4+
5+
/** @var int $compareTo */
6+
7+
?>
8+
9+
<?php foreach ([1,2,3] as $number) : ?>
10+
<?php if ($number > $compareTo) : ?>
11+
<xs:sequence>some xml data</xs:sequence>
12+
<?php else : ?>
13+
<?php throw new \Exception("Unexpected behavior") ?>
14+
<?php endif ?>
15+
<?php endforeach; ?>
16+
17+
<?php foreach ([1,2,3] as $number) : ?>
18+
<?php if ($number > $compareTo) : ?>
19+
<xs:sequence>some xml data</xs:sequence>
20+
<?php else : ?>
21+
<?php throw new \Exception("Unexpected behavior") ?>
22+
<?php throw new \Exception("Unreachable") ?>
23+
<?php endif ?>
24+
<?php endforeach; ?>

0 commit comments

Comments
 (0)