From fcad7158152d4e5c77b7c71a4c23485fc8bae3cc Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 9 Jan 2017 22:47:29 +0100 Subject: [PATCH] Arrays::searchKey() returns NULL instead of FALSE when item is not found (BC break) --- src/Utils/Arrays.php | 8 ++++---- tests/Utils/Arrays.searchKey().phpt | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Utils/Arrays.php b/src/Utils/Arrays.php index 17ca1c54a..adccaa8e3 100644 --- a/src/Utils/Arrays.php +++ b/src/Utils/Arrays.php @@ -78,12 +78,12 @@ public static function mergeTree(array $arr1, array $arr2): array /** * Searches the array for a given key and returns the offset if successful. - * @return int|FALSE offset if it is found, FALSE otherwise + * @return int|NULL offset if it is found, NULL otherwise */ public static function searchKey(array $arr, $key) { $foo = [$key => NULL]; - return array_search(key($foo), array_keys($arr), TRUE); + return ($tmp = array_search(key($foo), array_keys($arr), TRUE)) === FALSE ? NULL : $tmp; } @@ -105,7 +105,7 @@ public static function insertBefore(array &$arr, $key, array $inserted) public static function insertAfter(array &$arr, $key, array $inserted) { $offset = self::searchKey($arr, $key); - $offset = $offset === FALSE ? count($arr) : $offset + 1; + $offset = $offset === NULL ? count($arr) : $offset + 1; $arr = array_slice($arr, 0, $offset, TRUE) + $inserted + array_slice($arr, $offset, count($arr), TRUE); } @@ -117,7 +117,7 @@ public static function insertAfter(array &$arr, $key, array $inserted) public static function renameKey(array &$arr, $oldKey, $newKey) { $offset = self::searchKey($arr, $oldKey); - if ($offset !== FALSE) { + if ($offset !== NULL) { $keys = array_keys($arr); $keys[$offset] = $newKey; $arr = array_combine($keys, $arr); diff --git a/tests/Utils/Arrays.searchKey().phpt b/tests/Utils/Arrays.searchKey().phpt index f0d0ac723..21d4373f7 100644 --- a/tests/Utils/Arrays.searchKey().phpt +++ b/tests/Utils/Arrays.searchKey().phpt @@ -33,4 +33,4 @@ Assert::same(2, Arrays::searchKey($arr, 1)); Assert::same(1, Arrays::searchKey($arr, 0)); Assert::same(0, Arrays::searchKey($arr, NULL)); Assert::same(0, Arrays::searchKey($arr, '')); -Assert::false(Arrays::searchKey($arr, 'undefined')); +Assert::null(Arrays::searchKey($arr, 'undefined'));