Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions resources/functionMap_php80delta_bleedingEdge.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

return [
'new' => [
'array_rand' => ['int|string|array<int,int>|array<int,string>', 'input'=>'non-empty-array', 'num_req'=>'positive-int'],
'array_rand\'1' => ['int|string', 'input'=>'non-empty-array'],
],
'old' => [
'array_rand' => ['int|string|array<int,int>|array<int,string>', 'input'=>'array', 'num_req'=>'int'],
'array_rand\'1' => ['int|string', 'input'=>'array'],
],
];
1 change: 1 addition & 0 deletions resources/functionMap_php82delta.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
'iterator_count' => ['0|positive-int', 'iterator'=>'iterable'],
'iterator_to_array' => ['array', 'iterator'=>'iterable', 'use_keys='=>'bool'],
'str_split' => ['list<string>', 'str'=>'string', 'split_length='=>'positive-int'],
'Random\Randomizer::pickArrayKeys' => ['non-empty-array<int|string>', 'array'=>'non-empty-array', 'num'=>'positive-int'],
Copy link
Contributor Author

@staabm staabm Oct 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure whether/how this signature should be behind bleeding edge but beeing php 8.2+ at the same time.

as the api is pretty recent and throws runtime errors when used with empty array or non-positive-int its acceptable to enforce this more strict signature even in non-bleeding edge?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, in this case it's okay 👍

],
'old' => [

Expand Down
35 changes: 35 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2368,4 +2368,39 @@ public function testBug13556(): void
$this->analyse([__DIR__ . '/data/bug-13556.php'], []);
}

#[RequiresPhp('>= 8.0')]
public function testArrayRand(): void
{
$this->analyse([__DIR__ . '/data/array_rand.php'], [
[
'Parameter #1 $input of function array_rand expects non-empty-array, array{} given.',
7,
'array{} is empty.',
],
[
'Parameter #1 $input of function array_rand expects non-empty-array, array{} given.',
8,
'array{} is empty.',
],
[
'Parameter #2 $num_req of function array_rand expects int<1, max>, int given.',
8,
],
[
'Parameter #2 $num_req of function array_rand expects int<1, max>, -5 given.',
13,
],
[
'Parameter #2 $num_req of function array_rand expects int<1, max>, 0 given.',
14,
],
]);
}

#[RequiresPhp('< 8.0')]
public function testArrayRandPhp7(): void
{
$this->analyse([__DIR__ . '/data/array_rand.php'], []);
}

}
17 changes: 17 additions & 0 deletions tests/PHPStan/Rules/Functions/data/array_rand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace ArrayRand;

function doFoo(int $i) {
$arr = [];
$x = array_rand($arr);
$y = array_rand($arr, $i);
}

/** @param non-empty-array $arr */
function doBar(array $arr) {
$y = array_rand($arr, -5);
$y = array_rand($arr, 0);
$y = array_rand($arr, 1);
$y = array_rand($arr);
}
21 changes: 21 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3675,4 +3675,25 @@ public function testBug13511(): void
$this->analyse([__DIR__ . '/data/bug-13511.php'], []);
}

#[RequiresPhp('>= 8.2')]
public function testRandomizer(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->checkExplicitMixed = true;

$this->analyse([__DIR__ . '/data/randomizer.php'], [
[
'Parameter #2 $num of method Random\Randomizer::pickArrayKeys() expects int<1, max>, 0 given.',
7,
],
[
'Parameter #1 $array of method Random\Randomizer::pickArrayKeys() expects non-empty-array, array{} given.',
8,
'array{} is empty.',
],
]);
}

}
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Methods/data/randomizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Randomizer;

$r = new \Random\Randomizer();
var_dump($r->pickArrayKeys(['a', 'b'], 10));
var_dump($r->pickArrayKeys(['a'], 0));
var_dump($r->pickArrayKeys([], 1));
Loading