Skip to content

Conversation

@herndlm
Copy link
Contributor

@herndlm herndlm commented Apr 13, 2022

Closes phpstan/phpstan#6974

While it would previously error with Variable $a in empty() always exists and is not falsy., it errors now with Variable $a in empty() always exists and is always falsy.. I think this makes sense, because via empty array variadic parameter basically nothing is pushed onto the empty array, so it stays empty.

The non-empty-array intersections are not needed because setOffsetValueType should be doing this internally already.

@herndlm herndlm marked this pull request as draft April 13, 2022 14:51
@herndlm
Copy link
Contributor Author

herndlm commented Apr 13, 2022

I noticed one thing here, that did not change: if e.g. mixed[] is unpacked and pushed onto the empty array then it would evaluate to a truthy value as in https://phpstan.org/r/91ebaf38-ad93-499f-8f84-6382b13df4a8.
This is not correct and should not error IMO, as the array could be empty, then nothing is pushed and we have a falsy value. I wanted to improve this here too, but failed because of the constant arrays that are involved. They always lead to true/false values when calling toBoolean().

@herndlm herndlm marked this pull request as ready for review April 13, 2022 22:26
@ondrejmirtes
Copy link
Member

Please push a test with https://phpstan.org/r/91ebaf38-ad93-499f-8f84-6382b13df4a8, I have an idea about it :)

@herndlm herndlm marked this pull request as draft April 15, 2022 18:30
@herndlm
Copy link
Contributor Author

herndlm commented Apr 15, 2022

Pushed new tests and the implementation of the idea that I had as well :) It is a bit more complicated than I hoped it would be, maybe you have a better one.
Update: the longer I think about it, the more I can come up with weird edge cases or simplification ideas. I'll take another look tomorrow.

@herndlm herndlm marked this pull request as ready for review April 15, 2022 19:29
@herndlm herndlm marked this pull request as draft April 15, 2022 22:34
@herndlm
Copy link
Contributor Author

herndlm commented Apr 17, 2022

OK, I ended up basically re-writing the array_push / array_unshift handling. Not sure if that's ok, sorry 😅

It seems like this is quite well covered via both LegacyNodeScopeResolverTest - binary.php and NodeScopeResolverTest - constant-array-optional-set.php. At least those were the ones that started failing while I was working on this.
Maybe it is also possible to strip out even more common stuff from the 2 blocks that I currently have (constant array handling, other handling). done

@herndlm herndlm marked this pull request as ready for review April 17, 2022 13:09
@herndlm herndlm marked this pull request as draft April 17, 2022 13:23
@herndlm herndlm marked this pull request as ready for review April 17, 2022 19:15
@herndlm herndlm marked this pull request as draft April 18, 2022 11:47
@herndlm
Copy link
Contributor Author

herndlm commented Apr 18, 2022

found another thing (#1225) that will make things fail here atm, marking as draft for now again..

@herndlm
Copy link
Contributor Author

herndlm commented Apr 19, 2022

From my POV this would be ready. This PR improves array_push and array_unshift in general now. IMO it still simplifies it a bit, but tbh I don't like the current complexity much either. Parts of it are because of different handling for ConstantArrayType but I don't think this can be handled better atm.

The 10 failing phpstan-symfony checks should be unrelated.

@herndlm herndlm marked this pull request as ready for review April 19, 2022 10:35
@ondrejmirtes
Copy link
Member

Looks complicated, but probably worth it :) Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants