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

Consider optional keys in ConstantArrayType::slice #1345

Merged
merged 2 commits into from May 28, 2022

Conversation

herndlm
Copy link
Contributor

@herndlm herndlm commented May 24, 2022

This was hard. I think the tests show that it works correct, but tbh., it's a challenge for me to get the more complicated optional examples right in my head :D

There're a couple of more interesting "normal" constant array array_slice tests existing already in LegacyNodeScopeResolverTest.

@herndlm herndlm force-pushed the array-slice branch 3 times, most recently from 60bbcba to 286f1b4 Compare May 24, 2022 21:09
@herndlm herndlm marked this pull request as ready for review May 24, 2022 21:20
@herndlm
Copy link
Contributor Author

herndlm commented May 24, 2022

still not sure, about some tests, I'll check this again tomorrow. The behaviour is not correct yet

@herndlm herndlm marked this pull request as draft May 24, 2022 21:52
@ondrejmirtes
Copy link
Member

Oh yes, it's hard :)

@herndlm herndlm force-pushed the array-slice branch 3 times, most recently from a70fb09 to 922a531 Compare May 25, 2022 18:31
@herndlm
Copy link
Contributor Author

herndlm commented May 25, 2022

I think I made it work. Also added all edge cases I could think of. It's unfortunately far from as elegant as I was hoping, mostly because of negative offsets and lengths/limits.
Don't want to see this anymore for a bit xD, but maybe I'll come back later and think through a couple of the test cases again to ensure I didn't miss anything obvious.

@herndlm herndlm marked this pull request as ready for review May 25, 2022 18:41
@herndlm herndlm force-pushed the array-slice branch 5 times, most recently from 78106bb to 69fb4dc Compare May 25, 2022 20:14
@herndlm herndlm marked this pull request as draft May 26, 2022 07:05
@herndlm
Copy link
Contributor Author

herndlm commented May 26, 2022

Unfortunately I did find one more thing. I'll adapt it in the evening.

@herndlm
Copy link
Contributor Author

herndlm commented May 26, 2022

I have a fix locally again, but I was wondering @ondrejmirtes: I saw that many optional key cases seem to be handled via ConstantArrayType::getAllArrays which creates a power set. Then often, the arrays are modified in a loop, to be then unified again. My question: Should this always be preferred? E.g. I saw that array_pop uses this as well, combined with ConstantArrayType::removeLast, but I could simply adapt removeLast to handle the optional keys which safes us all the hassle of creating and unifying many arrays. Would that make sense from a performance point of view? If so, can I simply adapt removeLast (in yet another PR of course) or is BC a problem here?

@ondrejmirtes
Copy link
Member

I've stopped using ConstantArrayType::getAllArrays on purpose, because although it makes operations you're trying to fix easier, it's really expensive performance wise. That's why I decided it's better to use the one ConstantArrayType with optional keys included, and fix all the algorithms where ConstantArrayType::getAllArrays was previously used.

@herndlm
Copy link
Contributor Author

herndlm commented May 26, 2022

nice, ok then, I'll need to do something else first then ;) and thx for the immediate answer btw! :)

@herndlm
Copy link
Contributor Author

herndlm commented May 27, 2022

OK, I think I'm happy with it now. I did this in 2 commits, the first one extracts generic remove(First|Last)Elements from the recently adapted removeFirst and removeLast that works with an arbitrary amount of elements. The second commit does the slice magic. And complexity-wise I think it's OK now.

I changed the return type of removeFirst from Type to self though, which causes the BC check to fail. Is this really a problem? I can change that back for sure, makes using it a bit more annoying, but that's not a big deal. I saw that removeLast also returns self, so it made sense to me also consistency-wise.

@herndlm herndlm marked this pull request as ready for review May 27, 2022 21:10
@ondrejmirtes
Copy link
Member

Thank you very much for figuring this out :) I agree it's not really a BC break.

@ondrejmirtes ondrejmirtes merged commit b24249b into phpstan:1.7.x May 28, 2022
@herndlm herndlm deleted the array-slice branch July 25, 2022 18:40
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