Skip to content

Commit 005b59a

Browse files
committed
Fix empty array case
1 parent e77077b commit 005b59a

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/Internal/ArrayHelper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public static function unsetKeyAtPath(array &$array, array $path): void
1818

1919
if (count($tail) === 0) {
2020
unset($array[$head]);
21-
} else {
21+
22+
} elseif (isset($array[$head])) {
2223
self::unsetKeyAtPath($array[$head], $tail);
2324
}
2425
}

tests/PHPStan/Internal/ArrayHelperTest.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ public function testUnsetKeyAtPath(): void
1919
'dep1b' => null,
2020
];
2121

22-
$path = ['dep1a', 'dep2a', 'dep3a'];
23-
ArrayHelper::unsetKeyAtPath($array, $path);
22+
ArrayHelper::unsetKeyAtPath($array, ['dep1a', 'dep2a', 'dep3a']);
2423

2524
$this->assertSame([
2625
'dep1a' => [
@@ -29,6 +28,34 @@ public function testUnsetKeyAtPath(): void
2928
],
3029
'dep1b' => null,
3130
], $array);
31+
32+
ArrayHelper::unsetKeyAtPath($array, ['dep1a', 'dep2a']);
33+
34+
$this->assertSame([
35+
'dep1a' => [
36+
'dep2b' => null,
37+
],
38+
'dep1b' => null,
39+
], $array);
40+
41+
ArrayHelper::unsetKeyAtPath($array, ['dep1a']);
42+
43+
$this->assertSame([
44+
'dep1b' => null,
45+
], $array);
46+
47+
ArrayHelper::unsetKeyAtPath($array, ['dep1b']);
48+
49+
$this->assertSame([], $array);
50+
}
51+
52+
public function testUnsetKeyAtPathEmpty(): void
53+
{
54+
$array = [];
55+
56+
ArrayHelper::unsetKeyAtPath($array, ['foo', 'bar']);
57+
58+
$this->assertSame([], $array);
3259
}
3360

3461
}

0 commit comments

Comments
 (0)