We have a big codebase in @sulu and lots of our testcases are not really analyzeable by phpstan as there are lot of false typehints for mocks (we have a very very big baseline). We are using prophecy
for mocking. And there are different false typehints in our codebase:
Variant A (class but didnt define mock):
class MyClassTest extends TestCase {
/**
* @var MyClass
*/
private $my;
public function setUp(): void
{
$this->my = $this->prophesize(MyClass::class);
}
}
Variant B (mock but didnt define class):
class MyClassTest extends TestCase {
/**
* @var ObjectProphecy
*/
private $my;
public function setUp(): void
{
$this->my = $this->prophesize(MyClass::class);
}
}
Variant C (mock or class (is still false but works sometimes)):
class MyClassTest extends TestCase {
/**
* @var ObjectProphecy|MyClass
*/
private $my;
public function setUp(): void
{
$this->my = $this->prophesize(MyClass::class);
}
}
Variant D (nothing defined):
class MyClassTest extends TestCase {
private $my;
public function setUp(): void
{
$this->my = $this->prophesize(MyClass::class);
}
}
Expected would be ObjectProphecy Generic with the class:
class MyClassTest extends TestCase {
/**
* @var ObjectProphecy<MyClass>
*/
private $my;
public function setUp(): void
{
$this->my = $this->prophesize(MyClass::class);
}
}
So I asked myself if rector could be used to provide a rule to analyze all $this->prophesize
calls and set the correct type for this classes. Think it would be a great rule which could be shipped with rector via a CodeQuality ruleset. Any hints are welcome how to achieve this :)