Skip to content

Commit c78a29b

Browse files
committed
fix(SimplifyListIndexRector): Refactor key handling logic
- Improve logic for handling keys in array items - Use collection methods more efficiently - Handle null and non-integer keys correctly - Ensure refactored logic maintains intended functionality and structure
1 parent aaffea5 commit c78a29b

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ parameters:
6666
message: 'use config() instead'
6767
ignoreErrors:
6868
- identifier: argument.templateType
69-
# - identifier: binaryOp.invalid
7069
# - identifier: cast.string
7170
# - identifier: method.nonObject
7271
# - identifier: return.void
7372
# - identifier: typePerfect.noMixedMethodCaller
7473
- identifier: argument.type
74+
- identifier: binaryOp.invalid
7575
- identifier: encapsedStringPart.nonString
7676
- identifier: logicalAnd.resultUnused
7777
- identifier: missingType.generics

src/Support/Rectors/SimplifyListIndexRector.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Guanguans\SoarPHP\Support\Rectors;
1515

16+
use Illuminate\Support\Collection;
1617
use PhpParser\Node;
1718
use PhpParser\Node\ArrayItem;
1819
use PhpParser\Node\Expr;
@@ -69,29 +70,37 @@ public function getNodeTypes(): array
6970
}
7071

7172
/**
73+
* @noinspection ProperNullCoalescingOperatorUsageInspection
74+
*
7275
* @param Array_ $node
7376
*/
7477
public function refactor(Node $node): ?Node
7578
{
76-
$keys = collect($node->items)->pluck('key');
79+
$keys = collect($node->items)
80+
->pluck('key')
81+
->map(fn (?Expr $expr): mixed => $expr instanceof Expr ? $this->valueResolver->getValue($expr) : null);
7782

78-
if (
79-
$keys
80-
->filter(static fn (?Expr $expr): bool => !$expr instanceof Int_)
81-
->isNotEmpty()
82-
) {
83+
if ($keys->filter(static fn (mixed $key): bool => null !== $key && !\is_int($key))->isNotEmpty()) {
8384
return null;
8485
}
8586

86-
$keyValues = $keys->mapWithKeys(fn (?Expr $expr): array => [
87-
$key = $this->valueResolver->getValue($expr, true) => $key,
88-
]);
87+
$finalKeys = $keys->reduce(
88+
static fn (Collection $carry, ?int $key): Collection => $carry->put(
89+
$key ?? ($carry->isEmpty() ? 0 : $carry->keys()->sortDesc(\SORT_NUMERIC)->first() + 1),
90+
$key
91+
),
92+
collect()
93+
);
8994

90-
if ($keyValues->count() !== $keys->count() || !$this->isList($keyValues->all())) {
95+
if (!$this->isList($finalKeys->all())) {
9196
return null;
9297
}
9398

94-
collect($node->items)->each(static fn (ArrayItem $arrayItem): mixed => $arrayItem->key = null);
99+
collect($node->items)->each(static function (ArrayItem $arrayItem): void {
100+
if ($arrayItem->key instanceof Int_) {
101+
$arrayItem->key = null;
102+
}
103+
});
95104

96105
return $node;
97106
}

0 commit comments

Comments
 (0)