Skip to content

Commit 85faddc

Browse files
committed
Merge branch 2.1.x into 2.2.x
2 parents 915f23a + 7896148 commit 85faddc

3 files changed

Lines changed: 84 additions & 0 deletions

File tree

src/Rules/Functions/RandomIntParametersRule.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ public function processNode(Node $node, Scope $scope): array
6565

6666
$isSmaller = $maxType->isSmallerThan($minType, $this->phpVersion);
6767

68+
if ($isSmaller->maybe() && $this->reportMaybes) {
69+
if (
70+
$minType instanceof IntegerRangeType && ($minType->getMin() === null || $minType->getMax() === null)
71+
|| $maxType instanceof IntegerRangeType && ($maxType->getMin() === null || $maxType->getMax() === null)
72+
) {
73+
return [];
74+
}
75+
}
76+
6877
if ($isSmaller->yes() || $isSmaller->maybe() && $this->reportMaybes) {
6978
$message = 'Parameter #1 $min (%s) of function random_int expects lower number than parameter #2 $max (%s).';
7079
return [

tests/PHPStan/Rules/Functions/RandomIntParametersRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,14 @@ public function testBug6361(): void
7474
$this->analyse([__DIR__ . '/data/bug-6361.php'], []);
7575
}
7676

77+
public function testBug14468(): void
78+
{
79+
$this->analyse([__DIR__ . '/data/bug-14468.php'], [
80+
[
81+
'Parameter #1 $min (int<5, max>) of function random_int expects lower number than parameter #2 $max (int<min, 3>).',
82+
57,
83+
],
84+
]);
85+
}
86+
7787
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Bug14468;
4+
5+
function test(string $s): void
6+
{
7+
$min = 0;
8+
$max = strlen($s);
9+
while ($min < $max) {
10+
$rand = random_int($min, $max);
11+
$min++;
12+
}
13+
}
14+
15+
/**
16+
* @param int<0, max> $a
17+
* @param int<0, max> $b
18+
*/
19+
function bothUnboundedMax(int $a, int $b): void
20+
{
21+
random_int($a, $b);
22+
}
23+
24+
/**
25+
* @param int<min, 0> $a
26+
* @param int<min, 0> $b
27+
*/
28+
function bothUnboundedMin(int $a, int $b): void
29+
{
30+
random_int($a, $b);
31+
}
32+
33+
/**
34+
* @param int<0, max> $min
35+
* @param int<0, 10> $max
36+
*/
37+
function unboundedMinBoundedMax(int $min, int $max): void
38+
{
39+
random_int($min, $max);
40+
}
41+
42+
/**
43+
* @param int<0, 10> $min
44+
* @param int<min, 10> $max
45+
*/
46+
function boundedMinUnboundedMax(int $min, int $max): void
47+
{
48+
random_int($min, $max);
49+
}
50+
51+
/**
52+
* @param int<5, max> $min
53+
* @param int<min, 3> $max
54+
*/
55+
function unboundedButDefinitelyWrong(int $min, int $max): void
56+
{
57+
random_int($min, $max); // error - max <= 3 < 5 <= min
58+
}
59+
60+
/** @param positive-int $positiveInt */
61+
function positiveInt(int $int, int $positiveInt): void
62+
{
63+
random_int($int, $int);
64+
random_int($positiveInt, $positiveInt);
65+
}

0 commit comments

Comments
 (0)