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

Strict check when writing to array #3703

Closed
eigan opened this issue Aug 3, 2020 · 4 comments
Closed

Strict check when writing to array #3703

eigan opened this issue Aug 3, 2020 · 4 comments
Labels
Milestone

Comments

@eigan
Copy link

eigan commented Aug 3, 2020

Bug/Feature report

Writing to array should verify that the type is correct.

Code snippet that reproduces the problem

https://phpstan.org/r/d6bd5e2f-c8e7-4b07-b0ab-dc02940c71e4

Expected output

$array expects array<string, array<string, array<int>>>, array<string, array<string, array<int, string>>> given

Not sure how to handle $array = [].

Relates to #3311 though it looks to be specific to non-keyed arrays.

@ondrejmirtes
Copy link
Member

Arrays are not typed this way, only when:

  1. Array is a type of a property, then it's checked.
  2. When you're using array shapes as method parameters/return types then it's also checked.

This has been discussed before #3269

@eigan
Copy link
Author

eigan commented Aug 3, 2020

  1. Array is a type of a property, then it's checked.

Like this? https://phpstan.org/r/5866015d-9773-4f2b-a564-ba471fb8a2d9

@phpstan-bot
Copy link
Contributor

@eigan After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-21: Array (array<string, array<string, array<int>>>) does not accept string.
+15: Property Foo::$bar (array<string, array<string, array<int>>>) does not accept array<string, array<string, array<int, string>>>.
+18: Property Foo::$bar (array<string, array<string, array<int>>>) does not accept array<string, array<string, int>>.
+21: Property Foo::$bar (array<string, array<string, array<int>>>) does not accept array<string, string>.
Full report
Line Error
15 Property Foo::$bar (array<string, array<string, array<int>>>) does not accept array<string, array<string, array<int, string>>>.
18 Property Foo::$bar (array<string, array<string, array<int>>>) does not accept array<string, array<string, int>>.
21 Property Foo::$bar (array<string, array<string, array<int>>>) does not accept array<string, string>.

@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 Feb 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants