Updated Rector to commit 576198cf701684dd85ed42530bcb8c54b6c41a02

576198cf70 [CodeQuality] Transform type|void docblock to type|null as well on ExplicitReturnNullRector after add return null (#5756)
This commit is contained in:
Tomas Votruba 2024-03-22 17:50:28 +00:00
parent dd51f7ceed
commit ce9cc3aafc
3 changed files with 58 additions and 3 deletions

View File

@ -558,6 +558,10 @@ Add explicit return null to method/function that returns a value, but missed mai
```diff
class SomeClass
{
/**
- * @return string|void
+ * @return string|null
*/
public function run(int $number)
{
if ($number > 50) {

View File

@ -11,6 +11,12 @@ use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Throw_;
use PHPStan\Type\NullType;
use PHPStan\Type\UnionType;
use PHPStan\Type\VoidType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
use Rector\TypeDeclaration\TypeInferer\SilentVoidResolver;
@ -31,16 +37,37 @@ final class ExplicitReturnNullRector extends AbstractRector
* @var \Rector\TypeDeclaration\TypeInferer\SilentVoidResolver
*/
private $silentVoidResolver;
public function __construct(BetterNodeFinder $betterNodeFinder, SilentVoidResolver $silentVoidResolver)
/**
* @readonly
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
*/
private $phpDocInfoFactory;
/**
* @readonly
* @var \Rector\NodeTypeResolver\PHPStan\Type\TypeFactory
*/
private $typeFactory;
/**
* @readonly
* @var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger
*/
private $phpDocTypeChanger;
public function __construct(BetterNodeFinder $betterNodeFinder, SilentVoidResolver $silentVoidResolver, PhpDocInfoFactory $phpDocInfoFactory, TypeFactory $typeFactory, PhpDocTypeChanger $phpDocTypeChanger)
{
$this->betterNodeFinder = $betterNodeFinder;
$this->silentVoidResolver = $silentVoidResolver;
$this->phpDocInfoFactory = $phpDocInfoFactory;
$this->typeFactory = $typeFactory;
$this->phpDocTypeChanger = $phpDocTypeChanger;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Add explicit return null to method/function that returns a value, but missed main return', [new CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
/**
* @return string|void
*/
public function run(int $number)
{
if ($number > 50) {
@ -52,6 +79,9 @@ CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
/**
* @return string|null
*/
public function run(int $number)
{
if ($number > 50) {
@ -91,8 +121,29 @@ CODE_SAMPLE
return null;
}
$node->stmts[] = new Return_(new ConstFetch(new Name('null')));
$this->transformDocUnionVoidToUnionNull($node);
return $node;
}
private function transformDocUnionVoidToUnionNull(ClassMethod $classMethod) : void
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod);
$returnType = $phpDocInfo->getReturnType();
if (!$returnType instanceof UnionType) {
return;
}
$newTypes = [];
foreach ($returnType->getTypes() as $type) {
if ($type instanceof VoidType) {
$type = new NullType();
}
$newTypes[] = $type;
}
$type = $this->typeFactory->createMixedPassedOrUnionTypeAndKeepConstant($newTypes);
if (!$type instanceof UnionType) {
return;
}
$this->phpDocTypeChanger->changeReturnType($classMethod, $phpDocInfo, $type);
}
private function containsYieldOrThrow(ClassMethod $classMethod) : bool
{
return (bool) $this->betterNodeFinder->findInstancesOf($classMethod, [Yield_::class, Throw_::class, Node\Expr\Throw_::class, YieldFrom::class]);

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '32bf5c7e7a8da2100f12cbef185c958655d4292d';
public const PACKAGE_VERSION = '576198cf701684dd85ed42530bcb8c54b6c41a02';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-03-22 23:51:21';
public const RELEASE_DATE = '2024-03-23 00:48:13';
/**
* @var int
*/