Skip to content

Commit 8b9ec75

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 8b9ec75

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@
194194
"dump-soar-original-php-config": "Guanguans\\SoarPHP\\Support\\ComposerScripts::dumpSoarPHPConfig",
195195
"dump-soar-php-config": [
196196
"@dump-soar-original-php-config",
197-
"@rector-soar-options",
198-
"@style-fix"
197+
"@rector-soar-options --no-diffs",
198+
"@style-fix --quiet"
199199
],
200200
"dump-soar-yaml-config": "Guanguans\\SoarPHP\\Support\\ComposerScripts::dumpSoarYamlConfig",
201201
"facade-lint": "@facade-update --lint",

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: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
namespace Guanguans\SoarPHP\Support\Rectors;
1515

16+
use Illuminate\Support\Collection;
1617
use PhpParser\Node;
17-
use PhpParser\Node\ArrayItem;
1818
use PhpParser\Node\Expr;
1919
use PhpParser\Node\Expr\Array_;
2020
use PhpParser\Node\Scalar\Int_;
@@ -73,26 +73,30 @@ public function getNodeTypes(): array
7373
*/
7474
public function refactor(Node $node): ?Node
7575
{
76-
$keys = collect($node->items)->pluck('key');
76+
$keys = collect($node->items)
77+
->pluck('key')
78+
->map(fn (?Expr $expr): mixed => $expr instanceof Expr ? $this->valueResolver->getValue($expr) : null);
7779

80+
/** @noinspection NullPointerExceptionInspection */
7881
if (
79-
$keys
80-
->filter(static fn (?Expr $expr): bool => !$expr instanceof Int_)
81-
->isNotEmpty()
82+
$keys->filter(static fn (mixed $key): bool => null !== $key && !\is_int($key))->isNotEmpty()
83+
|| !$this->isList(
84+
$keys->reduce(
85+
static fn (Collection $carry, ?int $key): Collection => $carry->put(
86+
$key ?? ($carry->isEmpty() ? 0 : $carry->keys()->sortDesc(\SORT_NUMERIC)->first() + 1),
87+
$key
88+
),
89+
collect()
90+
)->all()
91+
)
8292
) {
8393
return null;
8494
}
8595

86-
$keyValues = $keys->mapWithKeys(fn (?Expr $expr): array => [
87-
$key = $this->valueResolver->getValue($expr, true) => $key,
88-
]);
89-
90-
if ($keyValues->count() !== $keys->count() || !$this->isList($keyValues->all())) {
91-
return null;
96+
foreach ($node->items as $item) {
97+
$item->key instanceof Int_ and $item->key = null;
9298
}
9399

94-
collect($node->items)->each(static fn (ArrayItem $arrayItem): mixed => $arrayItem->key = null);
95-
96100
return $node;
97101
}
98102

0 commit comments

Comments
 (0)