-
Notifications
You must be signed in to change notification settings - Fork 30
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
Return types on mocked methods cause static analysis failures #23
Comments
FWIW, as a workaround I have: parameters:
ignoreErrors:
- '#Cannot call method (?:willReturn|shouldBeCalledOnce|shouldNotBeCalled|shouldBeCalled|shouldNotHaveBeenCalled)\(\) on .*\.#' |
The point is that with the annotation, you lost the special dynamic type provided by this extension. I personally dislike using class properties to store mocks, and this is one of the reasons. |
Ya, but if you annotate without
Doesn't work either way 😞 |
Another related case, if the mocked method is marked as
Which is a little more difficult to suppress only for the Prophecy-specific use cases (as the regex may match something where we really ARE using a |
something->willReturn()
cannot be called
Technically, the prophecy is not an instance of |
Ah interesting, yes indeed that actually fixes the problem, and that makes a lot of sense. The extension works on |
Shouldn’t it be an intersection type instead of union? (Btw an extension can be written so that Foo|ObjectProphecy is also implemented as intersection because that’s usully what people mean.) |
Yes, you're right. |
Hmm, seems that doesn't work for everything: https://phpstan.org/r/0175d49a784ee8f429587b4295093e7b Interfaces don't like being intersected for some reason:
|
@asgrim that's an incomplete example, that wouldn't work without Prophecy and the exension. Intersection means that the object must implement both interfaces/class, so the only way to have it is to use a proper prophesized object. |
Indeed; https://twitter.com/JanTvrdik/status/1057601523524530176 Jan explained this because of the way phpstan resolves my example. The extension doesn't make any difference at all here. I think the correct annotation is |
Yes, indeed that seems to work better 👍 Thanks folks! |
Wait, we do not have generics yet! 😜 |
I think it needs at least |
Nope, the There's an open PR #19 that makes |
Hmm, why does it work already then? O_O (using phpstan 0.10.5 and phpstan-prophecy 0.2.0).... /me confused |
My tip is that it makes it an ErrorType which is basically mixed. There’s yet no rule for checking unresolvable property types... |
Can confirm |
A method being mocked might be called like:
Even if
$mockedClass
is annotated correctly, such as:someMethod()
for example might return anint
, so you'd get a warning from phpstan like:Not sure if this is even possible to handle really, but thought I'd at least bring it up for discussion! Thanks!
The text was updated successfully, but these errors were encountered: