Skip to content

Commit a14f582

Browse files
committed
[CLEANUP] Extract method DeclarationBlock::parseSelector
Closes #1324
1 parent 1b1f91c commit a14f582

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

src/RuleSet/DeclarationBlock.php

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -283,19 +283,33 @@ public function render(OutputFormat $outputFormat): string
283283
private static function parseSelectors(ParserState $parserState, array &$comments = []): array
284284
{
285285
$selectors = [];
286+
287+
while (true) {
288+
$selectors[] = self::parseSelector($parserState, $comments);
289+
if (!$parserState->consumeIfComes(',')) {
290+
break;
291+
}
292+
}
293+
294+
return $selectors;
295+
}
296+
297+
/**
298+
* @param list<Comment> $comments
299+
*
300+
* @throws UnexpectedTokenException
301+
*/
302+
private static function parseSelector(ParserState $parserState, array &$comments = []): string
303+
{
286304
$selectorParts = [];
287305
$stringWrapperCharacter = null;
288306
$functionNestingLevel = 0;
289-
$consumedNextCharacter = false;
290307
static $stopCharacters = ['{', '}', '\'', '"', '(', ')', ','];
291308

292-
do {
293-
if (!$consumedNextCharacter) {
294-
$selectorParts[] = $parserState->consume(1);
295-
}
309+
while (true) {
310+
$selectorParts[] = $parserState->consume(1);
296311
$selectorParts[] = $parserState->consumeUntil($stopCharacters, false, false, $comments);
297312
$nextCharacter = $parserState->peek();
298-
$consumedNextCharacter = false;
299313
switch ($nextCharacter) {
300314
case '\'':
301315
// The fallthrough is intentional.
@@ -321,22 +335,24 @@ private static function parseSelectors(ParserState $parserState, array &$comment
321335
--$functionNestingLevel;
322336
}
323337
break;
338+
case '{':
339+
case '}':
340+
if (!\is_string($stringWrapperCharacter)) {
341+
break 2;
342+
}
343+
break;
324344
case ',':
325345
if (!\is_string($stringWrapperCharacter) && $functionNestingLevel === 0) {
326-
$selectors[] = \implode('', $selectorParts);
327-
$selectorParts = [];
328-
$parserState->consume(1);
329-
$consumedNextCharacter = true;
346+
break 2;
330347
}
331348
break;
332349
}
333-
} while (!\in_array($nextCharacter, ['{', '}'], true) || \is_string($stringWrapperCharacter));
350+
}
334351

335352
if ($functionNestingLevel !== 0) {
336353
throw new UnexpectedTokenException(')', $nextCharacter);
337354
}
338-
$selectors[] = \implode('', $selectorParts); // add final or only selector
339355

340-
return $selectors;
356+
return \implode('', $selectorParts);
341357
}
342358
}

0 commit comments

Comments
 (0)