Skip to content

Commit

Permalink
Only report maybe cases at level 7
Browse files Browse the repository at this point in the history
  • Loading branch information
cs278 committed Jan 16, 2020
1 parent c2c2b88 commit 5e4c5d8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
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) {
return [
RuleErrorBuilder::message(sprintf(
$message,
$minType->describe(VerbosityLevel::value()),
$maxType->describe(VerbosityLevel::value())
))->build(),
];
}
}
}

Expand Down

0 comments on commit 5e4c5d8

Please sign in to comment.