Skip to content

Commit 6d7ba0f

Browse files
committed
Fix random_int() without args crash
1 parent f2696a4 commit 6d7ba0f

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

Diff for: src/Rules/Functions/RandomIntParametersRule.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use PHPStan\Type\Constant\ConstantIntegerType;
1212
use PHPStan\Type\IntegerRangeType;
1313
use PHPStan\Type\VerbosityLevel;
14+
use function array_values;
15+
use function count;
1416
use function sprintf;
1517

1618
/**
@@ -38,8 +40,13 @@ public function processNode(Node $node, Scope $scope): array
3840
return [];
3941
}
4042

41-
$minType = $scope->getType($node->getArgs()[0]->value)->toInteger();
42-
$maxType = $scope->getType($node->getArgs()[1]->value)->toInteger();
43+
$args = array_values($node->getArgs());
44+
if (count($args) < 2) {
45+
return [];
46+
}
47+
48+
$minType = $scope->getType($args[0]->value)->toInteger();
49+
$maxType = $scope->getType($args[1]->value)->toInteger();
4350

4451
if (
4552
!$minType instanceof ConstantIntegerType && !$minType instanceof IntegerRangeType

Diff for: tests/PHPStan/Rules/Functions/RandomIntParametersRuleTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,9 @@ public function testFile(): void
5858
]);
5959
}
6060

61+
public function testBug6361(): void
62+
{
63+
$this->analyse([__DIR__ . '/data/bug-6361.php'], []);
64+
}
65+
6166
}

Diff for: tests/PHPStan/Rules/Functions/data/bug-6361.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace Bug6361;
4+
5+
random_int();

0 commit comments

Comments
 (0)