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

List shapes #8789

Closed
zonuexe opened this issue Jan 29, 2023 · 5 comments
Closed

List shapes #8789

zonuexe opened this issue Jan 29, 2023 · 5 comments

Comments

@zonuexe
Copy link
Contributor

zonuexe commented Jan 29, 2023

Feature request

List shapes is a variant of array shapes introduced in Psalm 5 that only support implicit and integer-indexed keys.

list{string, int}
list{0: string, 1: int}
list{0: string, 1?: int}

The first step is to just parse list{} as an alias for array{} for interoperability with Psalm.

Did PHPStan help you today? Did it make you happy in any way?

This week I am writing an 8-page article to introduce PHPStan at the PHPerKaigi Japanese conference.

@ondrejmirtes
Copy link
Member

Hi, I don't really understand the difference between:

list{string, int}
list{0: string, 1: int}
list{0: string, 1?: int}

and

array{string, int}
array{0: string, 1: int}
array{0: string, 1?: int}

Can you please explain where would they behave differently in PHPStan?

@zonuexe
Copy link
Contributor Author

zonuexe commented Jan 29, 2023

list{string, int}
list{0: string, 1: int}
list{0: string, 1?: int}

and

array{string, int}
array{0: string, 1: int}
array{0: string, 1?: int}

Those examples match array and list exactly.


The following shapes are not allowed in list{}

list{1: 1, 2: 2, 3: 3} // should be array{1: 1, 2: 2, 3: 3}
list{1, 2, key: 3}  // should be array{0: 1, 1: 2, key: 3}

These patterns can emulate Psalm's behavior by outputting them as the intersection of an array shape and list.

Also, PHPStan does not yet implement sealed array shapes, so proper support requires changes beyond the parser.

@ondrejmirtes
Copy link
Member

Those examples match array and list exactly.

What do you mean? They're going to behave exactly the same way?

intersection of an array shape and list

Oh yeah, we could do the same thing here. If the ArrayShapeNode kind is list, we can intersect the array shape with AccessoryArrayListType, which will make list{1: 1, 2: 2, 3: 3} error with "unresolvable type".

@ondrejmirtes
Copy link
Member

Implemented: phpstan/phpstan-src#2202

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants