Skip to content

Commit

Permalink
Last elseif can be exhaustive and no else branch is not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 10, 2023
1 parent a42b35c commit 94e6e46
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Analyser/NodeScopeResolver.php
Expand Up @@ -784,7 +784,7 @@ private function processStmtNode(
}

if ($stmt->else === null) {
if (!$ifAlwaysTrue) {
if (!$ifAlwaysTrue && !$lastElseIfConditionIsTrue) {
$finalScope = $scope->mergeWith($finalScope);
$alwaysTerminating = false;
}
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Expand Up @@ -1165,6 +1165,7 @@ public function dataFileAsserts(): iterable
if (PHP_VERSION_ID >= 80000) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/pathinfo-php8.php');
}
yield from $this->gatherAssertTypes(__DIR__ . '/data/always-true-elseif.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/pathinfo.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8568.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/DeadCode/data/bug-8620.php');
Expand Down
28 changes: 28 additions & 0 deletions tests/PHPStan/Analyser/data/always-true-elseif.php
@@ -0,0 +1,28 @@
<?php

namespace AlwaysTrueElseif;

use PHPStan\TrinaryLogic;
use function PHPStan\Testing\assertVariableCertainty;

class Foo
{

/**
* @param 'a'|'b'|'c' $s
* @return void
*/
public function doFoo(string $s): void
{
if ($s === 'a') {
$a = true;
} elseif ($s === 'b') {
$a = false;
} elseif ($s === 'c') {
$a = true;
}

assertVariableCertainty(TrinaryLogic::createYes(), $a);
}

}

0 comments on commit 94e6e46

Please sign in to comment.