Fix ConstantArrayType
optional key unsetting via removeLastElements
#1366
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes phpstan/phpstan#7351
That was a really interesting one. The changes in #1352 were triggering it, but that bug was already there and I just seem to have adopted it too.
The problem was that while traversing the array and using
$i
as counter variable, we cannot simply unset the optional key on position$i
if the array element on that position is going to be removed.E.g. the array
array{a: 0, b?: 1}
would be represented with the$optionalKeys
array{0: 1}
and if if we removeb
from the original array (which is on position 1) then we don't remove anything from$optionalKeys
. Instead we have to unset the key of the value1
(which is actually0
) to keep the state consistent.Without this change we ended up with an array with one entry but 2 of them being marked optional and this triggered the fatal error later via
ConstantArrayType::getAllArrays