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

Regression in 4.24.0: casting array to object does not understand that the result is stdClass anymore #8440

Open
ArtemGoutsoul opened this issue Aug 29, 2022 · 3 comments

Comments

@ArtemGoutsoul
Copy link
Contributor

The following code (https://psalm.dev/r/45cf3afbc3):

function test(stdClass $a):void {
    print_r($a);
}

test((object)['a' => 1]);

started producing an error:

ERROR: [ArgumentTypeCoercion](https://psalm.dev/193) - 7:6 - Argument 1 of test expects stdClass, parent type object{a:1} provided

Which is funny, cause the actual output is:

stdClass Object
(
    [a] => 1
)

This did not happen in psalm 4.23.0, but happens in 4.26.0

My guess is that the regression happened in 4.24.0 and possibly caused by #7935

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/45cf3afbc3
<?php

function test(stdClass $a):void {
    print_r($a);
}

test((object)['a' => 1]);

/* Output:

stdClass Object
(
    [a] => 1
)

*/
Psalm output (using commit afe85fa):

ERROR: ArgumentTypeCoercion - 7:6 - Argument 1 of test expects stdClass, parent type object{a:1} provided

@orklah
Copy link
Collaborator

orklah commented Aug 29, 2022

You're right, it is in some way a regression.

Right now, Psalm's type system cannot store those two informations at the same time:

  • class name
  • properties names/values

For known object with known properties, the class name is enough to describe the whole object, however, for dynamic object where properties are not known, this is an issue.

Psalm's type system could probably be enhanced in order to store both informations but if we have to choose, I prefer keeping the current behaviour (it's better to be able to track property name and values instead of tracking the class name for stdClass, cases where we specifically want to only allow stdClass and no other object are rare I think).

Note: phpstan doesn't seem to support this syntax at all: https://phpstan.org/r/88fe1734-e4b6-4338-86e2-ee4ed4b68057

@AndrolGenhald
Copy link
Collaborator

The plan is to eventually support this by using intersections, but Psalm's intersection support really needs rebuilt from the ground up for this to work, and then a significant portion of the analysis needs to be updated to correctly account for intersections.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants