2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\DeadCode\Rector\Array_;
|
2018-12-25 13:11:31 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
2023-03-23 23:21:34 +00:00
|
|
|
use PhpParser\Node\Expr;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\Array_;
|
|
|
|
use PhpParser\Node\Expr\ArrayItem;
|
2023-04-11 04:16:20 +00:00
|
|
|
use PhpParser\Node\Expr\PreDec;
|
|
|
|
use PhpParser\Node\Expr\PreInc;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\PhpParser\Printer\BetterStandardPrinter;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-03-26 11:38:07 +00:00
|
|
|
/**
|
2022-05-12 19:17:07 +00:00
|
|
|
* @changelog https://3v4l.org/SG0Wu
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\DeadCode\Rector\Array_\RemoveDuplicatedArrayKeyRector\RemoveDuplicatedArrayKeyRectorTest
|
2019-03-26 11:38:07 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RemoveDuplicatedArrayKeyRector extends AbstractRector
|
2018-12-25 13:11:31 +00:00
|
|
|
{
|
2022-04-09 22:42:24 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Printer\BetterStandardPrinter
|
2022-04-09 22:42:24 +00:00
|
|
|
*/
|
2023-06-08 14:23:03 +00:00
|
|
|
private $betterStandardPrinter;
|
2023-07-09 09:46:05 +00:00
|
|
|
public function __construct(BetterStandardPrinter $betterStandardPrinter)
|
2022-04-09 22:42:24 +00:00
|
|
|
{
|
2023-06-08 14:23:03 +00:00
|
|
|
$this->betterStandardPrinter = $betterStandardPrinter;
|
2022-04-09 22:42:24 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-12-25 13:11:31 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove duplicated key in defined arrays.', [new CodeSample(<<<'CODE_SAMPLE'
|
2018-12-25 13:11:31 +00:00
|
|
|
$item = [
|
|
|
|
1 => 'A',
|
2019-03-26 11:38:07 +00:00
|
|
|
1 => 'B'
|
2018-12-25 13:11:31 +00:00
|
|
|
];
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-12-25 13:11:31 +00:00
|
|
|
$item = [
|
2019-03-26 11:38:07 +00:00
|
|
|
1 => 'B'
|
2018-12-25 13:11:31 +00:00
|
|
|
];
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2018-12-25 13:11:31 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-12-25 13:11:31 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-12-25 13:11:31 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Array_::class];
|
2018-12-25 13:11:31 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Array_ $node
|
2018-12-25 13:11:31 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2020-05-06 21:29:58 +00:00
|
|
|
{
|
2023-05-29 09:16:46 +00:00
|
|
|
$duplicatedKeysArrayItems = $this->resolveDuplicateKeysArrayItems($node);
|
|
|
|
if ($duplicatedKeysArrayItems === []) {
|
2020-05-06 21:29:58 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-29 09:16:46 +00:00
|
|
|
foreach ($node->items as $key => $arrayItem) {
|
|
|
|
if (!$arrayItem instanceof ArrayItem) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!$this->isArrayItemDuplicated($duplicatedKeysArrayItems, $arrayItem)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
unset($node->items[$key]);
|
2020-05-06 21:29:58 +00:00
|
|
|
}
|
|
|
|
return $node;
|
|
|
|
}
|
2020-07-27 06:56:25 +00:00
|
|
|
/**
|
2023-05-29 09:16:46 +00:00
|
|
|
* @return ArrayItem[]
|
2020-07-27 06:56:25 +00:00
|
|
|
*/
|
2023-05-29 09:16:46 +00:00
|
|
|
private function resolveDuplicateKeysArrayItems(Array_ $array) : array
|
2018-12-25 13:11:31 +00:00
|
|
|
{
|
2019-03-26 11:38:07 +00:00
|
|
|
$arrayItemsByKeys = [];
|
2020-05-06 21:29:58 +00:00
|
|
|
foreach ($array->items as $arrayItem) {
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$arrayItem instanceof ArrayItem) {
|
2019-03-31 13:43:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-03-23 23:21:34 +00:00
|
|
|
if (!$arrayItem->key instanceof Expr) {
|
2018-12-25 13:11:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-06-08 14:23:03 +00:00
|
|
|
$keyValue = $this->betterStandardPrinter->print($arrayItem->key);
|
2019-03-26 11:38:07 +00:00
|
|
|
$arrayItemsByKeys[$keyValue][] = $arrayItem;
|
|
|
|
}
|
2020-05-06 21:29:58 +00:00
|
|
|
return $this->filterItemsWithSameKey($arrayItemsByKeys);
|
2018-12-25 13:11:31 +00:00
|
|
|
}
|
2020-05-06 16:39:07 +00:00
|
|
|
/**
|
2023-05-29 09:16:46 +00:00
|
|
|
* @param array<mixed, ArrayItem[]> $arrayItemsByKeys
|
|
|
|
* @return array<ArrayItem>
|
2020-05-06 16:39:07 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
private function filterItemsWithSameKey(array $arrayItemsByKeys) : array
|
2020-05-06 16:39:07 +00:00
|
|
|
{
|
2023-05-29 09:16:46 +00:00
|
|
|
$duplicatedArrayItems = [];
|
2023-04-11 04:16:20 +00:00
|
|
|
foreach ($arrayItemsByKeys as $arrayItems) {
|
|
|
|
if (\count($arrayItems) <= 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$currentArrayItem = \current($arrayItems);
|
|
|
|
/** @var Expr $currentArrayItemKey */
|
|
|
|
$currentArrayItemKey = $currentArrayItem->key;
|
2023-07-09 09:46:05 +00:00
|
|
|
if ($currentArrayItemKey instanceof PreInc) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($currentArrayItemKey instanceof PreDec) {
|
2023-04-11 04:16:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-05-29 09:16:46 +00:00
|
|
|
// keep last one
|
|
|
|
\array_pop($arrayItems);
|
|
|
|
$duplicatedArrayItems = \array_merge($duplicatedArrayItems, $arrayItems);
|
2023-04-11 04:16:20 +00:00
|
|
|
}
|
2023-05-29 09:16:46 +00:00
|
|
|
return $duplicatedArrayItems;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param ArrayItem[] $duplicatedKeysArrayItems
|
|
|
|
*/
|
|
|
|
private function isArrayItemDuplicated(array $duplicatedKeysArrayItems, ArrayItem $arrayItem) : bool
|
|
|
|
{
|
|
|
|
return \in_array($arrayItem, $duplicatedKeysArrayItems, \true);
|
2020-05-06 16:39:07 +00:00
|
|
|
}
|
2018-12-25 13:11:31 +00:00
|
|
|
}
|