Add better handling for willImplement and willExtend call to fix issues with PHPStan 2.1.3 and 2.1.4#366
Add better handling for willImplement and willExtend call to fix issues with PHPStan 2.1.3 and 2.1.4#366alexander-schranz wants to merge 17 commits intomasterfrom
Conversation
|
Do we even need this dynamic return type extension at all ? Prophecy defines the return type of those methods: https://github.com/phpspec/prophecy/blob/53ab8cee442a4e9f9827aaec92b023af841783c5/src/Prophecy/Prophecy/ObjectProphecy.php#L70-L72 I think the improvement released in phpstan 2.1.3 and 1.12.17 might make it work out of the box. |
|
@stof Thx for the fast response. Looking at the code I would have expected the same but the result looks unexpected, it returns some strange return type without the extension. See #367 ( same as this branch but without the extension ) @stof Maybe still related to your issue here: phpstan/phpstan#8439 |
|
@stof The |
|
this looks like a bug in phpstan. It seems to resolve |
|
@stof @ondrejmirtes I tried to put all in a small case on phpstan playground. Think this should be the current state without the extension: https://phpstan.org/r/ccb8b2d4-fc46-4138-a4fd-a93548b52d18 Replace |
|
@ondrejmirtes @stof Best Result seems to be use Update using With some hacks ( |
|
Alright, what I figured out is that the extension WillExtendOrImplementDynamicReturnTypeExtension can be greatly simplified to: public function getTypeFromMethodCall(
Reflection\MethodReflection $methodReflection,
Node\Expr\MethodCall $methodCall,
Analyser\Scope $scope
): ?Type\Type {
$args = $methodCall->getArgs();
if (0 === \count($args)) {
return null;
}
$calledOnType = $scope->getType($methodCall->var);
$templateObjectType = $calledOnType->getTemplateType(Prophecy\ObjectProphecy::class, 'T');
$objects = [$templateObjectType];
$argumentType = $scope->getType($args[0]->value);
$objects[] = $argumentType->getClassStringObjectType();
return new Type\Generic\GenericObjectType(
Prophecy\ObjectProphecy::class,
[
Type\TypeCombinator::intersect(...$objects),
],
);
}No need to make it more complicated than that. But the build still fails with that. I figured out that when I remove So the bug is in PHPStan and it's about not properly supporting |
|
@ondrejmirtes thx, let me know If I can test something out. |
|
@ondrejmirtes see phpstan/phpstan#12575 where I reported an issue when reproducing this without a type extension |
|
Do we even need this extension at all or are the stubs (replacing the prophecy annotations to avoid using |
| * This stubs should be able to be removed when the following bug fixed: | ||
| * - https://github.com/Jan0707/phpstan-prophecy/pull/366#issuecomment-2650390076 | ||
| * - https://github.com/phpstan/phpstan/issues/12575 | ||
| * |
There was a problem hiding this comment.
This stubs replace static with ObjectProphecy which seems resolve correctly but we could remove it if this issue is fixed on phpstan side.
There was a problem hiding this comment.
I will look whether I can fix that in Prophecy itself. I used static<...> in prophecy because ObjectProphecy is not final, but I'm not sure we actually need this (I don't think there is a meaningful way to extend the class and use a child class anyway)
|
@stof you are right my current stubs would be enough. Only with the extension the call |
| @@ -13,11 +13,6 @@ | |||
| "php": "^7.4 || ^8.0", | |||
| "phpstan/phpstan": "^2.0.0" | |||
There was a problem hiding this comment.
should be bumped to 2.1.3 as min version as it relies on the phpstan changes
| * This stubs should be able to be removed when the following bug fixed: | ||
| * - https://github.com/Jan0707/phpstan-prophecy/pull/366#issuecomment-2650390076 | ||
| * - https://github.com/phpstan/phpstan/issues/12575 | ||
| * |
There was a problem hiding this comment.
I will look whether I can fix that in Prophecy itself. I used static<...> in prophecy because ObjectProphecy is not final, but I'm not sure we actually need this (I don't think there is a meaningful way to extend the class and use a child class anyway)
|
See #369 |
|
replaced by: #369 |
This pull request is based on discussion: phpstan/phpstan#12556 about new coming issue #365
Follows #.
Related to phpstan/phpstan#12556
Fixes #365
I still not get it to work 100%:
Why the following works:
it fails on direct assigning it:
/cc @ondrejmirtes
/cc @stof maybe you also have some ideas?