Skip to content

Commit

Permalink
[CodeQuality] Allow transform return; to return null; when there is o…
Browse files Browse the repository at this point in the history
…ther return with Expr on ExplicitReturnNullRector (#5761)

* [CodeQuality] Allow transform return; to return null; when there is other return with Expr on ExplicitReturnNullRector

* [ci-review] Rector Rectify

* more test

* [ci-review] Rector Rectify

* rename fixture

* update docblock example

* dont traverse children

* fix return

* more fixture

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Mar 23, 2024
1 parent cae0691 commit 97e2433
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

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

final class ReturnNoValueAndReturnValue
{
/**
* @return int|void
*/
public function run(int $number)
{
if ($number > 50) {
return;
}

if ($number < 50) {
return 100;
}
}
}

?>
-----
<?php

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

final class ReturnNoValueAndReturnValue
{
/**
* @return int|null
*/
public function run(int $number)
{
if ($number > 50) {
return null;
}

if ($number < 50) {
return 100;
}
return null;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

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

use stdClass;

final class ReturnNoValueAndReturnValue2
{
/**
* @return stdClass|void
*/
public function add()
{
if ('POST' !== $_SERVER['REQUEST_METHOD']) {
return;
}

return new stdClass();
}
}

?>
-----
<?php

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

use stdClass;

final class ReturnNoValueAndReturnValue2
{
/**
* @return stdClass|null
*/
public function add()
{
if ('POST' !== $_SERVER['REQUEST_METHOD']) {
return null;
}

return new stdClass();
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

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

final class SkipReturnNoValue
final class SkipAllReturnWithoutExpr
{
public function run(int $number)
{
if ($number > 50) {
echo 'test';
return;
}

if ($number < 50) {
return 100;
echo 'yes';
return;
}
}
}
50 changes: 31 additions & 19 deletions rules/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@
namespace Rector\CodeQuality\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Return_;
use PhpParser\NodeTraverser;
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\ReturnTypeInferer;
use Rector\TypeDeclaration\TypeInferer\SilentVoidResolver;
use Rector\ValueObject\MethodName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -28,11 +33,11 @@
final class ExplicitReturnNullRector extends AbstractRector
{
public function __construct(
private readonly BetterNodeFinder $betterNodeFinder,
private readonly SilentVoidResolver $silentVoidResolver,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly TypeFactory $typeFactory,
private readonly PhpDocTypeChanger $phpDocTypeChanger
private readonly PhpDocTypeChanger $phpDocTypeChanger,
private readonly ReturnTypeInferer $returnTypeInferer
) {
}

Expand Down Expand Up @@ -102,12 +107,32 @@ public function refactor(Node $node): ?Node
return null;
}

if (! $this->silentVoidResolver->hasSilentVoid($node)) {
$returnType = $this->returnTypeInferer->inferFunctionLike($node);
if (! $returnType instanceof UnionType) {
return null;
}

// it has at least some return value in it
if (! $this->hasReturnsWithValues($node)) {
$hasChanged = false;
$this->traverseNodesWithCallable((array) $node->stmts, static function (Node $node) use (&$hasChanged) : int|null|Return_ {
if ($node instanceof Class_ || $node instanceof Function_ || $node instanceof Closure) {
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}

if ($node instanceof Return_ && ! $node->expr instanceof Expr) {
$hasChanged = true;
$node->expr = new ConstFetch(new Name('null'));
return $node;
}

return null;
});

if (! $this->silentVoidResolver->hasSilentVoid($node)) {
if ($hasChanged) {
$this->transformDocUnionVoidToUnionNull($node);
return $node;
}

return null;
}

Expand Down Expand Up @@ -149,17 +174,4 @@ private function transformDocUnionVoidToUnionNull(ClassMethod $classMethod): voi

$this->phpDocTypeChanger->changeReturnType($classMethod, $phpDocInfo, $type);
}

private function hasReturnsWithValues(ClassMethod $classMethod): bool
{
/** @var Return_[] $returns */
$returns = $this->betterNodeFinder->findInstancesOfInFunctionLikeScoped($classMethod, Return_::class);
foreach ($returns as $return) {
if (! $return->expr instanceof Node) {
return false;
}
}

return $returns !== [];
}
}

0 comments on commit 97e2433

Please sign in to comment.