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

Narrowing down of an instance class in else branch doesn't work #11097

Open
SCIF opened this issue Sep 16, 2024 · 5 comments
Open

Narrowing down of an instance class in else branch doesn't work #11097

SCIF opened this issue Sep 16, 2024 · 5 comments

Comments

@SCIF
Copy link
Contributor

SCIF commented Sep 16, 2024

https://psalm.dev/r/29c4fb185d The trace on L9 should say stdClass

Copy link

I found these snippets:

https://psalm.dev/r/29c4fb185d
<?php

class A {}
/** @var A|stdClass $obj */;

if ($obj::class === A::class) {
    /** @psalm-trace $obj */;
} else {
    /** @psalm-trace $obj */;
}
Psalm output (using commit 16b24bd):

INFO: Trace - 7:29 - $obj: A

INFO: Trace - 9:29 - $obj: A|stdClass

@orklah
Copy link
Collaborator

orklah commented Sep 16, 2024

What about this?
https://psalm.dev/r/86cb22d9c3

B is an A but ::class on it won't === A::class so it will go in the else. That way, the else can't completely exclude A

Copy link

I found these snippets:

https://psalm.dev/r/86cb22d9c3
<?php

class A {}
class B extends A {}

/** @var A|stdClass $obj */;

if ($obj::class === A::class) {
    /** @psalm-trace $obj */;
} else {
    /** @psalm-trace $obj */;
}
Psalm output (using commit 16b24bd):

INFO: Trace - 9:29 - $obj: A

INFO: Trace - 11:29 - $obj: A|stdClass

@staabm
Copy link
Contributor

staabm commented Sep 18, 2024

It should work for final classes though

https://psalm.dev/r/85e60a375e

Copy link

I found these snippets:

https://psalm.dev/r/85e60a375e
<?php

final class A {}

/** @var A|stdClass $obj */;

if ($obj::class === A::class) {
    /** @psalm-trace $obj */;
} else {
    /** @psalm-trace $obj */;
}
Psalm output (using commit 16b24bd):

INFO: Trace - 8:29 - $obj: A

INFO: Trace - 10:29 - $obj: A|stdClass

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

No branches or pull requests

3 participants