forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoWhileLoopConstantConditionRule.php
More file actions
101 lines (89 loc) · 2.63 KB
/
DoWhileLoopConstantConditionRule.php
File metadata and controls
101 lines (89 loc) · 2.63 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Comparison;
use PhpParser\Node;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\Continue_;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Node\DoWhileLoopConditionNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Constant\ConstantBooleanType;
use function sprintf;
/**
* @implements Rule<DoWhileLoopConditionNode>
*/
#[RegisteredRule(level: 4)]
final class DoWhileLoopConstantConditionRule implements Rule
{
public function __construct(
private ConstantConditionRuleHelper $helper,
#[AutowiredParameter]
private bool $treatPhpDocTypesAsCertain,
#[AutowiredParameter(ref: '%tips.treatPhpDocTypesAsCertain%')]
private bool $treatPhpDocTypesAsCertainTip,
)
{
}
public function getNodeType(): string
{
return DoWhileLoopConditionNode::class;
}
public function processNode(Node $node, Scope $scope): array
{
$exprType = $this->helper->getBooleanType($scope, $node->getCond());
if ($exprType instanceof ConstantBooleanType) {
if ($exprType->getValue()) {
foreach ($node->getExitPoints() as $exitPoint) {
$statement = $exitPoint->getStatement();
if (!$statement instanceof Continue_) {
return [];
}
if (!$statement->num instanceof Int_) {
continue;
}
if ($statement->num->value > 1) {
return [];
}
}
foreach ($node->getThrowPoints() as $throwPoint) {
if ($throwPoint->isExplicit()) {
return [];
}
}
} else {
foreach ($node->getExitPoints() as $exitPoint) {
$statement = $exitPoint->getStatement();
if ($statement instanceof Break_) {
return [];
}
}
}
$addTip = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $node): RuleErrorBuilder {
if (!$this->treatPhpDocTypesAsCertain) {
return $ruleErrorBuilder;
}
$booleanNativeType = $this->helper->getNativeBooleanType($scope, $node->getCond());
if ($booleanNativeType instanceof ConstantBooleanType) {
return $ruleErrorBuilder;
}
if (!$this->treatPhpDocTypesAsCertainTip) {
return $ruleErrorBuilder;
}
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};
return [
$addTip(RuleErrorBuilder::message(sprintf(
'Do-while loop condition is always %s.',
$exprType->getValue() ? 'true' : 'false',
)))
->line($node->getCond()->getStartLine())
->identifier(sprintf('doWhile.always%s', $exprType->getValue() ? 'True' : 'False'))
->build(),
];
}
return [];
}
}