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

Intersection type for mocks? #1

Open
jaydiablo opened this issue Apr 10, 2019 · 2 comments
Open

Intersection type for mocks? #1

jaydiablo opened this issue Apr 10, 2019 · 2 comments

Comments

@jaydiablo
Copy link
Contributor

I have a project that uses Mockery, and have both phpstan and psalm running against it.

I'm using this plugin and the phpstan mockery plugin (https://github.com/phpstan/phpstan-mockery) to help reduce the number of errors that have to ignored and/or worked around.

One thing that the phpstan plugin does, that this plugin doesn't seem to, is treating the Mockery\MockInterface as an intersection type when defined with another object:

Interprets Foo|\Mockery\MockInterface in phpDoc so that it results in an intersection type instead of a union type.

This allows me to do something like this:

/** @var User|\Mockery\MockInterface $user */
        $user = Mockery::mock(
            'User[oneMethod,twoMethod]',
            $constructorParams
        );

When defining mocks, and then the methods available on both User and \Mockery\MockInterface are available. So when the Mock is later used like so:

$user->shouldReceive('oneMethod')->zeroOrMoreTimes();

phpstan won't complain that the User class doesn't have the shouldReceive method. Likewise, this code also won't trigger an error:

$user->oneMethod()

Since the User object does have this method (but \Mockery\MockInterface) does not.

Is this something that could/should be added to this plugin? I can try to take a stab at it if so...

@jaydiablo
Copy link
Contributor Author

After poking around some, I was able to get the plugin to work fine with the Object[method1,method2] syntax of Mockery::mock, as long as the @var User|\Mockery\MockInterface doc block is removed (which seems fine to me, though the phpstan plugin would need to be updated to support that syntax as well, but that's a different issue).

I believe I can override the @var with a @psalm-var though and define it as User&\Mockery\MockeryInterface which seems to make both psalm and phpstan happy in this case. Ideally it would be nice to not need the @var or @psalm-var at all, but I think I'll have to submit a PR to the phpstan mockery plugin project to make that happen.

I'll put together a PR for what I've done for this plugin to accept that Mockery mock syntax though.

@weirdan
Copy link
Member

weirdan commented Jun 29, 2019

Isn't this solved already in the linked (merged) PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants