Skip to content

Commit

Permalink
Set optional parameter default value to null
Browse files Browse the repository at this point in the history
Makes it less magical that it behaves differently when the parameter is omitted.
  • Loading branch information
vlakoff committed Jun 29, 2017
1 parent 67dca86 commit 79e6c8c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/Illuminate/Support/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,13 +448,13 @@ public static function pull(&$array, $key, $default = null)
*
* @throws \InvalidArgumentException
*/
public static function random($array, $amount = 1)
public static function random($array, $amount = null)
{
if ($amount > ($count = count($array))) {
throw new InvalidArgumentException("You requested {$amount} items, but there are only {$count} items in the array.");
if (($requested = $amount ?: 1) > ($count = count($array))) {
throw new InvalidArgumentException("You requested {$requested} items, but there are only {$count} items in the array.");
}

if (count(func_get_args()) == 1) {
if (is_null($amount)) {
return $array[array_rand($array)];
}

Expand Down
8 changes: 4 additions & 4 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1077,13 +1077,13 @@ public function put($key, $value)
*
* @throws \InvalidArgumentException
*/
public function random($amount = 1)
public function random($amount = null)
{
if ($amount > ($count = $this->count())) {
throw new InvalidArgumentException("You requested {$amount} items, but there are only {$count} items in the collection.");
if (($requested = $amount ?: 1) > ($count = $this->count())) {
throw new InvalidArgumentException("You requested {$requested} items, but there are only {$count} items in the collection.");
}

if (count(func_get_args()) == 0) {
if (is_null($amount)) {
return Arr::random($this->items);
}

Expand Down
6 changes: 1 addition & 5 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,8 @@ function array_pull(&$array, $key, $default = null)
* @param int|null $num
* @return mixed
*/
function array_random($array, $num = 1)
function array_random($array, $num = null)
{
if (count(func_get_args()) == 1) {
return Arr::random($array);
}

return Arr::random($array, $num);
}
}
Expand Down

0 comments on commit 79e6c8c

Please sign in to comment.