mirror of
https://github.com/rectorphp/rector.git
synced 2024-07-05 09:03:31 +00:00
[CodeQuality][EarlyReturn] Handle SimplifyIfElseToTernaryRector + ChangeIfElseValueAssignToEarlyReturnRector (#1710)
* [CodeQuality][EarlyReturn] Handle SimplifyIfElseToTernaryRector + ChangeIfElseValueAssignToEarlyReturnRector
* Fixed 🎉
* early return when nodesToRemoveCollector is not active
* clean up
* final touch: rename fixture
* test single rule for use on return after if else assign
* single rule update
* final touch: clean up
This commit is contained in:
parent
cb96224c93
commit
369c2f7dfe
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector\Fixture;
|
||||
|
||||
final class UseOnReturnAfterIfElseAssign
|
||||
{
|
||||
public static function classToArray($data): array
|
||||
{
|
||||
if (method_exists($data, 'toRawArray')) {
|
||||
$properties = $data->toRawArray();
|
||||
} else {
|
||||
$properties = (array) $data;
|
||||
}
|
||||
|
||||
return $properties;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector\Fixture;
|
||||
|
||||
final class UseOnReturnAfterIfElseAssign
|
||||
{
|
||||
public static function classToArray($data): array
|
||||
{
|
||||
$properties = method_exists($data, 'toRawArray') ? $data->toRawArray() : (array) $data;
|
||||
|
||||
return $properties;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -12,7 +12,9 @@ use PhpParser\Node\Expr\Ternary;
|
|||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
|
@ -118,12 +120,40 @@ CODE_SAMPLE
|
|||
return null;
|
||||
}
|
||||
|
||||
if ($this->isNextReturnRemoved($node, $ifAssignVar)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$expression = new Expression($assign);
|
||||
$this->mirrorComments($expression, $node);
|
||||
|
||||
return $expression;
|
||||
}
|
||||
|
||||
private function isNextReturnRemoved(If_ $if, Expr $expr): bool
|
||||
{
|
||||
if (! $this->nodesToRemoveCollector->isActive()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$next = $if->getAttribute(AttributeKey::NEXT_NODE);
|
||||
|
||||
if ($next instanceof Return_ && $next->expr instanceof Expr && $this->nodeComparator->areNodesEqual(
|
||||
$next->expr,
|
||||
$expr
|
||||
)) {
|
||||
$nodesToRemove = $this->nodesToRemoveCollector->getNodesToRemove();
|
||||
|
||||
foreach ($nodesToRemove as $nodeToRemove) {
|
||||
if ($this->nodeComparator->areNodesEqual($next, $nodeToRemove)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Stmt[] $stmts
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Tests\Issues\IfElseAssignReturnUsed\Fixture;
|
||||
|
||||
final class UseOnReturnAfterIfElseAssign
|
||||
{
|
||||
public static function classToArray($data): array
|
||||
{
|
||||
if (method_exists($data, 'toRawArray')) {
|
||||
$properties = $data->toRawArray();
|
||||
} else {
|
||||
$properties = (array) $data;
|
||||
}
|
||||
|
||||
return $properties;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Tests\Issues\IfElseAssignReturnUsed\Fixture;
|
||||
|
||||
final class UseOnReturnAfterIfElseAssign
|
||||
{
|
||||
public static function classToArray($data): array
|
||||
{
|
||||
if (method_exists($data, 'toRawArray')) {
|
||||
return $data->toRawArray();
|
||||
}
|
||||
|
||||
return (array) $data;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Tests\Issues\IfElseAssignReturnUsed;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class IfElseAssignReturnUsedTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/configured_rule.php';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector;
|
||||
use Rector\EarlyReturn\Rector\If_\ChangeIfElseValueAssignToEarlyReturnRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(ChangeIfElseValueAssignToEarlyReturnRector::class);
|
||||
$services->set(SimplifyIfElseToTernaryRector::class);
|
||||
};
|
Loading…
Reference in New Issue
Block a user