-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
random_int() return type and parameters rule
- Loading branch information
1 parent
4724469
commit 7e57ca0
Showing
12 changed files
with
364 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
parameters: | ||
featureToggles: | ||
randomIntParameters: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,18 @@ | ||
includes: | ||
- config.level4.neon | ||
|
||
conditionalTags: | ||
PHPStan\Rules\Functions\RandomIntParametersRule: | ||
phpstan.rules.rule: %featureToggles.randomIntParameters% | ||
|
||
parameters: | ||
checkFunctionArgumentTypes: true | ||
checkArgumentsPassedByReference: true | ||
featureToggles: | ||
randomIntParameters: false | ||
|
||
services: | ||
- | ||
class: PHPStan\Rules\Functions\RandomIntParametersRule | ||
arguments: | ||
reportMaybes: %reportMaybes% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Functions; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\Constant\ConstantIntegerType; | ||
use PHPStan\Type\IntegerRangeType; | ||
use PHPStan\Type\IntegerType; | ||
use PHPStan\Type\VerbosityLevel; | ||
|
||
/** | ||
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr\FuncCall> | ||
*/ | ||
class RandomIntParametersRule implements \PHPStan\Rules\Rule | ||
{ | ||
|
||
/** @var ReflectionProvider */ | ||
private $reflectionProvider; | ||
|
||
/** @var bool */ | ||
private $reportMaybes; | ||
|
||
public function __construct(ReflectionProvider $reflectionProvider, bool $reportMaybes) | ||
{ | ||
$this->reflectionProvider = $reflectionProvider; | ||
$this->reportMaybes = $reportMaybes; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return FuncCall::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!($node->name instanceof \PhpParser\Node\Name)) { | ||
return []; | ||
} | ||
|
||
if ($this->reflectionProvider->resolveFunctionName($node->name, $scope) !== 'random_int') { | ||
return []; | ||
} | ||
|
||
$minType = $scope->getType($node->args[0]->value)->toInteger(); | ||
$maxType = $scope->getType($node->args[1]->value)->toInteger(); | ||
$integerType = new IntegerType(); | ||
|
||
if ($minType->equals($integerType) || $maxType->equals($integerType)) { | ||
return []; | ||
} | ||
|
||
if ($minType instanceof ConstantIntegerType || $minType instanceof IntegerRangeType) { | ||
if ($minType instanceof ConstantIntegerType) { | ||
$maxPermittedType = IntegerRangeType::fromInterval($minType->getValue(), PHP_INT_MAX); | ||
} else { | ||
$maxPermittedType = IntegerRangeType::fromInterval($minType->getMax(), PHP_INT_MAX); | ||
} | ||
|
||
if (!$maxPermittedType->isSuperTypeOf($maxType)->yes()) { | ||
$message = 'Parameter #1 $min (%s) of function random_int expects lower number than parameter #2 $max (%s).'; | ||
|
||
// True if sometimes the parameters conflict. | ||
$isMaybe = !$maxType->isSuperTypeOf($minType)->no(); | ||
|
||
if (!$isMaybe || $this->reportMaybes) { | ||
return [ | ||
RuleErrorBuilder::message(sprintf( | ||
$message, | ||
$minType->describe(VerbosityLevel::value()), | ||
$maxType->describe(VerbosityLevel::value()) | ||
))->build(), | ||
]; | ||
} | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\Type\Constant\ConstantIntegerType; | ||
use PHPStan\Type\IntegerRangeType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\UnionType; | ||
|
||
class RandomIntFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension | ||
{ | ||
|
||
public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
{ | ||
return $functionReflection->getName() === 'random_int'; | ||
} | ||
|
||
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type | ||
{ | ||
if (count($functionCall->args) < 2) { | ||
return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType(); | ||
} | ||
|
||
$minType = $scope->getType($functionCall->args[0]->value)->toInteger(); | ||
$maxType = $scope->getType($functionCall->args[1]->value)->toInteger(); | ||
|
||
return $this->createRange($minType, $maxType); | ||
} | ||
|
||
private function createRange(Type $minType, Type $maxType): Type | ||
{ | ||
$minValue = array_reduce($minType instanceof UnionType ? $minType->getTypes() : [$minType], static function (int $carry, Type $type): int { | ||
if ($type instanceof IntegerRangeType) { | ||
$value = $type->getMin(); | ||
} elseif ($type instanceof ConstantIntegerType) { | ||
$value = $type->getValue(); | ||
} else { | ||
$value = PHP_INT_MIN; | ||
} | ||
|
||
return min($value, $carry); | ||
}, PHP_INT_MAX); | ||
|
||
$maxValue = array_reduce($maxType instanceof UnionType ? $maxType->getTypes() : [$maxType], static function (int $carry, Type $type): int { | ||
if ($type instanceof IntegerRangeType) { | ||
$value = $type->getMax(); | ||
} elseif ($type instanceof ConstantIntegerType) { | ||
$value = $type->getValue(); | ||
} else { | ||
$value = PHP_INT_MAX; | ||
} | ||
|
||
return max($value, $carry); | ||
}, PHP_INT_MIN); | ||
|
||
return IntegerRangeType::fromInterval($minValue, $maxValue); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
use function PHPStan\Analyser\assertType; | ||
|
||
function (int $min) { | ||
\assert($min === 10 || $min === 15); | ||
assertType('int<10, 20>', random_int($min, 20)); | ||
}; | ||
|
||
function (int $min) { | ||
\assert($min <= 0); | ||
assertType('int<min, 20>', random_int($min, 20)); | ||
}; | ||
|
||
function (int $max) { | ||
\assert($min >= 0); | ||
assertType('int<0, max>', random_int(0, $max)); | ||
}; | ||
|
||
function (int $i) { | ||
assertType('int', random_int($i, $i)); | ||
}; | ||
|
||
assertType('0', random_int(0, 0)); | ||
assertType('int', random_int(PHP_INT_MIN, PHP_INT_MAX)); | ||
assertType('int<0, max>', random_int(0, PHP_INT_MAX)); | ||
assertType('int<min, 0>', random_int(PHP_INT_MIN, 0)); | ||
assertType('int<-1, 1>', random_int(-1, 1)); | ||
assertType('int<0, 30>', random_int(0, random_int(0, 30))); | ||
assertType('int<0, 100>', random_int(random_int(0, 10), 100)); | ||
|
||
assertType('*NEVER*', random_int(10, 1)); | ||
assertType('*NEVER*', random_int(2, random_int(0, 1))); | ||
assertType('int<0, 1>', random_int(0, random_int(0, 1))); | ||
assertType('*NEVER*', random_int(random_int(0, 1), -1)); | ||
assertType('int<0, 1>', random_int(random_int(0, 1), 1)); | ||
|
||
assertType('int<-5, 5>', random_int(random_int(-5, 0), random_int(0, 5))); | ||
assertType('int', random_int(random_int(PHP_INT_MIN, 0), random_int(0, PHP_INT_MAX))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
tests/PHPStan/Rules/Functions/RandomIntParametersRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Functions; | ||
|
||
/** | ||
* @extends \PHPStan\Testing\RuleTestCase<RandomIntParametersRule> | ||
*/ | ||
class RandomIntParametersRuleTest extends \PHPStan\Testing\RuleTestCase | ||
{ | ||
|
||
protected function getRule(): \PHPStan\Rules\Rule | ||
{ | ||
return new RandomIntParametersRule($this->createReflectionProvider(), true); | ||
} | ||
|
||
public function testFile(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/random-int.php'], [ | ||
[ | ||
'Parameter #1 $min (1) of function random_int expects lower number than parameter #2 $max (0).', | ||
8, | ||
], | ||
[ | ||
'Parameter #1 $min (0) of function random_int expects lower number than parameter #2 $max (-1).', | ||
9, | ||
], | ||
[ | ||
'Parameter #1 $min (0) of function random_int expects lower number than parameter #2 $max (int<-10, -1>).', | ||
11, | ||
], | ||
[ | ||
'Parameter #1 $min (0) of function random_int expects lower number than parameter #2 $max (int<-10, 10>).', | ||
12, | ||
], | ||
[ | ||
'Parameter #1 $min (int<1, 10>) of function random_int expects lower number than parameter #2 $max (0).', | ||
15, | ||
], | ||
[ | ||
'Parameter #1 $min (int<-10, 10>) of function random_int expects lower number than parameter #2 $max (0).', | ||
16, | ||
], | ||
[ | ||
'Parameter #1 $min (int<-5, 1>) of function random_int expects lower number than parameter #2 $max (int<0, 5>).', | ||
19, | ||
], | ||
[ | ||
'Parameter #1 $min (int<-5, 0>) of function random_int expects lower number than parameter #2 $max (int<-1, 5>).', | ||
20, | ||
], | ||
[ | ||
'Parameter #1 $min (int<0, 10>) of function random_int expects lower number than parameter #2 $max (int<0, 10>).', | ||
31, | ||
], | ||
]); | ||
} | ||
|
||
} |
Oops, something went wrong.