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 a class-string with class_exists results in mixed instead of expected string #10215

Closed
asgrim opened this issue Sep 18, 2023 · 1 comment · Fixed by #10262
Closed

Comments

@asgrim
Copy link

asgrim commented Sep 18, 2023

https://psalm.dev/r/c32c4d2cd8

We are narrowing a parameter of type class-string to determine if the class_exists or not; but if the class does NOT exist, them Psalm is telling me the type is mixed; however I would expect the type to be string. Not sure why this is happening?

@psalm-github-bot
Copy link

I found these snippets:

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

/** @param class-string $className */
function foo(string $className): void {
    /** @psalm-trace $className */
   if ( ! class_exists($className) ) {
       /** @psalm-trace $className */
       throw new \RuntimeException($className);
   }
}
Psalm output (using commit 7428e49):

ERROR: DocblockTypeContradiction - 6:11 - Docblock-defined type class-string for $className is always class-string

INFO: MixedArgument - 8:36 - Argument 1 of RuntimeException::__construct cannot be mixed, expecting string

INFO: Trace - 8:8 - $className: mixed

INFO: Trace - 6:4 - $className: class-string

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.

1 participant