Skip to content

Commit 79e6c8c

Browse files
committed
Set optional parameter default value to null
Makes it less magical that it behaves differently when the parameter is omitted.
1 parent 67dca86 commit 79e6c8c

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

src/Illuminate/Support/Arr.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -448,13 +448,13 @@ public static function pull(&$array, $key, $default = null)
448448
*
449449
* @throws \InvalidArgumentException
450450
*/
451-
public static function random($array, $amount = 1)
451+
public static function random($array, $amount = null)
452452
{
453-
if ($amount > ($count = count($array))) {
454-
throw new InvalidArgumentException("You requested {$amount} items, but there are only {$count} items in the array.");
453+
if (($requested = $amount ?: 1) > ($count = count($array))) {
454+
throw new InvalidArgumentException("You requested {$requested} items, but there are only {$count} items in the array.");
455455
}
456456

457-
if (count(func_get_args()) == 1) {
457+
if (is_null($amount)) {
458458
return $array[array_rand($array)];
459459
}
460460

src/Illuminate/Support/Collection.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -1077,13 +1077,13 @@ public function put($key, $value)
10771077
*
10781078
* @throws \InvalidArgumentException
10791079
*/
1080-
public function random($amount = 1)
1080+
public function random($amount = null)
10811081
{
1082-
if ($amount > ($count = $this->count())) {
1083-
throw new InvalidArgumentException("You requested {$amount} items, but there are only {$count} items in the collection.");
1082+
if (($requested = $amount ?: 1) > ($count = $this->count())) {
1083+
throw new InvalidArgumentException("You requested {$requested} items, but there are only {$count} items in the collection.");
10841084
}
10851085

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

src/Illuminate/Support/helpers.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,8 @@ function array_pull(&$array, $key, $default = null)
253253
* @param int|null $num
254254
* @return mixed
255255
*/
256-
function array_random($array, $num = 1)
256+
function array_random($array, $num = null)
257257
{
258-
if (count(func_get_args()) == 1) {
259-
return Arr::random($array);
260-
}
261-
262258
return Arr::random($array, $num);
263259
}
264260
}

0 commit comments

Comments
 (0)