Skip to content

Commit e3055ae

Browse files
enumagondrejmirtes
authored andcommitted
Fix mt_rand return type
1 parent 199f955 commit e3055ae

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

src/Type/Php/RandomIntFunctionReturnTypeExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ class RandomIntFunctionReturnTypeExtension implements DynamicFunctionReturnTypeE
2323

2424
public function isFunctionSupported(FunctionReflection $functionReflection): bool
2525
{
26-
return in_array($functionReflection->getName(), ['random_int', 'rand'], true);
26+
return in_array($functionReflection->getName(), ['random_int', 'rand', 'mt_rand'], true);
2727
}
2828

2929
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
3030
{
31-
if ($functionReflection->getName() === 'rand' && count($functionCall->getArgs()) === 0) {
31+
if (in_array($functionReflection->getName(), ['rand', 'mt_rand'], true) && count($functionCall->getArgs()) === 0) {
3232
return IntegerRangeType::fromInterval(0, null);
3333
}
3434

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,7 @@ public function dataFileAsserts(): iterable
11211121
yield from $this->gatherAssertTypes(__DIR__ . '/data/closure-retain-expression-types.php');
11221122
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7913.php');
11231123
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Functions/data/bug-8280.php');
1124+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8272.php');
11241125
}
11251126

11261127
/**

tests/PHPStan/Analyser/data/bug-4091.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
if (mt_rand(0,10) > 3) {
88
echo 'Fizz';
9-
assertType('int', mt_rand(0,10));
9+
assertType('int<0, 10>', mt_rand(0,10));
1010
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Bug8272;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
function test(): void {
8+
assertType('int<1, 5>', mt_rand(1, 5));
9+
assertType('int<0, max>', mt_rand());
10+
};

0 commit comments

Comments
 (0)