|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Comparison; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr\BinaryOp\LogicalXor; |
| 7 | +use PHPStan\Analyser\Scope; |
| 8 | +use PHPStan\Parser\LastConditionVisitor; |
| 9 | +use PHPStan\Rules\Rule; |
| 10 | +use PHPStan\Rules\RuleErrorBuilder; |
| 11 | +use PHPStan\Type\Constant\ConstantBooleanType; |
| 12 | +use function sprintf; |
| 13 | + |
| 14 | +/** |
| 15 | + * @implements Rule<LogicalXor> |
| 16 | + */ |
| 17 | +class LogicalXorConstantConditionRule implements Rule |
| 18 | +{ |
| 19 | + |
| 20 | + public function __construct( |
| 21 | + private ConstantConditionRuleHelper $helper, |
| 22 | + private bool $treatPhpDocTypesAsCertain, |
| 23 | + private bool $reportAlwaysTrueInLastCondition, |
| 24 | + ) |
| 25 | + { |
| 26 | + } |
| 27 | + |
| 28 | + public function getNodeType(): string |
| 29 | + { |
| 30 | + return LogicalXor::class; |
| 31 | + } |
| 32 | + |
| 33 | + public function processNode(Node $node, Scope $scope): array |
| 34 | + { |
| 35 | + $errors = []; |
| 36 | + $leftType = $this->helper->getBooleanType($scope, $node->left); |
| 37 | + $tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.'; |
| 38 | + if ($leftType instanceof ConstantBooleanType) { |
| 39 | + $addTipLeft = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $tipText, $node): RuleErrorBuilder { |
| 40 | + if (!$this->treatPhpDocTypesAsCertain) { |
| 41 | + return $ruleErrorBuilder; |
| 42 | + } |
| 43 | + |
| 44 | + $booleanNativeType = $this->helper->getNativeBooleanType($scope, $node->left); |
| 45 | + if ($booleanNativeType instanceof ConstantBooleanType) { |
| 46 | + return $ruleErrorBuilder; |
| 47 | + } |
| 48 | + |
| 49 | + return $ruleErrorBuilder->tip($tipText); |
| 50 | + }; |
| 51 | + |
| 52 | + $isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME); |
| 53 | + if (!$leftType->getValue() || $isLast !== true || $this->reportAlwaysTrueInLastCondition) { |
| 54 | + $errorBuilder = $addTipLeft(RuleErrorBuilder::message(sprintf( |
| 55 | + 'Left side of xor is always %s.', |
| 56 | + $leftType->getValue() ? 'true' : 'false', |
| 57 | + )))->line($node->left->getLine()); |
| 58 | + if ($leftType->getValue() && $isLast === false && !$this->reportAlwaysTrueInLastCondition) { |
| 59 | + $errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.'); |
| 60 | + } |
| 61 | + $errors[] = $errorBuilder->build(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + $rightType = $this->helper->getBooleanType($scope, $node->right); |
| 66 | + if ($rightType instanceof ConstantBooleanType && !$scope->isInFirstLevelStatement()) { |
| 67 | + $addTipRight = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $node, $tipText): RuleErrorBuilder { |
| 68 | + if (!$this->treatPhpDocTypesAsCertain) { |
| 69 | + return $ruleErrorBuilder; |
| 70 | + } |
| 71 | + |
| 72 | + $booleanNativeType = $this->helper->getNativeBooleanType( |
| 73 | + $scope, |
| 74 | + $node->right, |
| 75 | + ); |
| 76 | + if ($booleanNativeType instanceof ConstantBooleanType) { |
| 77 | + return $ruleErrorBuilder; |
| 78 | + } |
| 79 | + |
| 80 | + return $ruleErrorBuilder->tip($tipText); |
| 81 | + }; |
| 82 | + |
| 83 | + $isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME); |
| 84 | + if (!$rightType->getValue() || $isLast !== true || $this->reportAlwaysTrueInLastCondition) { |
| 85 | + $errorBuilder = $addTipRight(RuleErrorBuilder::message(sprintf( |
| 86 | + 'Right side of xor is always %s.', |
| 87 | + $rightType->getValue() ? 'true' : 'false', |
| 88 | + )))->line($node->right->getLine()); |
| 89 | + if ($rightType->getValue() && $isLast === false && !$this->reportAlwaysTrueInLastCondition) { |
| 90 | + $errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.'); |
| 91 | + } |
| 92 | + $errors[] = $errorBuilder->build(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return $errors; |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments