forked from phpstan/phpstan-strict-rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBooleanInWhileConditionRule.php
46 lines (36 loc) · 1001 Bytes
/
BooleanInWhileConditionRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php declare(strict_types = 1);
namespace PHPStan\Rules\BooleansInConditions;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\VerbosityLevel;
use function sprintf;
/**
* @implements Rule<Node\Stmt\While_>
*/
class BooleanInWhileConditionRule implements Rule
{
private BooleanRuleHelper $helper;
public function __construct(BooleanRuleHelper $helper)
{
$this->helper = $helper;
}
public function getNodeType(): string
{
return Node\Stmt\While_::class;
}
public function processNode(Node $node, Scope $scope): array
{
if ($this->helper->passesAsBoolean($scope, $node->cond)) {
return [];
}
$conditionExpressionType = $scope->getType($node->cond);
return [
RuleErrorBuilder::message(sprintf(
'Only booleans are allowed in a while condition, %s given.',
$conditionExpressionType->describe(VerbosityLevel::typeOnly()),
))->identifier('while.condNotBoolean')->build(),
];
}
}