Repository navigation
Expand file tree
/
Copy pathBooleanNotConstantConditionRule.php
More file actions
86 lines (72 loc) · 2.7 KB
/
Copy pathBooleanNotConstantConditionRule.php
File metadata and controls
86 lines (72 loc) · 2.7 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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Comparison;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Parser\LastConditionVisitor;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Constant\ConstantBooleanType;
use function sprintf;
/**
* @implements Rule<Node\Expr\BooleanNot>
*/
#[RegisteredRule(level: 4)]
final class BooleanNotConstantConditionRule implements Rule
{
public function __construct(
private ConstantConditionRuleHelper $helper,
private PossiblyImpureTipHelper $possiblyImpureTipHelper,
#[AutowiredParameter]
private bool $treatPhpDocTypesAsCertain,
#[AutowiredParameter]
private bool $reportAlwaysTrueInLastCondition,
#[AutowiredParameter(ref: '%tips.treatPhpDocTypesAsCertain%')]
private bool $treatPhpDocTypesAsCertainTip,
)
{
}
public function getNodeType(): string
{
return Node\Expr\BooleanNot::class;
}
public function processNode(
Node $node,
Scope $scope,
): array
{
$exprType = $this->helper->getBooleanType($scope, $node->expr);
if ($exprType instanceof ConstantBooleanType) {
$addTip = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $node): RuleErrorBuilder {
if (!$this->treatPhpDocTypesAsCertain) {
return $this->possiblyImpureTipHelper->addTip($scope, $node->expr, $ruleErrorBuilder);
}
$booleanNativeType = $this->helper->getNativeBooleanType($scope, $node->expr);
if ($booleanNativeType instanceof ConstantBooleanType) {
return $this->possiblyImpureTipHelper->addTip($scope, $node->expr, $ruleErrorBuilder);
}
if (!$this->treatPhpDocTypesAsCertainTip) {
return $this->possiblyImpureTipHelper->addTip($scope, $node->expr, $ruleErrorBuilder);
}
$ruleErrorBuilder = $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
return $this->possiblyImpureTipHelper->addTip($scope, $node->expr, $ruleErrorBuilder);
};
$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
if ($exprType->getValue() || $isLast !== true || $this->reportAlwaysTrueInLastCondition) {
$errorBuilder = $addTip(RuleErrorBuilder::message(sprintf(
'Negated boolean expression is always %s.',
$exprType->getValue() ? 'false' : 'true',
)))->line($node->expr->getStartLine());
if (!$exprType->getValue() && $isLast === false && !$this->reportAlwaysTrueInLastCondition) {
$errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.');
}
$errorBuilder->identifier(sprintf('booleanNot.always%s', $exprType->getValue() ? 'False' : 'True'));
return [
$errorBuilder->build(),
];
}
}
return [];
}
}