Closed AlexandruGG closed 2 years ago
Support for this new return type has not been added yet in Prophecy.
However, I'm not quite sure how to implement that properly regarding the value actually returned by the double (when Prophecy guesses the returned value because it is not set explicitly).
I think it only needs to be handled the same way void
is here:
https://github.com/phpspec/prophecy/blob/master/src/Prophecy/Doubler/Generator/Node/ReturnTypeNode.php#L11-L13
There's no need to resolve the class, as this is valid:
class A {
function foo() : static {}
}
class B extends A {
function foo() : static {}
}
@AlexandruGG do you want to make this change? We'd add some tests too https://github.com/phpspec/prophecy/blob/master/spec/Prophecy/Doubler/Generator/Node/ReturnTypeNodeSpec.php#L108-L120
Oh hm you're right @stof that is tricky
@ciaranmcnulty generating the right signature is easy. But generating the right behavior is not.
My own opinion is that Prophecy should drop all that logic, and expect all calls to be explicitly configured (which solves that case for static
too, as it is then the responsibility of the dev to specify either ->willReturnSelf()
or another valid return value returning another double instance). #526 is already bringing back the immediate UnexpectedCallException
. My suggestion would then only mean removing the special case of an ObjectProphecy without anything configured in it. Today, it accepts any call (which stops as soon as you configure one). In my proposal, it would expect having no calls being done on it. I think that this is much less surprising, as Prophecy cannot properly determine what the call should return anyway.
In the short term (+ in prophecy 1.x) we can have a special case for static
and throw an UnexpectedCallException
maybe
I'm hitting this as well..
Any workaround besides dropping the use of the static return type in my code?
I'm hitting this as well..
Any workaround besides dropping the use of the static return type in my code?
@RobinHoutevelts this is the workaround I'm using for now. Works with static analysers like PHPStan and Psalm:
/**
* @return static
*/
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
https://github.com/irontec/prophecy/commit/2d17db5545a52cbfdcddcb7fed0952d8d2dc086f kinda did it for me as wel.
Not sure what impact it has but at least my tests are green.
Thanks for the help!
The fix from irontec@2d17db5 works. Could this be merged?
Well, nobody has submitted a PR with a fix yet
Yeah we don't scour forks for fixes; if someone can open a PR and write a test to go with it we can consider merging
Here is fix of the issue I've checked on my project https://github.com/phpspec/prophecy/pull/545
Hello, I'm seeing a fatal error trying to use Prophecy with PHPUnit in a Symfony 5 project running PHP 8 with the new
static
return type. Here is the error:And a simplified version of the Entity I'm trying to create a prophecy for:
Is this a known issue with PHP 8? Apologies if so but I couldn't find any mention of it.
Or am I doing something wrong with this usage?
Thanks