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
class_alias
not considered for inheritance checks in type hints
#8461
Comments
Hi, class aliases need to be registered the way how it's described in the documentation, then it's gonna work: https://phpstan.org/user-guide/discovering-symbols#class-aliases |
I already tried that out without success, but I now tested that out again more formally in an extra example repository with separate files for each class and composer autoloader. It now outputs a slightly different output than with the code snippet before. Instead of:
It now fails with:
Maybe the code snippet tool on phpstan.org isn't good to test this issue out, because there doesn't seem to be a way to provide additional files for bootstrapping code? Either way. Regardeless of providing the class aliasses with or without
|
@ondrejmirtes Can you reopen this issue or should I create a new issue? |
Yeah, I checked your example and it's really a bug, thanks. |
Friendly reminder that this issue is really nagging when creating compatibility. Imagine offering some nice tool to the world. This nice tool is using package "foo/bar:6.0" if you run PHP 8.2 . But it requires "foo/bar:5.0" when you use PHP 8.1 . Then you want to create compatibility for "foo/bar:5" and "foo/bar:6" to make everyone happy but ... |
I think I have this issue, I'm writing some "mocking" classes to allow phpstan to pass safely while I migrate a code base. I have the following snippet: // compatibility.php registered in bootstrapFiles
class A {}
class_alias('A', 'MyLib\\A');
// MyClass.php
class B extends MyLib\A {}
function returnsA(): MyLib\A
{
return new B(); // PHPStan error: Method returnsA() should return A but returns B
} The above error is invalid as I fail to reproduce this on the playground as I cannot specify the If this is not the same issue as this one, I can open a new issue. Thank you for this awesome tool ! |
I've looked at it again and this issue seems to be fixed now for me. Not the example in the playground that I provided, but when providing the class alias in According to my testing (commit) it is working since version 1.10.39 that released on I also locally tried out the example from @homersimpsons with different phpstan versions and it also seems to be fixed since version |
Thanks for that digging @Istador, I'm on |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Bug report
I'm using
class_alias
to replace an old implementation in the global namespace (in a bigger older library) with a new and improved implementation from a custom namespace (as its own optional dependency to replace the one in the library). The old global class name is still in use at a lot of places, e.g. in type hints (in other classes in the library and in the application).Extending the new class and passing an object of it into methods that uses the old class as parameter type hints works at least since PHP
7.3
(I haven't tested earlier versions), but PHPStan wrongly reports this as an error (tested with1.8.0
and1.9.2
).Code snippet that reproduces the problem
https://phpstan.org/r/0e405105-3752-4e55-be7c-77258e2d15a4
Expected output
The expected output is
int(2)
and not to produce an error, but PHPStan with level 5 or higher fails at the last line:Deprecated
is an alias forLatest
andLatestExtension
is a sub-class ofLatest
, therefore providing an argument of typeLatestExtension
should satisfyDeprecated
.Did PHPStan help you today? Did it make you happy in any way?
It helps to find bugs and programming misconceptions before reaching production. It's a great tool to use in CI/CD pipelines as well as locally.
The text was updated successfully, but these errors were encountered: