Commit 8163acb
Filter conditional expressions from foreach body in processAlwaysIterableForeachScopeWithoutPollute
When using $finalScope->conditionalExpressions, conditional expressions
referencing variables only defined inside the foreach body (like $key)
could leak into the outer scope. When those conditional expressions fired
(e.g. inside an if block), the foreach variable would be re-introduced
with Yes certainty, causing false "Foreach overwrites $key" errors.
Filter conditional expressions to only keep those where both the target
expression and all condition expressions reference variables that existed
before the foreach loop.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>1 parent 7cca523 commit 8163acb
File tree
2 files changed
+46
-1
lines changed- src/Analyser
- tests/PHPStan/Analyser/data
2 files changed
+46
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3870 | 3870 | | |
3871 | 3871 | | |
3872 | 3872 | | |
| 3873 | + | |
| 3874 | + | |
| 3875 | + | |
| 3876 | + | |
| 3877 | + | |
| 3878 | + | |
| 3879 | + | |
| 3880 | + | |
| 3881 | + | |
| 3882 | + | |
| 3883 | + | |
| 3884 | + | |
| 3885 | + | |
| 3886 | + | |
| 3887 | + | |
| 3888 | + | |
| 3889 | + | |
| 3890 | + | |
| 3891 | + | |
| 3892 | + | |
3873 | 3893 | | |
3874 | 3894 | | |
3875 | 3895 | | |
3876 | 3896 | | |
3877 | 3897 | | |
3878 | 3898 | | |
3879 | 3899 | | |
3880 | | - | |
| 3900 | + | |
3881 | 3901 | | |
3882 | 3902 | | |
3883 | 3903 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
0 commit comments