Understand always-overwritten arrays in foreach#4534
Conversation
81b1ada to
98cf40b
Compare
| && !$continueExitPointHasUnoriginalKeyType | ||
| && $stmt->keyVar !== null | ||
| && $exprType->isArray()->yes() | ||
| && $exprType->isConstantArray()->no() |
There was a problem hiding this comment.
tbh I did not yet understand why this PR works, but I wonder whether we could use the same logic to also infer a better result when the iteratee is a known constant array?
like in
There was a problem hiding this comment.
Why this works - I look at $a[$k] type at the end of the loop and at each continue point and if some other conditions are true (like there are zero break points), I rewrite the whole array value type with the $a[$k] type unioned from all the points I looked at.
There was a problem hiding this comment.
When $foreach->expr is a constant array I feel like we need a different approach, possibly analysing the foreach separately for each item (but run $nodeCallback for the rules still only once).
There was a problem hiding this comment.
Also I want to look at foreach ($a as &$v) to understand it better that also needs a different approach.
I don't understand why I didn't do this years ago 😂
Closes phpstan/phpstan#2273
Closes phpstan/phpstan#13730