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
PHPStan unresolvable type for intersection of final and MockObject #8901
Comments
This bug report is missing a link to reproduction at phpstan.org/try. It will most likely be closed after manual review. |
I have found many similar looking tickets, where was written: use bootstrap or autoload where I can run the but it not works. |
The bypass finals lib removes final when the autoloader loads the class AFAIK. Which is almost borderline magic/crazy IMO :) phpstan can't know that when it uses reflection. Is there no way to use a real instance of the final class I suppose? Or does it implement an interface you could use and mock instead? |
I have investigated, that issue is there, when I tried to access class that not exist. For example when I add After that, the PhpStan can't identify, that class https://phpstan.org/blog/solving-phpstan-error-unable-to-resolve-template-type but using |
@herndlm Until now, I have found many ugly way, how to workaround existence of |
I have also investigated, that this issue happen, when I have the folder (that contains the final class) in the like:
But when i put the folder in the
|
I'm not sure any more if I fully understood the issue tbh, but I would have also suggested to try out I also found #3179 now, which you found too I guess. So it sounds as the problem could be an autoloader race condition. E.g. something else might still be loading the final class earlier before bypass-finals is registered via phpstan (bootstrap or config). It might be hard to do, but a minimal reproducer would be really useful I guess. |
@herndlm the issue is based on I really like to use it... Yes, the issue is based on the topic, that class was loaded before the I have found at least 2 strange cases:
Now I'm facing another strange issue, but I can't reproduce it in another repository to make it public. When I add not used
How can number of array items have impact to loading te autoloader??? |
This doesn't make sense. You put things into The root issue of the behaviour you're seeing is because both https://github.com/dg/bypass-finals and PHPStan's https://github.com/phpstan/phpstan-src/blob/1.10.x/src/Reflection/BetterReflection/SourceLocator/AutoloadSourceLocator.php use the same hacky technique of overriding default This code https://github.com/phpstan/phpstan-src/blob/1.10.x/src/Reflection/BetterReflection/BetterReflectionSourceLocatorFactory.php and this documentation page https://phpstan.org/user-guide/discovering-symbols are closely related. Once searching for a symbol reaches AutoloadSourceLocator here (https://github.com/phpstan/phpstan-src/blob/28565ffe784f4bb244f0aa63ada2ecd03624df62/src/Reflection/BetterReflection/BetterReflectionSourceLocatorFactory.php#L132), the game is over for bypass-finals. This explains all the behaviour you're seeing. |
@ondrejmirtes And will it be possible to have such a feature in PHPStan like "Ignore finals"? Or if I understand the core of the problem well, when the |
I think you're much better served by refactoring your code in the way I described here: https://twitter.com/OndrejMirtes/status/1622311556531867651 You can also check other replies in the thread: https://twitter.com/matthieunapoli/status/1622258374837420032
Yeah, probably :) |
@ondrejmirtes Many thanks four your patience with this topic |
@ondrejmirtes Unfortunately it does not work. The builtin PHP reflection was fixed and return that the class is not final, but PHPStan still thinks that it is final :( |
Well, actually, they might, I solved it somehow because of the collaboration with Infection dg/bypass-finals@8151a24 |
But what if mine gets called first? If I implement the same fix then we end in infinite recursion. |
It shouldn't infinite loop, should it? Simply each handler, when initialized, remembers the previous one and calls it. |
I'm not really sure 🤷♂️ |
@ondrejmirtes, @dg to prevent the possible issue with infinite loop can help maybe this: https://gitlab.com/interitty/infinite-loop-prevention |
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
Because somebody use
final
in the third-party code that I need to use in my own and need to test integration with it,I have to use the dg/bypass-finals which allows me to use the PhpUnit.
Unfortunately the PHPStan detect the problem of using „not so final class“ together with
MockObject
and returns the following reportthat is not true, because It works.
Code snippet that reproduces the problem
Expected output
The issue was reported incorrectly
Did PHPStan help you today? Did it make you happy in any way?
Many times the PHPStan helps me. Thanks much.
The text was updated successfully, but these errors were encountered: