Skip to content

Commit cde53d1

Browse files
committed
Conditional expressions - do not take conclusions about identical variable in assignment
1 parent dc77608 commit cde53d1

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3795,6 +3795,10 @@ private function processSureTypesForConditionalExpressionsAfterAssign(Scope $sco
37953795
continue;
37963796
}
37973797

3798+
if ($expr->name === $variableName) {
3799+
continue;
3800+
}
3801+
37983802
if (!isset($conditionalExpressions[$exprString])) {
37993803
$conditionalExpressions[$exprString] = [];
38003804
}
@@ -3825,6 +3829,10 @@ private function processSureNotTypesForConditionalExpressionsAfterAssign(Scope $
38253829
continue;
38263830
}
38273831

3832+
if ($expr->name === $variableName) {
3833+
continue;
3834+
}
3835+
38283836
if (!isset($conditionalExpressions[$exprString])) {
38293837
$conditionalExpressions[$exprString] = [];
38303838
}

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,8 @@ public function dataFileAsserts(): iterable
11571157
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8568.php');
11581158
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/DeadCode/data/bug-8620.php');
11591159
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8635.php');
1160+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8625.php');
1161+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8621.php');
11601162
}
11611163

11621164
/**
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Bug8621;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class HelloWorld
8+
{
9+
/**
10+
* @param array<string> $data
11+
*/
12+
public function rows (array $data): void
13+
{
14+
$even = true;
15+
16+
echo "<table>";
17+
foreach ($data as $datum)
18+
{
19+
$even = !$even;
20+
assertType('bool', $even);
21+
22+
echo "<tr class='" . ($even ? 'even' :'odd') . "'>";
23+
echo "<td>{$datum}</td></tr>";
24+
}
25+
echo "</table>";
26+
}
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Bug8625;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class HelloWorld
8+
{
9+
public static function sayHello(): void
10+
{
11+
$key = false;
12+
$loopArray = [1, 2, 3];
13+
foreach ($loopArray as $i) {
14+
$key = $key === false;
15+
assertType('bool', $key);
16+
if ($key) {
17+
echo "i=$i key is true\n";
18+
} else {
19+
echo "i=$i key is false\n";
20+
}
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)