Skip to content

Commit 8e45197

Browse files
authored
[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 11e634c commit 8e45197

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

config/phpstan-baseline.neon

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@ parameters:
4242
count: 1
4343
path: ../src/Rule/Rule.php
4444

45-
-
46-
message: '#^Loose comparison via "\!\=" is not allowed\.$#'
47-
identifier: notEqual.notAllowed
48-
count: 1
49-
path: ../src/RuleSet/DeclarationBlock.php
50-
5145
-
5246
message: '#^Parameters should have "string" types as the only types passed to this method$#'
5347
identifier: typePerfect.narrowPublicClassMethodParamType

src/RuleSet/DeclarationBlock.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,25 @@ public static function parse(ParserState $parserState, ?CSSList $list = null): ?
4141
$result = new DeclarationBlock($parserState->currentLine());
4242
try {
4343
$selectorParts = [];
44+
$stringWrapperCharacter = null;
4445
do {
4546
$selectorParts[] = $parserState->consume(1)
4647
. $parserState->consumeUntil(['{', '}', '\'', '"'], false, false, $comments);
47-
if (\in_array($parserState->peek(), ['\'', '"'], true) && \substr(\end($selectorParts), -1) != '\\') {
48-
if (!isset($stringWrapperCharacter)) {
49-
$stringWrapperCharacter = $parserState->peek();
50-
} elseif ($stringWrapperCharacter === $parserState->peek()) {
51-
unset($stringWrapperCharacter);
52-
}
48+
$nextCharacter = $parserState->peek();
49+
switch ($nextCharacter) {
50+
case '\'':
51+
// The fallthrough is intentional.
52+
case '"':
53+
if (!\is_string($stringWrapperCharacter)) {
54+
$stringWrapperCharacter = $nextCharacter;
55+
} elseif ($stringWrapperCharacter === $nextCharacter) {
56+
if (\substr(\end($selectorParts), -1) !== '\\') {
57+
$stringWrapperCharacter = null;
58+
}
59+
}
60+
break;
5361
}
54-
} while (!\in_array($parserState->peek(), ['{', '}'], true) || isset($stringWrapperCharacter));
62+
} while (!\in_array($nextCharacter, ['{', '}'], true) || \is_string($stringWrapperCharacter));
5563
$result->setSelectors(\implode('', $selectorParts), $list);
5664
if ($parserState->comes('{')) {
5765
$parserState->consume(1);

0 commit comments

Comments
 (0)