psalm
psalm copied to clipboard
Accessing different nested object properties trigger MixedPropertyFetch
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.
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
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.
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
I have the exact same issue.