mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-04 02:10:51 +00:00
7d36c3b0b9
a80cf5292d
revert to working scoper 0.14
81 lines
2.3 KiB
PHP
81 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\CodeQuality\Rector\FuncCall;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Expr\Array_;
|
|
use PhpParser\Node\Expr\ArrayItem;
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
use Rector\Core\Rector\AbstractRector;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
/**
|
|
* @see https://3v4l.org/aLf96
|
|
* @see https://3v4l.org/2r26K
|
|
* @see https://3v4l.org/anks3
|
|
*
|
|
* @see \Rector\Tests\CodeQuality\Rector\FuncCall\ArrayMergeOfNonArraysToSimpleArrayRector\ArrayMergeOfNonArraysToSimpleArrayRectorTest
|
|
*/
|
|
final class ArrayMergeOfNonArraysToSimpleArrayRector extends \Rector\Core\Rector\AbstractRector
|
|
{
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
|
{
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Change array_merge of non arrays to array directly', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
|
|
class SomeClass
|
|
{
|
|
public function go()
|
|
{
|
|
$value = 5;
|
|
$value2 = 10;
|
|
|
|
return array_merge([$value], [$value2]);
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
class SomeClass
|
|
{
|
|
public function go()
|
|
{
|
|
$value = 5;
|
|
$value2 = 10;
|
|
|
|
return [$value, $value2];
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
)]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [\PhpParser\Node\Expr\FuncCall::class];
|
|
}
|
|
/**
|
|
* @param FuncCall $node
|
|
*/
|
|
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
|
{
|
|
if (!$this->isName($node, 'array_merge')) {
|
|
return null;
|
|
}
|
|
$array = new \PhpParser\Node\Expr\Array_();
|
|
foreach ($node->args as $arg) {
|
|
$nestedArrayItem = $arg->value;
|
|
if (!$nestedArrayItem instanceof \PhpParser\Node\Expr\Array_) {
|
|
return null;
|
|
}
|
|
foreach ($nestedArrayItem->items as $nestedArrayItemItem) {
|
|
if ($nestedArrayItemItem === null) {
|
|
continue;
|
|
}
|
|
$array->items[] = new \PhpParser\Node\Expr\ArrayItem($nestedArrayItemItem->value, $nestedArrayItemItem->key);
|
|
}
|
|
}
|
|
return $array;
|
|
}
|
|
}
|