Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

random_int() return type and parameters rule #99

Merged
merged 9 commits into from Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions conf/config.level5.neon
Expand Up @@ -14,3 +14,5 @@ parameters:
services:
-
class: PHPStan\Rules\Functions\RandomIntParametersRule
arguments:
reportMaybes: %reportMaybes%
25 changes: 17 additions & 8 deletions src/Rules/Functions/RandomIntParametersRule.php
Expand Up @@ -21,9 +21,13 @@ class RandomIntParametersRule implements \PHPStan\Rules\Rule
/** @var ReflectionProvider */
private $reflectionProvider;

public function __construct(ReflectionProvider $reflectionProvider)
/** @var bool */
private $reportMaybes;

public function __construct(ReflectionProvider $reflectionProvider, bool $reportMaybes)
{
$this->reflectionProvider = $reflectionProvider;
$this->reportMaybes = $reportMaybes;
}

public function getNodeType(): string
Expand Down Expand Up @@ -59,13 +63,18 @@ public function processNode(Node $node, Scope $scope): array
if (!$maxPermittedType->isSuperTypeOf($maxType)->yes()) {
$message = 'Parameter #1 $min (%s) of function random_int expects lower number than parameter #2 $max (%s).';

return [
RuleErrorBuilder::message(sprintf(
$message,
$minType->describe(VerbosityLevel::value()),
$maxType->describe(VerbosityLevel::value())
))->build(),
];
// True if sometimes the parameters conflict.
$isMaybe = !$maxType->isSuperTypeOf($minType)->no();

if (!$isMaybe || $this->reportMaybes) {
cs278 marked this conversation as resolved.
Show resolved Hide resolved
return [
RuleErrorBuilder::message(sprintf(
$message,
$minType->describe(VerbosityLevel::value()),
$maxType->describe(VerbosityLevel::value())
))->build(),
];
}
}
}

Expand Down