Skip to content

Commit d0a82a6

Browse files
committed
[CLEANUP] Tidy up DeclarationBlock::parse() (#1294)
- Assign the result of `ParserState::peek()` to a local variable, for efficiency; - Use a switch statement to branch on its value, for extensibility (e.g. #1292); - Don't unnecessarily test that a quote character is not escaped when not within a string.
1 parent fb32af0 commit d0a82a6

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/RuleSet/DeclarationBlock.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,27 @@ public static function parse(ParserState $oParserState, $oList = null)
5757
$aComments = [];
5858
$oResult = new DeclarationBlock($oParserState->currentLine());
5959
try {
60-
$aSelectorParts = [];
61-
$sStringWrapperChar = false;
60+
$selectorParts = [];
61+
$stringWrapperCharacter = null;
6262
do {
63-
$aSelectorParts[] = $oParserState->consume(1)
63+
$selectorParts[] = $oParserState->consume(1)
6464
. $oParserState->consumeUntil(['{', '}', '\'', '"'], false, false, $aComments);
65-
if (in_array($oParserState->peek(), ['\'', '"']) && substr(end($aSelectorParts), -1) != "\\") {
66-
if ($sStringWrapperChar === false) {
67-
$sStringWrapperChar = $oParserState->peek();
68-
} elseif ($sStringWrapperChar == $oParserState->peek()) {
69-
$sStringWrapperChar = false;
70-
}
65+
$nextCharacter = $oParserState->peek();
66+
switch ($nextCharacter) {
67+
case '\'':
68+
// The fallthrough is intentional.
69+
case '"':
70+
if (!\is_string($stringWrapperCharacter)) {
71+
$stringWrapperCharacter = $nextCharacter;
72+
} elseif ($stringWrapperCharacter === $nextCharacter) {
73+
if (\substr(\end($selectorParts), -1) !== '\\') {
74+
$stringWrapperCharacter = null;
75+
}
76+
}
77+
break;
7178
}
72-
} while (!in_array($oParserState->peek(), ['{', '}']) || $sStringWrapperChar !== false);
73-
$oResult->setSelectors(implode('', $aSelectorParts), $oList);
79+
} while (!\in_array($nextCharacter, ['{', '}'], true) || \is_string($stringWrapperCharacter));
80+
$oResult->setSelectors(\implode('', $selectorParts), $oList);
7481
if ($oParserState->comes('{')) {
7582
$oParserState->consume(1);
7683
}

0 commit comments

Comments
 (0)