Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodeQuality] Add new rule - ExplicitReturnNullRector (#5753)
* [CodeQuality] Add new rule - ExplicitReturnNullRector * bump docs * register in set
- Loading branch information
1 parent
f32dff3
commit dc69b1a
Showing
11 changed files
with
303 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
.../CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/ExplicitReturnNullRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class ExplicitReturnNullRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...deQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/skip_already_return.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector\Fixture; | ||
|
||
final class SkipAlreadyReturn | ||
{ | ||
public function run(int $number) | ||
{ | ||
if ($number > 50) { | ||
return 'yes'; | ||
} | ||
|
||
return 100; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...odeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/skip_nested_return.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector\Fixture; | ||
|
||
final class SkipNestedReturn | ||
{ | ||
public function run(int $number) | ||
{ | ||
$result = function () { | ||
return 100; | ||
}; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...eQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/skip_return_no_value.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector\Fixture; | ||
|
||
final class SkipReturnNoValue | ||
{ | ||
public function run(int $number) | ||
{ | ||
if ($number > 50) { | ||
return; | ||
} | ||
|
||
if ($number < 50) { | ||
return 100; | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/some_class.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public function run(int $number) | ||
{ | ||
if ($number > 50) { | ||
return 'yes'; | ||
} | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public function run(int $number) | ||
{ | ||
if ($number > 50) { | ||
return 'yes'; | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
?> |
10 changes: 10 additions & 0 deletions
10
...-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector; | ||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(ExplicitReturnNullRector::class); | ||
}; |
135 changes: 135 additions & 0 deletions
135
rules/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\Rector\ClassMethod; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ConstFetch; | ||
use PhpParser\Node\Expr\Yield_; | ||
use PhpParser\Node\Expr\YieldFrom; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Return_; | ||
use PhpParser\Node\Stmt\Throw_; | ||
use Rector\PhpParser\Node\BetterNodeFinder; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector\ExplicitReturnNullRectorTest | ||
*/ | ||
final class ExplicitReturnNullRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private readonly BetterNodeFinder $betterNodeFinder, | ||
) { | ||
} | ||
|
||
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 | ||
{ | ||
public function run(int $number) | ||
{ | ||
if ($number > 50) { | ||
return 'yes'; | ||
} | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function run(int $number) | ||
{ | ||
if ($number > 50) { | ||
return 'yes'; | ||
} | ||
return null; | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
|
||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [ClassMethod::class]; | ||
} | ||
|
||
/** | ||
* @param ClassMethod $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
// known return type, nothing to improve | ||
if ($node->returnType instanceof Node) { | ||
return null; | ||
} | ||
|
||
if ($this->hasRootLevelReturn($node)) { | ||
return null; | ||
} | ||
|
||
if ($this->containsYieldOrThrow($node)) { | ||
return null; | ||
} | ||
|
||
// it has at least some return value in it | ||
if (! $this->hasReturnsWithValues($node)) { | ||
return null; | ||
} | ||
|
||
$node->stmts[] = new Return_(new ConstFetch(new Name('null'))); | ||
|
||
return $node; | ||
} | ||
|
||
private function hasRootLevelReturn(ClassMethod $classMethod): bool | ||
{ | ||
foreach ((array) $classMethod->stmts as $stmt) { | ||
if ($stmt instanceof Return_) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private function containsYieldOrThrow(ClassMethod $classMethod): bool | ||
{ | ||
return (bool) $this->betterNodeFinder->findInstancesOf( | ||
$classMethod, | ||
[Yield_::class, Throw_::class, Node\Expr\Throw_::class, YieldFrom::class] | ||
); | ||
} | ||
|
||
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 !== []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters