Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ConstantArrayType optional key unsetting via removeLastElements #1366

Merged
merged 1 commit into from May 30, 2022

Conversation

herndlm
Copy link
Contributor

@herndlm herndlm commented May 30, 2022

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 remove b 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 value 1 (which is actually 0) 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

@ondrejmirtes
Copy link
Member

Yes, in optionalKeys the value is significant, not the index :)

@herndlm herndlm marked this pull request as ready for review May 30, 2022 09:22
@herndlm herndlm requested a review from ondrejmirtes May 30, 2022 09:23
@ondrejmirtes ondrejmirtes merged commit 135f4c1 into phpstan:1.7.x May 30, 2022
@ondrejmirtes
Copy link
Member

Thank you!

@herndlm herndlm deleted the fix-7351 branch May 30, 2022 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants