Skip to content

Commit 2548dbb

Browse files
authored
array_rand() requires a non-empty-array as of PHP 8
1 parent bb8faa8 commit 2548dbb

File tree

6 files changed

+86
-0
lines changed

6 files changed

+86
-0
lines changed

resources/functionMap_php80delta_bleedingEdge.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
return [
44
'new' => [
5+
'array_rand' => ['int|string|array<int,int>|array<int,string>', 'input'=>'non-empty-array', 'num_req'=>'positive-int'],
6+
'array_rand\'1' => ['int|string', 'input'=>'non-empty-array'],
57
],
68
'old' => [
9+
'array_rand' => ['int|string|array<int,int>|array<int,string>', 'input'=>'array', 'num_req'=>'int'],
10+
'array_rand\'1' => ['int|string', 'input'=>'array'],
711
],
812
];

resources/functionMap_php82delta.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
'iterator_count' => ['0|positive-int', 'iterator'=>'iterable'],
2525
'iterator_to_array' => ['array', 'iterator'=>'iterable', 'use_keys='=>'bool'],
2626
'str_split' => ['list<string>', 'str'=>'string', 'split_length='=>'positive-int'],
27+
'Random\Randomizer::pickArrayKeys' => ['non-empty-array<int|string>', 'array'=>'non-empty-array', 'num'=>'positive-int'],
2728
],
2829
'old' => [
2930

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2368,4 +2368,39 @@ public function testBug13556(): void
23682368
$this->analyse([__DIR__ . '/data/bug-13556.php'], []);
23692369
}
23702370

2371+
#[RequiresPhp('>= 8.0')]
2372+
public function testArrayRand(): void
2373+
{
2374+
$this->analyse([__DIR__ . '/data/array_rand.php'], [
2375+
[
2376+
'Parameter #1 $input of function array_rand expects non-empty-array, array{} given.',
2377+
7,
2378+
'array{} is empty.',
2379+
],
2380+
[
2381+
'Parameter #1 $input of function array_rand expects non-empty-array, array{} given.',
2382+
8,
2383+
'array{} is empty.',
2384+
],
2385+
[
2386+
'Parameter #2 $num_req of function array_rand expects int<1, max>, int given.',
2387+
8,
2388+
],
2389+
[
2390+
'Parameter #2 $num_req of function array_rand expects int<1, max>, -5 given.',
2391+
13,
2392+
],
2393+
[
2394+
'Parameter #2 $num_req of function array_rand expects int<1, max>, 0 given.',
2395+
14,
2396+
],
2397+
]);
2398+
}
2399+
2400+
#[RequiresPhp('< 8.0')]
2401+
public function testArrayRandPhp7(): void
2402+
{
2403+
$this->analyse([__DIR__ . '/data/array_rand.php'], []);
2404+
}
2405+
23712406
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace ArrayRand;
4+
5+
function doFoo(int $i) {
6+
$arr = [];
7+
$x = array_rand($arr);
8+
$y = array_rand($arr, $i);
9+
}
10+
11+
/** @param non-empty-array $arr */
12+
function doBar(array $arr) {
13+
$y = array_rand($arr, -5);
14+
$y = array_rand($arr, 0);
15+
$y = array_rand($arr, 1);
16+
$y = array_rand($arr);
17+
}

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3675,4 +3675,25 @@ public function testBug13511(): void
36753675
$this->analyse([__DIR__ . '/data/bug-13511.php'], []);
36763676
}
36773677

3678+
#[RequiresPhp('>= 8.2')]
3679+
public function testRandomizer(): void
3680+
{
3681+
$this->checkThisOnly = false;
3682+
$this->checkNullables = true;
3683+
$this->checkUnionTypes = true;
3684+
$this->checkExplicitMixed = true;
3685+
3686+
$this->analyse([__DIR__ . '/data/randomizer.php'], [
3687+
[
3688+
'Parameter #2 $num of method Random\Randomizer::pickArrayKeys() expects int<1, max>, 0 given.',
3689+
7,
3690+
],
3691+
[
3692+
'Parameter #1 $array of method Random\Randomizer::pickArrayKeys() expects non-empty-array, array{} given.',
3693+
8,
3694+
'array{} is empty.',
3695+
],
3696+
]);
3697+
}
3698+
36783699
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Randomizer;
4+
5+
$r = new \Random\Randomizer();
6+
var_dump($r->pickArrayKeys(['a', 'b'], 10));
7+
var_dump($r->pickArrayKeys(['a'], 0));
8+
var_dump($r->pickArrayKeys([], 1));

0 commit comments

Comments
 (0)