Skip to content

Commit b7e16bc

Browse files
committed
simplify PrintfHelper
1 parent b223e07 commit b7e16bc

File tree

1 file changed

+5
-39
lines changed

1 file changed

+5
-39
lines changed

src/Rules/Functions/PrintfHelper.php

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -161,46 +161,12 @@ private function getAcceptingTypeBySpecifier(string $specifier): string
161161

162162
private function getPlaceholdersCount(string $specifiersPattern, string $format): int
163163
{
164-
$addSpecifier = '';
165-
if ($this->phpVersion->supportsHhPrintfSpecifier()) {
166-
$addSpecifier .= 'hH';
167-
}
168-
169-
$specifiers = sprintf($specifiersPattern, $addSpecifier);
170-
171-
$pattern = '~(?<before>%*)%(?:(?<position>\d+)\$)?[-+]?(?:[ 0]|(?:\'[^%]))?(?<width>\*)?-?\d*(?:\.(?:\d+|(?<precision>\*))?)?' . $specifiers . '~';
172-
173-
$matches = Strings::matchAll($format, $pattern, PREG_SET_ORDER);
174-
175-
if (count($matches) === 0) {
176-
return 0;
177-
}
178-
179-
$placeholders = array_filter($matches, static fn (array $match): bool => strlen($match['before']) % 2 === 0);
180-
181-
if (count($placeholders) === 0) {
182-
return 0;
183-
}
184-
185-
$maxPositionedNumber = 0;
186-
$maxOrdinaryNumber = 0;
187-
foreach ($placeholders as $placeholder) {
188-
if (isset($placeholder['width']) && $placeholder['width'] !== '') {
189-
$maxOrdinaryNumber++;
190-
}
191-
192-
if (isset($placeholder['precision']) && $placeholder['precision'] !== '') {
193-
$maxOrdinaryNumber++;
194-
}
195-
196-
if (isset($placeholder['position']) && $placeholder['position'] !== '') {
197-
$maxPositionedNumber = max((int) $placeholder['position'], $maxPositionedNumber);
198-
} else {
199-
$maxOrdinaryNumber++;
200-
}
201-
}
164+
$paramIndices = array_keys($this->parsePlaceholders($specifiersPattern, $format));
202165

203-
return max($maxPositionedNumber, $maxOrdinaryNumber);
166+
return $paramIndices === []
167+
? 0
168+
// The indices start from 0
169+
: max($paramIndices) + 1;
204170
}
205171

206172
}

0 commit comments

Comments
 (0)