Skip to content

Commit 94e6e46

Browse files
committed
Last elseif can be exhaustive and no else branch is not needed
1 parent a42b35c commit 94e6e46

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ private function processStmtNode(
784784
}
785785

786786
if ($stmt->else === null) {
787-
if (!$ifAlwaysTrue) {
787+
if (!$ifAlwaysTrue && !$lastElseIfConditionIsTrue) {
788788
$finalScope = $scope->mergeWith($finalScope);
789789
$alwaysTerminating = false;
790790
}

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,7 @@ public function dataFileAsserts(): iterable
11651165
if (PHP_VERSION_ID >= 80000) {
11661166
yield from $this->gatherAssertTypes(__DIR__ . '/data/pathinfo-php8.php');
11671167
}
1168+
yield from $this->gatherAssertTypes(__DIR__ . '/data/always-true-elseif.php');
11681169
yield from $this->gatherAssertTypes(__DIR__ . '/data/pathinfo.php');
11691170
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8568.php');
11701171
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/DeadCode/data/bug-8620.php');
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace AlwaysTrueElseif;
4+
5+
use PHPStan\TrinaryLogic;
6+
use function PHPStan\Testing\assertVariableCertainty;
7+
8+
class Foo
9+
{
10+
11+
/**
12+
* @param 'a'|'b'|'c' $s
13+
* @return void
14+
*/
15+
public function doFoo(string $s): void
16+
{
17+
if ($s === 'a') {
18+
$a = true;
19+
} elseif ($s === 'b') {
20+
$a = false;
21+
} elseif ($s === 'c') {
22+
$a = true;
23+
}
24+
25+
assertVariableCertainty(TrinaryLogic::createYes(), $a);
26+
}
27+
28+
}

0 commit comments

Comments
 (0)