Skip to content

Commit

Permalink
Remove array items directly RemoveDuplicatedArrayKeyRector (#4013)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 29, 2023
1 parent cd48bfc commit 32510e0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ class Fixture
1 => 'B',
1 => 'A'
];

$key = 1;
$items = [
$key => 'A',
$key => 'A',
];
}
}

Expand All @@ -33,11 +27,6 @@ class Fixture
$items = [
1 => 'A'
];

$key = 1;
$items = [
$key => 'A',
];
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Array_\RemoveDuplicatedArrayKeyRector\Fixture;

class VariableKey
{
public function lists()
{
$key = 1;

$items = [
$key => 'A',
$key => 'A',
];
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Array_\RemoveDuplicatedArrayKeyRector\Fixture;

class VariableKey
{
public function lists()
{
$key = 1;

$items = [
$key => 'A',
];
}
}

?>
45 changes: 32 additions & 13 deletions rules/DeadCode/Rector/Array_/RemoveDuplicatedArrayKeyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,30 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$arrayItemsWithDuplicatedKey = $this->getArrayItemsWithDuplicatedKey($node);
if ($arrayItemsWithDuplicatedKey === []) {
$duplicatedKeysArrayItems = $this->resolveDuplicateKeysArrayItems($node);
if ($duplicatedKeysArrayItems === []) {
return null;
}

foreach ($arrayItemsWithDuplicatedKey as $arrayItemWithDuplicatedKey) {
// keep last item
array_pop($arrayItemWithDuplicatedKey);
$this->nodeRemover->removeNodes($arrayItemWithDuplicatedKey);
foreach ($node->items as $key => $arrayItem) {
if (! $arrayItem instanceof ArrayItem) {
continue;
}

if (! $this->isArrayItemDuplicated($duplicatedKeysArrayItems, $arrayItem)) {
continue;
}

unset($node->items[$key]);
}

return $node;
}

/**
* @return ArrayItem[][]
* @return ArrayItem[]
*/
private function getArrayItemsWithDuplicatedKey(Array_ $array): array
private function resolveDuplicateKeysArrayItems(Array_ $array): array
{
$arrayItemsByKeys = [];

Expand All @@ -104,28 +110,41 @@ private function getArrayItemsWithDuplicatedKey(Array_ $array): array
}

/**
* @param ArrayItem[][] $arrayItemsByKeys
* @return ArrayItem[][]
* @param array<mixed, ArrayItem[]> $arrayItemsByKeys
* @return array<ArrayItem>
*/
private function filterItemsWithSameKey(array $arrayItemsByKeys): array
{
$result = [];
$duplicatedArrayItems = [];

foreach ($arrayItemsByKeys as $arrayItems) {
if (count($arrayItems) <= 1) {
continue;
}

$currentArrayItem = current($arrayItems);

/** @var Expr $currentArrayItemKey */
$currentArrayItemKey = $currentArrayItem->key;

if ($this->multiInstanceofChecker->isInstanceOf($currentArrayItemKey, self::ALLOWED_KEY_DUPLICATES)) {
continue;
}

$result[] = $arrayItems;
// keep last one
array_pop($arrayItems);

$duplicatedArrayItems = array_merge($duplicatedArrayItems, $arrayItems);
}

return $result;
return $duplicatedArrayItems;
}

/**
* @param ArrayItem[] $duplicatedKeysArrayItems
*/
private function isArrayItemDuplicated(array $duplicatedKeysArrayItems, ArrayItem $arrayItem): bool
{
return in_array($arrayItem, $duplicatedKeysArrayItems, true);
}
}

0 comments on commit 32510e0

Please sign in to comment.