Add explicit instance assert between nullable object assign and method call on nullable object (spotted by PHPStan)
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
public function test()
{
$someObject = $this->getSomeObject();
+ $this->assertInstanceOf(SomeClass::class, $someObject);
$value = $someObject->getSomeMethod();
}
private function getSomeObject(): ?SomeClass
{
if (mt_rand(0, 1)) {
return new SomeClass();
}
return null;
}
}
Configure your rector.php
:
<?php
use Rector\Config\RectorConfig;
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector;
return RectorConfig::configure()
->withRules([
AddInstanceofAssertForNullableInstanceRector::class,
]);