mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-20 23:41:57 +02:00
[CodingQuality] Add ForeachItemsAssignToEmptyArrayToAssignRector
This commit is contained in:
parent
b7d6078852
commit
44e96deeff
@ -50,3 +50,4 @@ services:
|
||||
Rector\CodeQuality\Rector\Include_\AbsolutizeRequireAndIncludePathRector: ~
|
||||
Rector\CodeQuality\Rector\FuncCall\ChangeArrayPushToArrayAssignRector: ~
|
||||
Rector\CodeQuality\Rector\For_\ForRepeatedCountToOwnVariableRector: ~
|
||||
Rector\CodeQuality\Rector\Foreach_\ForeachItemsAssignToEmptyArrayToAssignRector: ~
|
||||
|
@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodeQuality\Rector\Foreach_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\ArrayDimFetch;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\Foreach_;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
/**
|
||||
* @see \Rector\CodeQuality\Tests\Rector\Foreach_\ForeachItemsAssignToEmptyArrayToAssignRector\ForeachItemsAssignToEmptyArrayToAssignRectorTest
|
||||
*/
|
||||
final class ForeachItemsAssignToEmptyArrayToAssignRector extends AbstractRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Change foreach() items assign to empty array to direct assign', [
|
||||
new CodeSample(
|
||||
<<<'PHP'
|
||||
class SomeClass
|
||||
{
|
||||
public function run($items)
|
||||
{
|
||||
$items2 = [];
|
||||
foreach ($items as $item) {
|
||||
$items2[] = $item;
|
||||
}
|
||||
}
|
||||
}
|
||||
PHP
|
||||
,
|
||||
<<<'PHP'
|
||||
class SomeClass
|
||||
{
|
||||
public function run($items)
|
||||
{
|
||||
$items2 = [];
|
||||
$items2 = $items;
|
||||
}
|
||||
}
|
||||
PHP
|
||||
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Foreach_::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Foreach_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$assignVariable = $this->matchAssignItemsOnlyForeachArrayVariable($node);
|
||||
if ($assignVariable === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$previousDeclaration = $this->findPreviousNodeUsage($node, $assignVariable);
|
||||
if ($previousDeclaration === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$previousDeclarationParentNode = $previousDeclaration->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if (! $previousDeclarationParentNode instanceof Assign) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// must be empty array, othewise it will false override
|
||||
$defaultValue = $this->getValue($previousDeclarationParentNode->expr);
|
||||
if ($defaultValue !== []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new Assign($assignVariable, $node->expr);
|
||||
}
|
||||
|
||||
private function matchAssignItemsOnlyForeachArrayVariable(Foreach_ $foreach): ?Expr
|
||||
{
|
||||
if (count($foreach->stmts) !== 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$onlyStatement = $foreach->stmts[0];
|
||||
if ($onlyStatement instanceof Expression) {
|
||||
$onlyStatement = $onlyStatement->expr;
|
||||
}
|
||||
|
||||
if (! $onlyStatement instanceof Assign) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $onlyStatement->var instanceof ArrayDimFetch) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->areNodesEqual($foreach->valueVar, $onlyStatement->expr)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $onlyStatement->var->var;
|
||||
}
|
||||
|
||||
private function findPreviousNodeUsage(Node $node, Expr $expr): ?Node
|
||||
{
|
||||
return $this->betterNodeFinder->findFirstPrevious($node, function (Node $node) use ($expr) {
|
||||
if ($node === $expr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->areNodesEqual($node, $expr);
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\Foreach_\ForeachItemsAssignToEmptyArrayToAssignRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run($items)
|
||||
{
|
||||
$items2 = [];
|
||||
foreach ($items as $item) {
|
||||
$items2[] = $item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\Foreach_\ForeachItemsAssignToEmptyArrayToAssignRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run($items)
|
||||
{
|
||||
$items2 = [];
|
||||
$items2 = $items;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\Foreach_\ForeachItemsAssignToEmptyArrayToAssignRector\Fixture;
|
||||
|
||||
class SkipArgument
|
||||
{
|
||||
public function run($items, $items2)
|
||||
{
|
||||
foreach ($items as $item) {
|
||||
$items2[] = $item;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\Foreach_\ForeachItemsAssignToEmptyArrayToAssignRector\Fixture;
|
||||
|
||||
class SkipNonEmptyArray
|
||||
{
|
||||
public function run($items)
|
||||
{
|
||||
$items2 = [1];
|
||||
foreach ($items as $item) {
|
||||
$items2[] = $item;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\Foreach_\ForeachItemsAssignToEmptyArrayToAssignRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\CodeQuality\Rector\Foreach_\ForeachItemsAssignToEmptyArrayToAssignRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class ForeachItemsAssignToEmptyArrayToAssignRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideDataForTest()
|
||||
*/
|
||||
public function test(string $file): void
|
||||
{
|
||||
$this->doTestFile($file);
|
||||
}
|
||||
|
||||
public function provideDataForTest(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return ForeachItemsAssignToEmptyArrayToAssignRector::class;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user