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

Add support for ::class type inference #5479

Closed
simPod opened this issue Mar 25, 2021 · 3 comments · Fixed by #5484
Closed

Add support for ::class type inference #5479

simPod opened this issue Mar 25, 2021 · 3 comments · Fixed by #5484

Comments

@simPod
Copy link
Contributor

simPod commented Mar 25, 2021

Calling $class = get_class($obj); for $obj of type T correctly resolves $class to class-string<T>.

But $class = $obj::class resolves $class to mixed

https://psalm.dev/r/57434e1f10

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/57434e1f10
<?php

/**
 * @template T of object
 * @psalm-param T $obj
 */
function a(object $obj) {
    $class = get_class($obj);
    $class2 = $obj::class;
    
    /** @psalm-trace $class */
    /** @psalm-trace $class2 */
}
Psalm output (using commit b627962):

INFO: MixedAssignment - 9:5 - Unable to determine the type that $class2 is being assigned to

INFO: Trace - 12:0 - $class2: mixed

INFO: UnusedVariable - 8:5 - $class is never referenced or the value is not used

INFO: UnusedVariable - 9:5 - $class2 is never referenced or the value is not used

INFO: MissingReturnType - 7:10 - Method a does not have a return type, expecting void

@AndrolGenhald
Copy link
Collaborator

Ideally support would be conditioned on PHP 8, and previous versions would show an error: https://3v4l.org/ZCj6L

@simPod
Copy link
Contributor Author

simPod commented Mar 25, 2021

It's PHP8+ syntax.

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

Successfully merging a pull request may close this issue.

2 participants