-
Notifications
You must be signed in to change notification settings - Fork 19
Return an intersection type when nameMock is used #19
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
Conversation
Hi, this isn't sufficient as the extension will try to make a type from the first argument too. |
Hm, I understand first argument also represents a type, essantually |
From the documentation I don't understand what the method does: http://docs.mockery.io/en/latest/cookbook/class_constants.html There are only classes involved, and in PHP you can't create intersection types from classes. |
When you mock something with Now suppose you need to create two mock objects of same interface.
If you do Using |
Yeah but if class name in the first argument isn't an existing one, we have to skip it when creating the intersection... |
Can you help me understand why? If we use How do you think we can fix this? |
I don't think that's true. |
<?php
// x.php
require_once __DIR__ . '/vendor/autoload.php';
$mock1 = Mockery::mock('WhaterverRandomNameIComeUpWith');
$mock2 = Mockery::mock('WhaterverRandomNameIComeUpWith');
var_dump(get_class($mock1));
var_dump(get_class($mock2)); php x.php
/Users/msipenko/src/msipenko/forks/phpstan-mockery/x.php:8:
string(41) "Mockery_0__WhaterverRandomNameIComeUpWith"
/Users/msipenko/src/msipenko/forks/phpstan-mockery/x.php:9:
string(41) "Mockery_0__WhaterverRandomNameIComeUpWith" |
Yeah but PHPStan won't like it: https://phpstan.org/r/31d8e42c-5d2c-420a-b7d0-8161f271e686 Is it a valid use-case? |
Ah ok, I understand now, strangely I edited the code locally in my projects vendor directory, and just adding that entry to array made PHPStan error go away. Either way, what do you suggest we can do here? |
There has to be another extension similar to MockDynamicReturnTypeExtension but that skips the first parameter. |
Meybe we can just add something like this? $args = $methodCall->args;
if ($methodReflection->getName() === 'namedMock') {
array_shift($args);
} |
Nope, it'd be a mess, this needs a separate class. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some code added to https://github.com/phpstan/phpstan-mockery/blob/master/tests/Mockery/MockeryTest.php to verify by running vendor/bin/phing phpstan
this actually works would be nice.
$defaultReturnType = new ObjectType('Mockery\\MockInterface'); | ||
|
||
$args = $methodCall->args; | ||
array_shift($args); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to make sure the array has at least two elements.
* master: Update incompatible dependency
@ondrejmirtes, any updates on this? |
Merged and released, thanks. |
Thank you! |
This will return intersetion type when
namedMock
function is used.Currently when
Mockery::namedMock()
is used within tests PHPStan complains that the mocked object does not conform to what is expected.