Skip to content

Commit fcad715

Browse files
committed
Arrays::searchKey() returns NULL instead of FALSE when item is not found (BC break)
1 parent a632cd6 commit fcad715

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/Utils/Arrays.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ public static function mergeTree(array $arr1, array $arr2): array
7878

7979
/**
8080
* Searches the array for a given key and returns the offset if successful.
81-
* @return int|FALSE offset if it is found, FALSE otherwise
81+
* @return int|NULL offset if it is found, NULL otherwise
8282
*/
8383
public static function searchKey(array $arr, $key)
8484
{
8585
$foo = [$key => NULL];
86-
return array_search(key($foo), array_keys($arr), TRUE);
86+
return ($tmp = array_search(key($foo), array_keys($arr), TRUE)) === FALSE ? NULL : $tmp;
8787
}
8888

8989

@@ -105,7 +105,7 @@ public static function insertBefore(array &$arr, $key, array $inserted)
105105
public static function insertAfter(array &$arr, $key, array $inserted)
106106
{
107107
$offset = self::searchKey($arr, $key);
108-
$offset = $offset === FALSE ? count($arr) : $offset + 1;
108+
$offset = $offset === NULL ? count($arr) : $offset + 1;
109109
$arr = array_slice($arr, 0, $offset, TRUE) + $inserted + array_slice($arr, $offset, count($arr), TRUE);
110110
}
111111

@@ -117,7 +117,7 @@ public static function insertAfter(array &$arr, $key, array $inserted)
117117
public static function renameKey(array &$arr, $oldKey, $newKey)
118118
{
119119
$offset = self::searchKey($arr, $oldKey);
120-
if ($offset !== FALSE) {
120+
if ($offset !== NULL) {
121121
$keys = array_keys($arr);
122122
$keys[$offset] = $newKey;
123123
$arr = array_combine($keys, $arr);

tests/Utils/Arrays.searchKey().phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ Assert::same(2, Arrays::searchKey($arr, 1));
3333
Assert::same(1, Arrays::searchKey($arr, 0));
3434
Assert::same(0, Arrays::searchKey($arr, NULL));
3535
Assert::same(0, Arrays::searchKey($arr, ''));
36-
Assert::false(Arrays::searchKey($arr, 'undefined'));
36+
Assert::null(Arrays::searchKey($arr, 'undefined'));

0 commit comments

Comments
 (0)