psalm icon indicating copy to clipboard operation
psalm copied to clipboard

Accessing different nested object properties trigger MixedPropertyFetch

Open annuh opened this issue 1 year ago • 4 comments

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.

annuh avatar Nov 16 '23 10:11 annuh

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

psalm-github-bot[bot] avatar Nov 16 '23 10:11 psalm-github-bot[bot]

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.

theodorejb avatar Jan 10 '24 16:01 theodorejb

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

psalm-github-bot[bot] avatar Jan 10 '24 16:01 psalm-github-bot[bot]

I have the exact same issue.

rzvc avatar Feb 15 '24 10:02 rzvc