Skip to content

Commit c733366

Browse files
committed
Removed cloning input array for set and remove method
1 parent 05e88ff commit c733366

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/Arr.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public static function set(array $array, $keys, $value): array
192192
*/
193193
public static function setNestedElement(array $array, $keys, $value): array
194194
{
195-
$result = self::clone($array);
195+
$result = $array;
196196
$keysArray = self::getKeysArray($keys);
197197

198198
// If no keys specified then preserve array
@@ -229,7 +229,7 @@ public static function setNestedElement(array $array, $keys, $value): array
229229
*/
230230
public static function remove(array $array, $keys): array
231231
{
232-
$result = self::clone($array);
232+
$result = $array;
233233
$keysArray = self::getKeysArray($keys);
234234

235235
$tmp = &$result;

test/ArrTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,35 @@ public function testSetNestedElement()
151151
$this->assertSame([], Arr::setNestedElement([], [], 'test'));
152152
$this->assertSame([], Arr::setNestedElement([], [null], 'test'));
153153

154+
// Test for objects
155+
$obj1 = new stdClass();
156+
$obj2 = new stdClass();
157+
$array = [
158+
[
159+
$obj1,
160+
],
161+
'test' => [
162+
'abc' => [
163+
'foo' => $obj1,
164+
]
165+
],
166+
$obj2,
167+
];
168+
169+
$this->assertSame($array, Arr::setNestedElement($array, '', 'whatever'));
170+
$this->assertSame([
171+
[
172+
$obj1,
173+
],
174+
'test' => [
175+
'abc' => [
176+
'foo' => $obj1,
177+
]
178+
],
179+
$obj2,
180+
'test2' => $obj2,
181+
], Arr::setNestedElement($array, 'test2', $obj2));
182+
154183
// Test alias
155184
$this->assertSame(Arr::setNestedElement([], '[].[].[]', 'test'), Arr::set([], '[].[].[]', 'test'));
156185
$this->assertSame(Arr::setNestedElement($array, 'test.test2.test3', 'abc'), Arr::set($array, 'test.test2.test3', 'abc'));

0 commit comments

Comments
 (0)