Skip to content

Commit

Permalink
[CodeQuality] Transform type|void docblock to type|null as well on Ex…
Browse files Browse the repository at this point in the history
…plicitReturnNullRector after add return null (#5756)

* [CodeQuality] Transform type|void docblock to type|null on ExplicitReturnNullRector

* [CodeQuality] Transform type|void docblock to type|null on ExplicitReturnNullRector

* doc include docblock feature

* doc include docblock feature
  • Loading branch information
samsonasik committed Mar 22, 2024
1 parent 32bf5c7 commit 576198c
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
4 changes: 4 additions & 0 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector\Fixture;

class WithUnionVoidDoc
{
/**
* @return string|void
*/
public function run(int $number)
{
if ($number > 50) {
return 'yes';
}
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector\Fixture;

class WithUnionVoidDoc
{
/**
* @return string|null
*/
public function run(int $number)
{
if ($number > 50) {
return 'yes';
}
return null;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
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;
Expand All @@ -25,7 +31,10 @@ final class ExplicitReturnNullRector extends AbstractRector
{
public function __construct(
private readonly BetterNodeFinder $betterNodeFinder,
private readonly SilentVoidResolver $silentVoidResolver
private readonly SilentVoidResolver $silentVoidResolver,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly TypeFactory $typeFactory,
private readonly PhpDocTypeChanger $phpDocTypeChanger
) {
}

Expand All @@ -38,6 +47,9 @@ public function getRuleDefinition(): RuleDefinition
<<<'CODE_SAMPLE'
class SomeClass
{
/**
* @return string|void
*/
public function run(int $number)
{
if ($number > 50) {
Expand All @@ -51,6 +63,9 @@ public function run(int $number)
<<<'CODE_SAMPLE'
class SomeClass
{
/**
* @return string|null
*/
public function run(int $number)
{
if ($number > 50) {
Expand Down Expand Up @@ -100,9 +115,37 @@ public function refactor(Node $node): ?Node

$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(
Expand Down

0 comments on commit 576198c

Please sign in to comment.