Skip to content

Commit aff829f

Browse files
committed
Fixes #217: use array_key_exists() for arrays
1 parent df44e36 commit aff829f

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/Functional/Pick.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* @param ArrayAccess|array $collection
2121
* @param mixed $index
2222
* @param mixed $default
23-
* @param callable $callback Custom function to check if index exists, default function is "isset"
23+
* @param callable|null $callback Custom function to check if index exists
2424
* @return mixed
2525
* @no-named-arguments
2626
*/
@@ -29,7 +29,7 @@ function pick($collection, $index, $default = null, callable $callback = null)
2929
InvalidArgumentException::assertArrayAccess($collection, __FUNCTION__, 1);
3030

3131
if ($callback === null) {
32-
if (!isset($collection[$index])) {
32+
if (!isset($collection[$index]) && (!\is_array($collection) || !\array_key_exists($index, $collection))) {
3333
return $default;
3434
}
3535
} else {

tests/Functional/PickTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public function testDefaultValue(): void
7171
pick($this->array_1, 'one', 5),
7272
'Index does exists, should return the corresponding value'
7373
);
74+
75+
self::assertNull(pick($this->array_1, 'null-index', 'default'), 'Will handle null correctly');
7476
}
7577

7678
public function testCustomCallback(): void
@@ -93,8 +95,10 @@ public function testArrayAccess(): void
9395
$object = new ArrayObject();
9496

9597
$object['test'] = 5;
98+
$object['null'] = null;
9699

97100
self::assertSame(5, pick($object, 'test'), 'Key exists');
101+
self::assertNull(pick($object, 'null'), 'Key exists');
98102
self::assertSame(10, pick($object, 'dummy', 10), 'Key does not exists');
99103
}
100104
}

0 commit comments

Comments
 (0)