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

PHP 8 ::class | Round #2 #5671

Closed
simPod opened this issue Apr 26, 2021 · 3 comments · Fixed by #6823
Closed

PHP 8 ::class | Round #2 #5671

simPod opened this issue Apr 26, 2021 · 3 comments · Fixed by #6823

Comments

@simPod
Copy link
Contributor

simPod commented Apr 26, 2021

When using $object::class in switch statement, narrowing the type using case $child::class does not narrow it for psalm

get_class() still works fine.

https://psalm.dev/r/c8c3fadc13

crosslink for previous issue #5479

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/c8c3fadc13
<?php

abstract class A {}
class B extends A {}

function a(A $a): void {
    switch ($a::class) {
        case B::class:
            b($a);
    }
    
    switch (get_class($a)) {
        case B::class:
            b($a);
    }
}

function b(B $b): void {
}
Psalm output (using commit 698adef):

ERROR: ArgumentTypeCoercion - 9:15 - Argument 1 of b expects B, parent type A provided

INFO: UnusedParam - 18:14 - Param $b is never referenced in this method

@orklah
Copy link
Collaborator

orklah commented Nov 4, 2021

simplified: https://psalm.dev/r/03a8ed173c

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/03a8ed173c
<?php

abstract class A {}
class B extends A {}

function a(A $a): void {
    if($a::class == B::class) {
        b($a);
    }
}

function b(B $b): void {
}
Psalm output (using commit cd74f66):

ERROR: ArgumentTypeCoercion - 8:11 - Argument 1 of b expects B, parent type A provided

INFO: UnusedParam - 12:14 - Param $b is never referenced in this method

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

Successfully merging a pull request may close this issue.

2 participants