Skip to content

Commit dc03471

Browse files
committed
I don't see why we should handle only numeric constant values
1 parent e89f8e4 commit dc03471

2 files changed

Lines changed: 9 additions & 14 deletions

File tree

src/Rules/Comparison/SwitchConditionRule.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -166,28 +166,15 @@ private function isConstantBoolean(Type $type): bool
166166
* A later `case` is a duplicate of an earlier one when both match the exact
167167
* same set of subject values. Besides identical values, `switch` compares
168168
* 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`. Booleans,
170-
* null and non-numeric strings are intentionally left to the strict check
171-
* because their loose-comparison match sets are broader than a single value.
169+
* are duplicates too - they cannot be told apart by a `switch`.
172170
*/
173171
private function isDuplicateCase(Type $seenType, Type $caseValueType): bool
174172
{
175173
if ($seenType->equals($caseValueType)) {
176174
return true;
177175
}
178176

179-
if (!$this->isNumericConstant($seenType) || !$this->isNumericConstant($caseValueType)) {
180-
return false;
181-
}
182-
183177
return $seenType->looseCompare($caseValueType, $this->phpVersion)->isTrue()->yes();
184178
}
185179

186-
private function isNumericConstant(Type $type): bool
187-
{
188-
return $type->isInteger()->yes()
189-
|| $type->isFloat()->yes()
190-
|| $type->isNumericString()->yes();
191-
}
192-
193180
}

tests/PHPStan/Rules/Comparison/SwitchConditionRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ public function testRule(): void
103103
'Case 1.0 in switch is a duplicate of case 1 on line 149.',
104104
153,
105105
],
106+
[
107+
'Case true in switch is a duplicate of case 1 on line 149.',
108+
155,
109+
],
110+
[
111+
'Case false in switch is a duplicate of case 0 on line 157.',
112+
159,
113+
],
106114
]);
107115
}
108116

0 commit comments

Comments
 (0)