Skip to content

Commit 8ae82c4

Browse files
committed
array_rand() requires a non-empty-array as of php8
1 parent bb8faa8 commit 8ae82c4

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
@@ -2368,4 +2368,27 @@ 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+
}
2387+
2388+
#[RequiresPhp('< 8.0')]
2389+
public function testArrayRandPhp7(): void
2390+
{
2391+
$this->analyse([__DIR__ . '/data/array-rand.php'], []);
2392+
}
2393+
23712394
}
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)