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

Accessing different nested object properties trigger MixedPropertyFetch #10380

Open
annuh opened this issue Nov 16, 2023 · 4 comments · May be fixed by #11156
Open

Accessing different nested object properties trigger MixedPropertyFetch #10380

annuh opened this issue Nov 16, 2023 · 4 comments · May be fixed by #11156

Comments

@annuh
Copy link

annuh commented Nov 16, 2023

Hi,

I've noticed an issue when accessing multiple nested object properties.

Hopefully this snippet explains the issue:

<?php
/**
 * @var object{
 *     Nested: object{
 *         foo: string,
 *         bar: string,
 *    }
 * } $object
 */
if ($object->Nested->foo !== 'Foo') {
    return null;
}
if ($object->Nested->bar !== 'Bar') { // the MixedPropertyFetch is unexpected here
    return null;
}

Link to the snippet https://psalm.dev/r/4ec39c5d44

FYI this only happens with nested objects, accessing multiple properties directly on $response works as expected.

Copy link

I found these snippets:

https://psalm.dev/r/4ec39c5d44
<?php
/**
 * @var object{
 *     Nested: object{
 *         foo: string,
 *         bar: string,
 *    }
 * } $object
 */
if ($object->Nested->foo !== 'Foo') {
    return null;
}
if ($object->Nested->bar !== 'Bar') {
    return null;
}
Psalm output (using commit 5095f4e):

INFO: MixedPropertyFetch - 13:5 - Cannot fetch property on mixed var $object->Nested

@annuh annuh changed the title Multiple nested object property fetch trigger "Cannot fetch property on mixed var $object->Nested" Accessing different nested object properties trigger MixedPropertyFetch Nov 16, 2023
@theodorejb
Copy link
Contributor

I ran into a similar issue with nested objects when throwing an exception: https://psalm.dev/r/f7ffc1d343

If I comment out the throw new Exception line, then the MixedPropertyFetch error goes away.

Copy link

I found these snippets:

https://psalm.dev/r/f7ffc1d343
<?php
/** @var object{Wrapper: object{
 *     Header: object{UID: string, Status: string}
 * }} $resp
 */
$result = $resp->Wrapper;

if ($result->Header->Status !== "OK") {
    throw new Exception("Response failed");
}

return $result->Header->UID;
Psalm output (using commit a75d26a):

INFO: MixedPropertyFetch - 12:8 - Cannot fetch property on mixed var $result->Header

@rzvc
Copy link

rzvc commented Feb 15, 2024

I have the exact same issue.

@MoonE MoonE linked a pull request Nov 12, 2024 that will close this issue
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 a pull request may close this issue.

3 participants