|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Comparison; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr\BinaryOp\Equal; |
| 7 | +use PHPStan\Analyser\CollectedDataEmitter; |
| 8 | +use PHPStan\Analyser\NodeCallbackInvoker; |
| 9 | +use PHPStan\Analyser\Scope; |
| 10 | +use PHPStan\Node\Printer\ExprPrinter; |
| 11 | +use PHPStan\Node\SwitchConditionNode; |
| 12 | +use PHPStan\Php\PhpVersion; |
| 13 | +use PHPStan\Rules\Rule; |
| 14 | +use PHPStan\Rules\RuleErrorBuilder; |
| 15 | +use PHPStan\Type\Type; |
| 16 | +use PHPStan\Type\VerbosityLevel; |
| 17 | +use function count; |
| 18 | +use function sprintf; |
| 19 | + |
| 20 | +/** |
| 21 | + * @implements Rule<SwitchConditionNode> |
| 22 | + */ |
| 23 | +final class SwitchConditionRule implements Rule |
| 24 | +{ |
| 25 | + |
| 26 | + public function __construct( |
| 27 | + private ConstantConditionRuleHelper $constantConditionRuleHelper, |
| 28 | + private PossiblyImpureTipHelper $possiblyImpureTipHelper, |
| 29 | + private ConstantConditionInTraitHelper $constantConditionInTraitHelper, |
| 30 | + private ExprPrinter $exprPrinter, |
| 31 | + private PhpVersion $phpVersion, |
| 32 | + private bool $treatPhpDocTypesAsCertain, |
| 33 | + ) |
| 34 | + { |
| 35 | + } |
| 36 | + |
| 37 | + public function getNodeType(): string |
| 38 | + { |
| 39 | + return SwitchConditionNode::class; |
| 40 | + } |
| 41 | + |
| 42 | + public function processNode(Node $node, Scope&NodeCallbackInvoker&CollectedDataEmitter $scope): array |
| 43 | + { |
| 44 | + $subject = $node->getSubject(); |
| 45 | + $errors = []; |
| 46 | + $nextCaseIsDeadForType = false; |
| 47 | + $nextCaseIsDeadForNativeType = false; |
| 48 | + $seenCases = []; |
| 49 | + |
| 50 | + foreach ($node->getArms() as $arm) { |
| 51 | + if ( |
| 52 | + $nextCaseIsDeadForNativeType |
| 53 | + || ($nextCaseIsDeadForType && $this->treatPhpDocTypesAsCertain) |
| 54 | + ) { |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + $armScope = $arm->getScope(); |
| 59 | + $caseCondition = $arm->getCaseCondition(); |
| 60 | + |
| 61 | + $caseConditionType = $armScope->getType($caseCondition); |
| 62 | + $finiteTypes = $caseConditionType->getFiniteTypes(); |
| 63 | + if (count($finiteTypes) === 1) { |
| 64 | + $caseValueType = $finiteTypes[0]; |
| 65 | + $firstSeen = null; |
| 66 | + foreach ($seenCases as $seenCase) { |
| 67 | + if ($this->isDuplicateCase($seenCase['type'], $caseValueType)) { |
| 68 | + $firstSeen = $seenCase; |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if ($firstSeen !== null) { |
| 74 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 75 | + 'Case %s in switch is a duplicate of case %s on line %d.', |
| 76 | + $this->exprPrinter->printExpr($caseCondition), |
| 77 | + $firstSeen['printed'], |
| 78 | + $firstSeen['line'], |
| 79 | + ))->line($arm->getLine())->identifier('switch.duplicateCase')->build(); |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + $seenCases[] = [ |
| 84 | + 'type' => $caseValueType, |
| 85 | + 'printed' => $this->exprPrinter->printExpr($caseCondition), |
| 86 | + 'line' => $arm->getLine(), |
| 87 | + ]; |
| 88 | + } |
| 89 | + |
| 90 | + $conditionExpr = new Equal($subject, $caseCondition); |
| 91 | + |
| 92 | + $conditionType = $armScope->getType($conditionExpr); |
| 93 | + if (!$this->isConstantBoolean($conditionType)) { |
| 94 | + $this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $conditionExpr); |
| 95 | + continue; |
| 96 | + } |
| 97 | + if ($conditionType->isTrue()->yes()) { |
| 98 | + $nextCaseIsDeadForType = true; |
| 99 | + } |
| 100 | + |
| 101 | + if (!$this->treatPhpDocTypesAsCertain) { |
| 102 | + $conditionNativeType = $armScope->getNativeType($conditionExpr); |
| 103 | + if (!$this->isConstantBoolean($conditionNativeType)) { |
| 104 | + $this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $conditionExpr); |
| 105 | + continue; |
| 106 | + } |
| 107 | + if ($conditionNativeType->isTrue()->yes()) { |
| 108 | + $nextCaseIsDeadForNativeType = true; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + $subjectType = $armScope->getType($subject); |
| 113 | + if ($this->isConstantBoolean($subjectType)) { |
| 114 | + $caseConditionStandaloneType = $this->constantConditionRuleHelper->getBooleanType($armScope, $caseCondition); |
| 115 | + if (!$this->isConstantBoolean($caseConditionStandaloneType)) { |
| 116 | + $this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $conditionExpr); |
| 117 | + continue; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if ($conditionType->isFalse()->yes()) { |
| 122 | + $errorBuilder = RuleErrorBuilder::message(sprintf( |
| 123 | + 'Switch condition comparison between %s and %s is always false.', |
| 124 | + $subjectType->describe(VerbosityLevel::value()), |
| 125 | + $caseConditionType->describe(VerbosityLevel::value()), |
| 126 | + ))->line($arm->getLine())->identifier('switch.alwaysFalse'); |
| 127 | + $this->possiblyImpureTipHelper->addTip($armScope, $conditionExpr, $errorBuilder); |
| 128 | + $ruleError = $errorBuilder->build(); |
| 129 | + if ($scope->isInTrait()) { |
| 130 | + $this->constantConditionInTraitHelper->emitError(self::class, $scope, $conditionExpr, false, $ruleError); |
| 131 | + } else { |
| 132 | + $errors[] = $ruleError; |
| 133 | + } |
| 134 | + continue; |
| 135 | + } |
| 136 | + |
| 137 | + if ($arm->isLast()) { |
| 138 | + $this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $conditionExpr); |
| 139 | + continue; |
| 140 | + } |
| 141 | + |
| 142 | + $errorBuilder = RuleErrorBuilder::message(sprintf( |
| 143 | + 'Switch condition comparison between %s and %s is always true.', |
| 144 | + $subjectType->describe(VerbosityLevel::value()), |
| 145 | + $armScope->getType($caseCondition)->describe(VerbosityLevel::value()), |
| 146 | + ))->line($arm->getLine())->identifier('switch.alwaysTrue') |
| 147 | + ->tip('Remove remaining cases below this one and this error will disappear too.'); |
| 148 | + $this->possiblyImpureTipHelper->addTip($armScope, $conditionExpr, $errorBuilder); |
| 149 | + $ruleError = $errorBuilder->build(); |
| 150 | + if ($scope->isInTrait()) { |
| 151 | + $this->constantConditionInTraitHelper->emitError(self::class, $scope, $conditionExpr, true, $ruleError); |
| 152 | + } else { |
| 153 | + $errors[] = $ruleError; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + return $errors; |
| 158 | + } |
| 159 | + |
| 160 | + private function isConstantBoolean(Type $type): bool |
| 161 | + { |
| 162 | + return $type->isTrue()->yes() || $type->isFalse()->yes(); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * A later `case` is a duplicate of an earlier one when both match the exact |
| 167 | + * same set of subject values. Besides identical values, `switch` compares |
| 168 | + * with loose `==`, so two numerically-equal constants (e.g. 1, '1' and 1.0) |
| 169 | + * are duplicates too - they cannot be told apart by a `switch`. |
| 170 | + */ |
| 171 | + private function isDuplicateCase(Type $seenType, Type $caseValueType): bool |
| 172 | + { |
| 173 | + if ($seenType->equals($caseValueType)) { |
| 174 | + return true; |
| 175 | + } |
| 176 | + |
| 177 | + return $seenType->looseCompare($caseValueType, $this->phpVersion)->isTrue()->yes(); |
| 178 | + } |
| 179 | + |
| 180 | +} |
0 commit comments