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

Psalm don't determine type of object after instanceof with class-string<CLASS> #6499

Closed
vjik opened this issue Sep 17, 2021 · 3 comments · Fixed by #6503
Closed

Psalm don't determine type of object after instanceof with class-string<CLASS> #6499

vjik opened this issue Sep 17, 2021 · 3 comments · Fixed by #6503
Labels

Comments

@vjik
Copy link
Contributor

vjik commented Sep 17, 2021

https://psalm.dev/r/7e3853aa3c

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/7e3853aa3c
<?php

class X {
    public function run(): void {
    }
}

final class Y {
    /**
     * @psalm-param class-string<X> $class
     */
    public function __construct(
        private string $class,
    ) {}
    
    public function go(object $object): void {
        if (!$object instanceof $this->class) {
            throw new Exception;
        }
		$object->run();
    }
}
Psalm output (using commit b857d0b):

INFO: MixedMethodCall - 20:12 - Cannot determine the type of $object when calling method run

@orklah
Copy link
Collaborator

orklah commented Sep 17, 2021

simpler reproducer:
https://psalm.dev/r/e11cdd8900

@psalm-github-bot
Copy link

I found these snippets:

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

final class Y {
    /**
     * @psalm-var class-string<stdClass> $class
     */
    private string $class;
    
    public function go(object $object): void {
        $a = $this->class;
        /** @psalm-trace $a */;
        if ($object instanceof $a) {
            /** @psalm-trace $object */; // doesn't works
        }
        
        $b = stdClass::class;
        /** @psalm-trace $b */;
        if ($object instanceof $b) {
            /** @psalm-trace $object */; // works
        }
    }
}
Psalm output (using commit b857d0b):

INFO: Trace - 11:31 - $a: class-string<stdClass>

INFO: Trace - 13:40 - $object: object

INFO: Trace - 17:31 - $b: stdClass::class

INFO: Trace - 19:40 - $object: stdClass

ERROR: MissingConstructor - 7:20 - Y has an uninitialized property Y::$class, but no constructor

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

Successfully merging a pull request may close this issue.

2 participants