-
-
Notifications
You must be signed in to change notification settings - Fork 863
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
DynamicMethodReturnTypeExtension does not seem to work for static method calls #71
Comments
You're not mising anything, it didn't occur to me yet that it should work
for static methods too 😊 I'll fix it for the next version and your code
will work.
Thank you for the report.
…On Sat, 7 Jan 2017 at 17:56, Andrea Sprega ***@***.***> wrote:
First of all, thanks for this amazing static analyzer.
Is it possible that DynamicMethodReturnTypeExtension does not work for
static method calls?
I am using mockery in my test code and this is the code for creating a
mock: \Mockery::mock(SomeClass::class). The return value of this is a
\Mockery\MockInterface which I'd like to also "union" with the mocked
type, in order to remove the errors PHPStan reports.
So, based on your example, I built this extension:
public static function getClass(): string
{
return \Mockery::class;
}
public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'mock';
}
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
if (count($methodCall->args) === 0) {
return $methodReflection->getReturnType();
}
$arg = $methodCall->args[0]->value;
$type = $scope->getType($arg);
if ($type->getClass() !== null) {
return $type;
}
return $methodReflection->getReturnType();
}
and registered it. I then executed phpstan over a file with plenty of
\Mockery::mock calls and I noticed only getClass() was being called.
PHPStan never asked for isMethodSupported from my extension, and the
errors are still there.
Is there anything I'm missing?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#71>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAGZuLWIj8exxZuvkJHETSsnqwgbe754ks5rP8PGgaJpZM4Ldd2R>
.
|
Implemented in e665b74. I actually had to add new interface I know that Feel free to test it if it works like you want it to. I will add it to the nearest 0.5.2 release which will be out soon. |
Hi @ondrejmirtes! Thanks for the very quick fix. I still have an issue and I'd like to understand whether it's my fault or not. Again, this is my extension:
and this is how I register it:
Now the extension method
I am sure $type, however, is always returned as an instance of Sorry if I wasn't clear enough, let me know if I can provide you with more info. Thanks! |
In your case, you need to write code if |
So it was my fault ;) For future reference, I changed the implementation to:
and now everything works flawlessly. Thanks a lot for your help, much appreciated! |
You're missing one if. Imagine this:
In that case, your mock would return |
Good catch :) thank you again! |
@asprega Actually, thanks to your original mistake, I realized that |
right, I did not realize it before (I'm still getting acquainted with PHPParser) but it makes total sense now. |
First of all, thanks for this amazing static analyzer.
Is it possible that
DynamicMethodReturnTypeExtension
does not work for static method calls?I am using mockery in my test code and this is the code for creating a mock:
\Mockery::mock(SomeClass::class)
. The return value of this is a\Mockery\MockInterface
which I'd like to also "union" with the mocked type, in order to remove the errors PHPStan reports.So, based on your example, I built this extension (NOTE: this does not yet provide the union type, but it should at least "convert" the mock type to the specific class type being mocked):
and registered it. I then executed phpstan over a file with plenty of
\Mockery::mock
calls and I noticed onlygetClass()
was being called. PHPStan never asked forisMethodSupported
from my extension, and the errors are still there. I suspect the extension system does not work with static method calls.Is there anything I'm missing?
The text was updated successfully, but these errors were encountered: