Skip to content

Commit 3c62bf6

Browse files
committed
array_rand() requires a non-empty-array as of php8
1 parent f23422c commit 3c62bf6

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-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'=>'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
];

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2331,4 +2331,27 @@ public function testBug13556(): void
23312331
$this->analyse([__DIR__ . '/data/bug-13556.php'], []);
23322332
}
23332333

2334+
#[RequiresPhp('>= 8.0')]
2335+
public function testArrayRand(): void
2336+
{
2337+
$this->analyse([__DIR__ . '/data/array-rand.php'], [
2338+
[
2339+
'Parameter #1 $input of function array_rand expects non-empty-array, array{} given.',
2340+
7,
2341+
'array{} is empty.'
2342+
],
2343+
[
2344+
'Parameter #1 $input of function array_rand expects non-empty-array, array{} given.',
2345+
8,
2346+
'array{} is empty.'
2347+
]
2348+
]);
2349+
}
2350+
2351+
#[RequiresPhp('< 8.0')]
2352+
public function testArrayRandPhp7(): void
2353+
{
2354+
$this->analyse([__DIR__ . '/data/array-rand.php'], []);
2355+
}
2356+
23342357
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
}

0 commit comments

Comments
 (0)