-
Notifications
You must be signed in to change notification settings - Fork 534
Add "DynamicFunctionReturnTypeExtension" for "array_replace" #622
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
Conversation
Which case doesn‘t work? It seems all tests pass? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this union handling covered by a test?
i am wondering why only the last type of a union is taken into account
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand it, I only copy&past this code. :-/
I will try to add a test for that, but I also don't understand the "unpack" part here. Whenever we unpack "...", then we need to use a second loop for all possible arguments, or not?
array_combine is working as expected all other cases, are not what I expected. array_merge and array_replace results looks wrong for me, because some indexes are doublicate in the array and so there should be 4 or 2, but not both. Or? 🤔 |
does this mean the tests added with the PR don‘t assert the values you would expect? I am wondering why the testsuite is green. |
Regarding the general idea of array_replace: I would expect phpstan to know that the returned array will have the same key-types as the 1st arg but the value types being a union of the value type of all args? I would leave the arg-unpack case as a next iteration open for a followup PR and instead try to cover the most important use-cases first |
since you mentioned array_combine is doing what you expect. In case you didn't find it yet, the corresponding array_combine sources are here: https://github.com/phpstan/phpstan-src/blob/master/src/Type/Php/ArrayCombineFunctionReturnTypeExtension.php |
I added support for array shapes, but we need to combine the different arrays the same way as array_merge / array_replace would do it. Any idea how to achieve that? EDIT: logic is implemented @ondrejmirtes what do you think about the changes, or is there already code for combining arrays, somewhere in the codebase? 🤔 What is with the "+" operator, do we need to do something similar for that? |
28bb5f3
to
bb31ede
Compare
5204cf0
to
46a3963
Compare
INFO: The failed tests seem to come from master. |
48ac30d
to
6f403b0
Compare
bf77d0e
to
f989e87
Compare
@ondrejmirtes I added some more tests here, but I don't know if we need more tests here? Do you have some edge cases that should be tested in mind? |
I think this will also fix: |
could you verify that by adding a unit test? |
would it be easier to have separate PRs for the 2 extensions? |
f989e87
to
b3a8112
Compare
done: #641 |
b3a8112
to
139b720
Compare
Thank you! |
Unfortunately I had to revert this extension: d28f785 It's because of performance impact on real-world projects. The extension itself may not be the fault but it proves that having more ConstantArrayType instances during analysis leads to a slowdown. The only way I see is to build a benchmarking mode into PHPStan itself that would compare the analysis times of project files and point out which file slowed down after this commit. Then isolate what exactly in that file causes it and try to optimize that. Unfortunately it's not my priority right now. |
@voku do you think you could re-do this PR without using the that way your work could at least close phpstan/phpstan#5327 and we could have a more in-detail look regarding the array-shape related stuff with a focus on performance in a separate PR |
discussion: phpstan/phpstan#5440