phpstan-prophecy icon indicating copy to clipboard operation
phpstan-prophecy copied to clipboard

The MethodsClassReflectionExtension should restrict the available methods based on the doubled type

Open stof opened this issue 9 months ago • 1 comments

ClassReflection::getActiveTemplateTypeMap allows to access the template type. This would allow restricting hasMethod to the methods available in the doubled type.

Once we know the doubled type, we might also inspect the original method signature in the MethodReflection to define the appropriate signature variants:

  • create a variant where each original argument has a type being the union of the original type (referenced as U), its prophecy type (ProphecyInterface<U&object>) and Prophecy\Argument\Token\TokenInterface
  • create variants removing the last N mandatory arguments (and all optional arguments after them) and changing the type of the last argument to be \Prophecy\Argument\Token\AnyValuesToken, to allow the usage of Argument::cetera()

This would allow phpstan to report cases where your test tries to configure a call with the wrong method name or wrong arguments for it.

stof avatar Feb 11 '25 12:02 stof

To go even further, it would be nice after that to make MethodProphecy generic based on the expected return type to detect bad configuration of willReturn, but this might be harder.

stof avatar Feb 11 '25 12:02 stof