Skip to content

Commit 2a011f3

Browse files
committed
Deduplicate code for setting nested array value
1 parent 4f1eef4 commit 2a011f3

File tree

2 files changed

+23
-41
lines changed

2 files changed

+23
-41
lines changed

src/Helpers.php

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,28 @@ public static function propMapToSelectMap(array $map): array
4444
$selectMap = [];
4545

4646
foreach ($map as $prop) {
47-
$key = $prop->map[0];
48-
/** @psalm-suppress EmptyArrayAccess */
49-
$_ref = &$selectMap[$key];
50-
51-
for ($i = 1; $i < $prop->depth; $i++) {
52-
$key = $prop->map[$i];
53-
/** @psalm-suppress MixedArrayAccess */
54-
$_ref = &$_ref[$key];
55-
}
56-
57-
$_ref = $prop->col;
58-
unset($_ref); // dereference
47+
self::setNestedValue($selectMap, $prop->map, $prop->col);
5948
}
6049

6150
return $selectMap;
6251
}
6352

53+
/**
54+
* @param array<string, mixed> $array
55+
* @param string[] $path
56+
*/
57+
private static function setNestedValue(array &$array, array $path, mixed $value): void
58+
{
59+
$arr = &$array;
60+
61+
foreach ($path as $key) {
62+
/** @phpstan-ignore offsetAccess.nonOffsetAccessible */
63+
$arr = &$arr[$key];
64+
}
65+
66+
$arr = $value;
67+
}
68+
6469
/**
6570
* @param \Generator<int, array> $rows
6671
* @param Prop[] $fieldProps
@@ -111,34 +116,12 @@ public static function mapRows(\Generator $rows, array $fieldProps): array
111116
continue;
112117
}
113118

114-
$key = $prop->map[0];
115-
/** @psalm-suppress EmptyArrayAccess */
116-
$_ref = &$entity[$key];
117-
118-
for ($i = 1; $i < $prop->depth; $i++) {
119-
$key = $prop->map[$i];
120-
/** @psalm-suppress MixedArrayAccess */
121-
$_ref = &$_ref[$key];
122-
}
123-
124-
$_ref = $value;
125-
unset($_ref); // dereference
119+
self::setNestedValue($entity, $prop->map, $value);
126120
}
127121

128122
foreach ($nullParents as $prop) {
129-
$depth = $prop->depth - 1;
130-
$key = $prop->map[0];
131-
/** @psalm-suppress EmptyArrayAccess */
132-
$_ref = &$entity[$key];
133-
134-
for ($i = 1; $i < $depth; $i++) {
135-
$key = $prop->map[$i];
136-
/** @psalm-suppress MixedArrayAccess */
137-
$_ref = &$_ref[$key];
138-
}
139-
140-
$_ref = null;
141-
unset($_ref); // dereference
123+
$path = array_slice($prop->map, 0, -1);
124+
self::setNestedValue($entity, $path, null);
142125
}
143126

144127
$entities[] = $entity;

src/Prop.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class Prop
1717
* @var string[]
1818
*/
1919
public readonly array $map;
20-
public readonly int $depth;
2120

2221
/**
2322
* @var string[]
@@ -41,17 +40,17 @@ public function __construct(
4140
) {
4241
$this->output = $output;
4342
$this->map = explode('.', $name);
44-
$this->depth = count($this->map);
43+
$depth = count($this->map);
4544
$parent = '';
4645
$parents = [];
4746

48-
for ($i = 0; $i < $this->depth - 1; $i++) {
47+
for ($i = 0; $i < $depth - 1; $i++) {
4948
$parent .= $this->map[$i] . '.';
5049
$parents[] = $parent;
5150
}
5251
$this->parents = $parents;
5352

54-
if ($nullGroup && $this->depth < 2) {
53+
if ($nullGroup && $depth < 2) {
5554
throw new \Exception("nullGroup cannot be set on top-level {$name} property");
5655
}
5756

0 commit comments

Comments
 (0)