Skip to content

Commit ca3477d

Browse files
committed
replace match with switch
1 parent f790571 commit ca3477d

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

src/Rules/Functions/PrintfHelper.php

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use function array_map;
1515
use function array_reduce;
1616
use function count;
17+
use function in_array;
1718
use function max;
1819
use function sort;
1920
use function sprintf;
@@ -64,14 +65,24 @@ public function getPrintfPlaceholderAcceptingTypes(string $format): array
6465
$typeName,
6566
static function (Type $t) use ($types): bool {
6667
foreach ($types as $acceptingType) {
67-
$subresult = match ($acceptingType) {
68-
'strict-int' => (new IntegerType())->accepts($t, true)->yes(),
69-
'int' => ! $t->toInteger() instanceof ErrorType,
70-
'float' => ! $t->toFloat() instanceof ErrorType,
68+
switch ($acceptingType) {
69+
case 'strict-int':
70+
$subresult = (new IntegerType())->accepts($t, true)->yes();
71+
break;
72+
case 'int':
73+
$subresult = ! $t->toInteger() instanceof ErrorType;
74+
break;
75+
case 'float':
76+
$subresult = ! $t->toFloat() instanceof ErrorType;
77+
break;
7178
// The function signature already limits the parameters to stringable types, so there's
7279
// no point in checking string again here.
73-
'string', 'mixed' => true,
74-
};
80+
case 'string':
81+
case 'mixed':
82+
default:
83+
$subresult = true;
84+
break;
85+
}
7586

7687
if (!$subresult) {
7788
return false;
@@ -149,12 +160,19 @@ private function parsePlaceholders(string $specifiersPattern, string $format): a
149160
/** @phpstan-return 'string'|'int'|'float'|'mixed' */
150161
private function getAcceptingTypeBySpecifier(string $specifier): string
151162
{
152-
return match ($specifier) {
153-
's' => 'string',
154-
'd', 'u', 'c', 'o', 'x', 'X', 'b' => 'int',
155-
'e', 'E', 'f', 'F', 'g', 'G', 'h', 'H' => 'float',
156-
default => 'mixed',
157-
};
163+
if ($specifier === 's') {
164+
return 'string';
165+
}
166+
167+
if (in_array($specifier, ['d', 'u', 'c', 'o', 'x', 'X', 'b'], true)) {
168+
return 'int';
169+
}
170+
171+
if (in_array($specifier, ['e', 'E', 'f', 'F', 'g', 'G', 'h', 'H'], true)) {
172+
return 'float';
173+
}
174+
175+
return 'mixed';
158176
}
159177

160178
private function getPlaceholdersCount(string $specifiersPattern, string $format): int

0 commit comments

Comments
 (0)