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

PHPStan probably wrong understand the ArrayAccess #7619

Closed
zeleznypa opened this issue Jul 13, 2022 · 7 comments
Closed

PHPStan probably wrong understand the ArrayAccess #7619

zeleznypa opened this issue Jul 13, 2022 · 7 comments

Comments

@zeleznypa
Copy link

Bug report

In this short example the phpstan return the following errors but it should be OK :)

Line Error
45 Cannot assign offset array{'deep', 'key'} to Foo<array<string, array<string, string>>>.
45 Foo<array{deep: array{key: string}}> does not accept string.

Code snippet that reproduces the problem

https://phpstan.org/r/e7080419-7d11-4124-b49d-84b9f36cf404

Expected output

Expected result is, that the code works well.
https://3v4l.org/GL0bM#v8.0.21

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

PHPStan and @ondrejmirtes helps me many times :) Thanks much

@staabm
Copy link
Contributor

staabm commented Jul 13, 2022

I think the structure of your $offset variable, does not reflect the type of your inline @var.

This example works https://phpstan.org/r/6706c2de-ab6d-4deb-a3a4-c0241cf8679a

@zeleznypa
Copy link
Author

@staabm Unfortunately no. The mechanism you described is also correct, but I really try to set the $offset by array, because it is a part of low-code solution, where somebody can define the offset as a structure.

@ondrejmirtes
Copy link
Member

A direct call maybe makes the problem clearer: https://phpstan.org/r/8755cdcd-66a3-46cd-b20f-4bb9f9327c89

Your only valid offset is 'deep' and your only valid value is array{key: string}, but you're assigning offset ['deep', 'key'] and value 'value'.

@zeleznypa
Copy link
Author

Ok, maybe better syntax should be: https://phpstan.org/r/b800de73-0f2a-4e29-bb44-649c5f9e2220

Description on line 43 describe the inner array structure:

/** @var Foo<array{deep: Foo<array{key: string}>}> $foo */

where the internal array can contain the key deep that will contain the object with internal array that contain the key key with string value

And the documentation on line 13

@param key-of<InnerArrayType>|key-of<InnerArrayType>[] $offset

said, that it can be deep or array{'deep'}.

Now the error code is correct

Line Error
45 Parameter #1 $offset of method Foo<array<string, Foo<array<string, string>>>>::offsetSet() expects 'deep'|array<'deep'>, array{'deep', 'key'} given.

So if I understand well, the „correct“ definition should be:

https://phpstan.org/r/d67d278a-69ff-4b2e-b37d-68b60e72b766

Because there is no possibility to describe this?

@ondrejmirtes
Copy link
Member

Does this help you? https://phpstan.org/r/95a7bcea-3198-4297-8770-823861adacfb

From: #7650

@zeleznypa
Copy link
Author

zeleznypa commented Jul 20, 2022

@ondrejmirtes I think that the message that there is no possibility to define what I'm expecting helps me.

Because what I need to say is, that the offsetGet and offsetSet can accept the key from the inside array or it can accept array of any keys/properties from the same inside array or any array/object in it.

I need to allow ['foo', 'bar', 'baz'] for the following inside structure:

$var = [
    'foo' => [
        'bar' => (object) [
             'baz' => 42
        ]
    ]
]

which is impossible to describe in @param without previous knowledge of the structure.

I'm closing the ticket as "impossible request" ;)

Thanks anyway for your time and patience

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

No branches or pull requests

3 participants