Commit 5d4844f
committed
Fix phpstan/phpstan#14353: Falsy "Variable might not be defined" after foreach
- After a foreach that conditionally defines a variable, conditional expressions
are created tracking "if array is empty, variable doesn't exist"
- When isset() confirms the variable exists, specifyExpressionType sets certainty
to Yes but stale conditional expressions with No certainty persist
- A subsequent foreach over the same array triggers filterByTruthyValue for the
"array is empty" case, activating the stale conditional and removing the variable
- Fix: in specifyExpressionType, when certainty is Yes, remove conditional
expressions for the variable that have No certainty (would unset it)
- New regression test in tests/PHPStan/Rules/Variables/data/bug-14353.php1 parent 14bf97d commit 5d4844f
3 files changed
Lines changed: 63 additions & 1 deletion
File tree
- src/Analyser
- tests/PHPStan/Rules/Variables
- data
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2758 | 2758 | | |
2759 | 2759 | | |
2760 | 2760 | | |
| 2761 | + | |
| 2762 | + | |
| 2763 | + | |
| 2764 | + | |
| 2765 | + | |
| 2766 | + | |
| 2767 | + | |
| 2768 | + | |
| 2769 | + | |
| 2770 | + | |
| 2771 | + | |
2761 | 2772 | | |
2762 | 2773 | | |
2763 | 2774 | | |
2764 | 2775 | | |
2765 | 2776 | | |
2766 | 2777 | | |
2767 | 2778 | | |
2768 | | - | |
| 2779 | + | |
2769 | 2780 | | |
2770 | 2781 | | |
2771 | 2782 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1514 | 1514 | | |
1515 | 1515 | | |
1516 | 1516 | | |
| 1517 | + | |
| 1518 | + | |
| 1519 | + | |
| 1520 | + | |
| 1521 | + | |
| 1522 | + | |
| 1523 | + | |
| 1524 | + | |
| 1525 | + | |
1517 | 1526 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
0 commit comments