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

InvalidClass does not work PHP native classes and stubs #8456

Closed
kkmuffme opened this issue Sep 4, 2022 · 4 comments · Fixed by #8465
Closed

InvalidClass does not work PHP native classes and stubs #8456

kkmuffme opened this issue Sep 4, 2022 · 4 comments · Fixed by #8465
Assignees

Comments

@kkmuffme
Copy link
Contributor

kkmuffme commented Sep 4, 2022

https://psalm.dev/r/a2caa7560a

When class Hello is not in the project files but provided via stubs, it doesn't report InvalidClass error.
This is true for global (psalm provided) and custom (user/config provided) stubs.

e.g. DateTime:
https://psalm.dev/r/78f1f31d1b

Should give an InvalidClass error too

@psalm-github-bot
Copy link

I found these snippets:

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

class Hello {
    
}

/**
 * @param hello $arg
 * @return void
 */
function abc( $arg ) {
    
}
Psalm output (using commit afe85fa):

ERROR: InvalidClass - 8:11 - Class, interface or enum hello has wrong casing

INFO: UnusedParam - 11:15 - Param $arg is never referenced in this method
https://psalm.dev/r/78f1f31d1b
<?php

/**
 * @param dateTime $arg
 * @return void
 */
function abc( $arg ) {
    
}
Psalm output (using commit afe85fa):

INFO: UnusedParam - 7:15 - Param $arg is never referenced in this method

@kkmuffme kkmuffme changed the title InvalidClass does not work global classes and stubs InvalidClass does not work PHP native classes and stubs Sep 4, 2022
@kkmuffme
Copy link
Contributor Author

kkmuffme commented Sep 4, 2022

Issue is bc ClassLikes.php public function classHasCorrectCasing ironically uses strtolower on classlike_aliases:

if (isset($this->classlike_aliases[strtolower($fq_class_name)])) {

which is necessary to check if the class exists. But not sufficient to check here.

Afaik changing this:

if (isset($this->classlike_aliases[strtolower($fq_class_name)])) {
    return true;
}

to:

if (isset($this->classlike_aliases[strtolower($fq_class_name)]) && $this->classlike_aliases[strtolower($fq_class_name)] === $fq_class_name) {
    return true;
}

should be enough to fix this
(same for interface/enum in functions after it)

Additionally
if ($codebase->classlikes->isUserDefined(strtolower($aliased_name))) { before new InvalidClass should be removed too?

Would like some feedback on this, then can PR this

@AndrolGenhald
Copy link
Collaborator

Sounds good to me, go ahead and PR it and see if any tests fail!

@orklah
Copy link
Collaborator

orklah commented Sep 5, 2022

Beware, there is a lot of weirdness going on with stub dependencies.

Due to cache interaction and plugins, it is not clear whether the 'user-defined' part of a stub is defined.

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.

3 participants