phpspec / prophecy

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

Improve auto IDE support of reveal #448

Closed mpoiriert closed 1 year ago

mpoiriert commented 4 years ago

I am using PHPStorm and using prophecy (via PHPUnit) cause a lot of warning because on wrong argument when using the reveal return value directly.

What I end up doing it this:


$prophecy = $this->prophesize(Connection::class);
// ...
/** @var Connection $connection */
$connection = $prophecy->reveal();
$object = new Object($connection);

To do this a bit "cleaner" a and using the phpstorm.meta this could be change to


$prophecy = $this->prophesize(Connection::class);
// ...
$object = new Object($ $prophecy->reveal(Connection::class));

This work but I have a warning because the reveal method doesn't have any argument.

Adding a file .phpstorm.meta.php with this would solve that

<?php namespace PHPSTORM_META {

    $STATIC_METHOD_TYPES = [
        \Prophecy\Prophecy\ObjectProphecy::reveal('') => [
            "" == "@",
        ],
    ];
}

namespace Prophecy\Prophecy {
    class ObjectProphecy
    {
        public function reveal(string $class = null)
        {

        }
    }
}
sstok commented 4 years ago

Given you use PHPUnit, this plug-in might help you : https://plugins.jetbrains.com/plugin/9674-phpunit-enhancement/ 👍

ciaranmcnulty commented 4 years ago

@mpoiriert I don't know how phpstorm does this, you're saying we can add this to our project and it'll be found in the vendor folder?

mpoiriert commented 4 years ago

@ciaranmcnulty Yes exactly. I tested it by putting this file manually in the vendor/phpspec/prophecy direcoty and it worked.

You can read about it here https://blog.jetbrains.com/phpstorm/2019/02/new-phpstorm-meta-php-features/

But the plugin that @sstok mention is working great. So it might be better to recommend it on the ream me documentation instead of adding the file...

danepowell commented 1 year ago

PhpStorm handles Prophecy objects natively now (pretty well, not perfectly). I'm not sure what value that plugin adds. Maybe this issue can be closed.

mpoiriert commented 1 year ago

No body seems to care and I am not using prophecy anymore so I will close it.

stof commented 1 year ago

We indeed solved this by using generics, which makes it work for any static analysis tool supporting them instead of only solving it for phpstorm.