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

Different output for get_class and ::class #9607

Open
franmomu opened this issue Apr 4, 2023 · 2 comments
Open

Different output for get_class and ::class #9607

franmomu opened this issue Apr 4, 2023 · 2 comments

Comments

@franmomu
Copy link
Contributor

franmomu commented Apr 4, 2023

Apparently it's the same, but when the result is used in a call, they behave different:

https://psalm.dev/r/23928709e1

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/23928709e1
<?php

declare(strict_types=1);

interface ObjectManager
{
    /**
     * @template T of object
     * @param class-string<T> $className
     * @return T|null
     */
    public function find(string $className): ?object;
}

function test(ObjectManager $dm): void {
    $obj = new \stdClass();

    /** @psalm-trace $class1 */
    $class1 = get_class($obj);
    
    /** @psalm-trace $class2 */
    $class2 = $obj::class;

    /** @psalm-trace $result1 */
    $result1 = $dm->find($class1);
    
    /** @psalm-trace $result2 */
    $result2 = $dm->find($class2);
    
}
Psalm output (using commit 1f72597):

INFO: Trace - 19:5 - $class1: class-string<stdClass>

INFO: Trace - 22:5 - $class2: class-string<stdClass>

INFO: Trace - 25:5 - $result1: null|object

INFO: Trace - 28:5 - $result2: null|stdClass

INFO: UnusedVariable - 25:5 - $result1 is never referenced or the value is not used

INFO: UnusedVariable - 28:5 - $result2 is never referenced or the value is not used

@orklah
Copy link
Collaborator

orklah commented Apr 4, 2023

You're right, get_class is internally handled by TDependentGetClass while ::class creates a TLiteralClassString. I don't really remember why those two are different though.

Seems like one is handled on template resolution but not the other

franmomu added a commit to franmomu/mongodb-odm that referenced this issue Apr 5, 2023
These new errors are because a bug in psalm with get_class
see vimeo/psalm#9607, they will be
gone once we use ::class
franmomu added a commit to franmomu/mongodb-odm that referenced this issue Apr 5, 2023
These new errors are because a bug in psalm with get_class
see vimeo/psalm#9607, they will be
gone once we use ::class
franmomu added a commit to franmomu/mongodb-odm that referenced this issue Apr 5, 2023
These new errors are because a bug in psalm with get_class
see vimeo/psalm#9607, they will be
gone once we use ::class
franmomu added a commit to franmomu/mongodb-odm that referenced this issue Apr 5, 2023
These new errors are because a bug in psalm with get_class
see vimeo/psalm#9607, they will be
gone once we use ::class
franmomu added a commit to doctrine/mongodb-odm that referenced this issue Apr 5, 2023
* Fix return type

* Fix psalm build

These new errors are because a bug in psalm with get_class
see vimeo/psalm#9607, they will be
gone once we use ::class
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

2 participants