Skip to content

Commit

Permalink
Fix random_int() without args crash
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 14, 2022
1 parent f2696a4 commit 6d7ba0f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Rules/Functions/RandomIntParametersRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\VerbosityLevel;
use function array_values;
use function count;
use function sprintf;

/**
Expand Down Expand Up @@ -38,8 +40,13 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$minType = $scope->getType($node->getArgs()[0]->value)->toInteger();
$maxType = $scope->getType($node->getArgs()[1]->value)->toInteger();
$args = array_values($node->getArgs());
if (count($args) < 2) {

This comment has been minimized.

Copy link
@staabm

staabm Jan 14, 2022

Contributor

I had a similar problem yesterday in a custom rule.. it made me think whether we could/should have a rule which detect that the arg count was checked before the array-lookup can done..

or maybe we can improve the typing somehow so existing rules can catch this situation?

return [];
}

$minType = $scope->getType($args[0]->value)->toInteger();
$maxType = $scope->getType($args[1]->value)->toInteger();

if (
!$minType instanceof ConstantIntegerType && !$minType instanceof IntegerRangeType
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Functions/RandomIntParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ public function testFile(): void
]);
}

public function testBug6361(): void
{
$this->analyse([__DIR__ . '/data/bug-6361.php'], []);
}

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

namespace Bug6361;

random_int();

0 comments on commit 6d7ba0f

Please sign in to comment.