phpspec / prophecy

Highly opinionated mocking framework for PHP 5.3+
MIT License
8.53k stars 240 forks source link

Feature Request: PHPDoc generics typehints #544

Closed tyteen4a03 closed 1 year ago

tyteen4a03 commented 2 years ago

It would be amazing if we could typehint our prophecies with Generics phpdoc:


/**
 * @var ObjectProphecy<ClassBeingMocked>
 */
private $prophecy;
ciaranmcnulty commented 2 years ago

This sounds like a psalm/phpstan/jetbrains feature request...

tyteen4a03 commented 2 years ago

There needs to be @template annotations in place on Prophecy's side.

stof commented 2 years ago

For phpstan, you can use https://packagist.org/packages/jangregor/phpstan-prophecy which provides the stubs for the generics, and also extra extensions for better support of the magic calls. Due to our signature based on magic calls (and the additional extra magic of Argument::cetera()), I don't think we can make our code statically analysable based only on phpdoc, without relying on a phpstan extension.

gnutix commented 1 year ago

But adding @template on ObjectProphecy would at the very least allow PHPStorm (and maybe other IDEs) to consider the types too, and not raise inspections everywhere, no ?

Or is there a way to point the stub files in the extension (which I'm using) to PHPStorm so it reads it ?

stof commented 1 year ago

With @phpstan-this-out being implemented in phpstan 1.9.0, it becomes possible to represent the behavior of willImplement() in phpdoc. The phpstan extension will still be necessary to support our API based on magic calls.

ciaranmcnulty commented 1 year ago

I think we can/should import some subset of the types from those stubs, then users would get some benefit from them even without the extension (which probably would still be needed)