Skip to content

Commit

Permalink
Arrays::searchKey() returns NULL instead of FALSE when item is not fo…
Browse files Browse the repository at this point in the history
…und (BC break)
  • Loading branch information
dg committed Feb 1, 2017
1 parent a632cd6 commit fcad715
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/Utils/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand All @@ -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);
}

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/Utils/Arrays.searchKey().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

0 comments on commit fcad715

Please sign in to comment.