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

Add support for strict arrays, fix type alias intersection, fix array_is_list assertion on non-lists #8395

Merged
merged 159 commits into from Nov 5, 2022

Conversation

danog
Copy link
Collaborator

@danog danog commented Aug 11, 2022

This PR introduces new syntax to define list-like keyed arrays: list{} which replaces the previously undocumented (and frankly confusing) array{int, string}.
Also introducing syntax for callable-list{}.

Finally, introducing strict-*{} for sealed variants of array types.

@orklah
Copy link
Collaborator

orklah commented Aug 11, 2022

right now, sealed arrays can only be created by explicitly creating an array in php. The sealed flag is used for several things (like being sure of how many elements there is in an array or checking if the array can be inferred to be empty, things like that).

If you want to be able to document a sealed-array, there are a few new rules that will need to be enforced. For example, a method expecting a sealead-array param now need to check that the provided array is sealed. Likewise, trying to fetch an undocumented key from a sealed array should return TNever (EDIT: this seems handled by InvalidArrayOffset already)

There will be a bunch of things like that to handle that were not previously implemented because the sealed flag couldn't be used directly by people.

@kkmuffme
Copy link
Contributor

I think list{} is ultra confusing, since a list currently is a 0-monotonous index array.

@orklah
Copy link
Collaborator

orklah commented Nov 5, 2022

there was (I think) no pre-existing support for that in psalm

We have a is_list property in TKeyedArray that will change a standard keyed array like array{0: int, 1: string} into an array{int, string}. The second one enforces the order. That only works with list-like keys though

@danog
Copy link
Collaborator Author

danog commented Nov 5, 2022

That only works with list-like keys though

Ah that makes sense, I suppose it would be nice to have an ordered-strict-array variant, but that's for a future PR ;)

This PR should be now good to merge, the phpcs comments can be removed once @ondrejmirtes merges phpstan/phpdoc-parser#161 and tags, which adds basic strict-array, strict-list and list syntax support to phpstan and phpcs, and @romm merges CuyZ/Valinor#246 so at least strict-array syntax is supported in valinor :)

@danog danog changed the title Sealed shaped arrays, fix type alias intersection, fix array_is_list assertion on non-lists Strict arrays, fix type alias intersection, fix array_is_list assertion on non-lists Nov 5, 2022
@danog danog changed the title Strict arrays, fix type alias intersection, fix array_is_list assertion on non-lists Add support for strict arrays, fix type alias intersection, fix array_is_list assertion on non-lists Nov 5, 2022
@danog danog added release:feature The PR will be included in 'Features' section of the release notes release:fix The PR will be included in 'Fixes' section of the release notes release:docs The PR will be included in 'Docs' section of the release notes labels Nov 5, 2022
@orklah orklah merged commit 1986c8b into vimeo:master Nov 5, 2022
@orklah
Copy link
Collaborator

orklah commented Nov 5, 2022

Thanks a lot again!

@zmitic
Copy link

zmitic commented Nov 24, 2022

This is very useful feature. Can it be added to 4.x branch?

@danog
Copy link
Collaborator Author

danog commented Nov 24, 2022

Sorry, the 4.x is in feature freeze, no more PRs are accepted for it, but you can already upgrade to the latest rc1 of Psalm v5 right now, the release itself is imminent! :D

@zmitic
Copy link

zmitic commented Nov 24, 2022

@danog I tried, but it is the Symfony plugin that depends on V4.

Never mind, I will see what I can do. Thank you for this awesome feature anyway.

@orklah
Copy link
Collaborator

orklah commented Nov 24, 2022

It makes sense that Symfony still depends on Psalm 4 given the release is yet to come! One of our task after releasing will be to make sure there are no big plugins left behind for too long :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:docs The PR will be included in 'Docs' section of the release notes release:feature The PR will be included in 'Features' section of the release notes release:fix The PR will be included in 'Fixes' section of the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants