From 037c649dc220be748568c28ec6bcf4a0d18daff1 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Thu, 4 Jul 2024 09:29:24 +0200 Subject: [PATCH] [CLEANUP] Use single-quoted string literals whenever possible Fixes #640 --- config/php-cs-fixer.php | 1 + src/CSSList/CSSList.php | 10 ++++---- src/OutputFormat.php | 6 ++--- src/Parsing/ParserState.php | 2 +- src/Property/Import.php | 2 +- src/Rule/Rule.php | 2 +- src/RuleSet/DeclarationBlock.php | 16 ++++++------- src/RuleSet/RuleSet.php | 2 +- src/Value/CSSString.php | 2 +- src/Value/Size.php | 6 ++--- src/Value/Value.php | 12 +++++----- tests/OutputFormatTest.php | 2 +- tests/ParserTest.php | 34 ++++++++++++++-------------- tests/RuleSet/LenientParsingTest.php | 4 ++-- 14 files changed, 51 insertions(+), 50 deletions(-) diff --git a/config/php-cs-fixer.php b/config/php-cs-fixer.php index c814a0d5..850af259 100644 --- a/config/php-cs-fixer.php +++ b/config/php-cs-fixer.php @@ -92,6 +92,7 @@ 'strict_param' => true, // string notation + 'single_quote' => true, 'string_implicit_backslashes' => ['single_quoted' => 'escape'], ] ); diff --git a/src/CSSList/CSSList.php b/src/CSSList/CSSList.php index dede7bb4..a99a6049 100644 --- a/src/CSSList/CSSList.php +++ b/src/CSSList/CSSList.php @@ -92,7 +92,7 @@ public static function parseList(ParserState $oParserState, CSSList $oList): voi } $oList->addComments($aComments); if (!$bIsRoot && !$bLenientParsing) { - throw new SourceException("Unexpected end of document", $oParserState->currentLine()); + throw new SourceException('Unexpected end of document', $oParserState->currentLine()); } } @@ -133,7 +133,7 @@ private static function parseListItem(ParserState $oParserState, CSSList $oList) if ($oParserState->getSettings()->bLenientParsing) { return DeclarationBlock::parse($oParserState); } else { - throw new SourceException("Unopened {", $oParserState->currentLine()); + throw new SourceException('Unopened {', $oParserState->currentLine()); } } else { // End of list @@ -205,11 +205,11 @@ private static function parseAtRule(ParserState $oParserState) } else { // Unknown other at rule (font-face or such) $sArgs = \trim($oParserState->consumeUntil('{', false, true)); - if (\substr_count($sArgs, "(") != \substr_count($sArgs, ")")) { + if (\substr_count($sArgs, '(') != \substr_count($sArgs, ')')) { if ($oParserState->getSettings()->bLenientParsing) { return null; } else { - throw new SourceException("Unmatched brace count in media query", $oParserState->currentLine()); + throw new SourceException('Unmatched brace count in media query', $oParserState->currentLine()); } } $bUseRuleSet = true; @@ -375,7 +375,7 @@ public function removeDeclarationBlockBySelector($mSelector, $bRemoveAll = false throw new UnexpectedTokenException( "Selector did not match '" . Selector::SELECTOR_VALIDATION_RX . "'.", $mSel, - "custom" + 'custom' ); } $mSel = new Selector($mSel); diff --git a/src/OutputFormat.php b/src/OutputFormat.php index 7465c50b..7ada5981 100644 --- a/src/OutputFormat.php +++ b/src/OutputFormat.php @@ -258,7 +258,7 @@ public function indentWithTabs($iNumber = 1) */ public function indentWithSpaces($iNumber = 2) { - return $this->setIndentation(\str_repeat(" ", $iNumber)); + return $this->setIndentation(\str_repeat(' ', $iNumber)); } /** @@ -314,8 +314,8 @@ public static function create(): OutputFormat public static function createCompact() { $format = self::create(); - $format->set('Space*Rules', "") - ->set('Space*Blocks', "") + $format->set('Space*Rules', '') + ->set('Space*Blocks', '') ->setSpaceAfterRuleName('') ->setSpaceBeforeOpeningBrace('') ->setSpaceAfterSelectorSeparator('') diff --git a/src/Parsing/ParserState.php b/src/Parsing/ParserState.php index 307308ff..cbfa89c9 100644 --- a/src/Parsing/ParserState.php +++ b/src/Parsing/ParserState.php @@ -193,7 +193,7 @@ public function parseCharacter($bIsForIdentifier) } } $iUnicode = \intval($sUnicode, 16); - $sUtf32 = ""; + $sUtf32 = ''; for ($i = 0; $i < 4; ++$i) { $sUtf32 .= \chr($iUnicode & 0xff); $iUnicode = $iUnicode >> 8; diff --git a/src/Property/Import.php b/src/Property/Import.php index 39f2a43a..04e80191 100644 --- a/src/Property/Import.php +++ b/src/Property/Import.php @@ -75,7 +75,7 @@ public function __toString(): string public function render(OutputFormat $oOutputFormat): string { - return $oOutputFormat->comments($this) . "@import " . $this->oLocation->render($oOutputFormat) + return $oOutputFormat->comments($this) . '@import ' . $this->oLocation->render($oOutputFormat) . ($this->sMediaQuery === null ? '' : ' ' . $this->sMediaQuery) . ';'; } diff --git a/src/Rule/Rule.php b/src/Rule/Rule.php index e7dbe74e..ca9f380c 100644 --- a/src/Rule/Rule.php +++ b/src/Rule/Rule.php @@ -78,7 +78,7 @@ public static function parse(ParserState $oParserState): Rule { $aComments = $oParserState->consumeWhiteSpace(); $oRule = new Rule( - $oParserState->parseIdentifier(!$oParserState->comes("--")), + $oParserState->parseIdentifier(!$oParserState->comes('--')), $oParserState->currentLine(), $oParserState->currentColumn() ); diff --git a/src/RuleSet/DeclarationBlock.php b/src/RuleSet/DeclarationBlock.php index f626b26b..6232a7fa 100644 --- a/src/RuleSet/DeclarationBlock.php +++ b/src/RuleSet/DeclarationBlock.php @@ -60,7 +60,7 @@ public static function parse(ParserState $oParserState, $oList = null) do { $aSelectorParts[] = $oParserState->consume(1) . $oParserState->consumeUntil(['{', '}', '\'', '"'], false, false, $aComments); - if (\in_array($oParserState->peek(), ['\'', '"'], true) && \substr(\end($aSelectorParts), -1) != "\\") { + if (\in_array($oParserState->peek(), ['\'', '"'], true) && \substr(\end($aSelectorParts), -1) != '\\') { if ($sStringWrapperChar === false) { $sStringWrapperChar = $oParserState->peek(); } elseif ($sStringWrapperChar == $oParserState->peek()) { @@ -107,7 +107,7 @@ public function setSelectors($mSelector, $oList = null): void throw new UnexpectedTokenException( "Selector did not match '" . Selector::SELECTOR_VALIDATION_RX . "'.", $mSelector, - "custom" + 'custom' ); } $this->aSelectors[$iKey] = new Selector($mSelector); @@ -116,7 +116,7 @@ public function setSelectors($mSelector, $oList = null): void throw new UnexpectedTokenException( "Selector did not match '" . KeyframeSelector::SELECTOR_VALIDATION_RX . "'.", $mSelector, - "custom" + 'custom' ); } $this->aSelectors[$iKey] = new KeyframeSelector($mSelector); @@ -225,14 +225,14 @@ public function expandBorderShorthand(): void $mNewValue = $mValue; } if ($mValue instanceof Size) { - $sNewRuleName = $sBorderRule . "-width"; + $sNewRuleName = $sBorderRule . '-width'; } elseif ($mValue instanceof Color) { - $sNewRuleName = $sBorderRule . "-color"; + $sNewRuleName = $sBorderRule . '-color'; } else { if (\in_array($mValue, $aBorderSizes, true)) { - $sNewRuleName = $sBorderRule . "-width"; + $sNewRuleName = $sBorderRule . '-width'; } else { - $sNewRuleName = $sBorderRule . "-style"; + $sNewRuleName = $sBorderRule . '-style'; } } $oNewRule = new Rule($sNewRuleName, $oRule->getLineNo(), $oRule->getColNo()); @@ -789,7 +789,7 @@ public function render(OutputFormat $oOutputFormat): string $sResult = $oOutputFormat->comments($this); if (\count($this->aSelectors) === 0) { // If all the selectors have been removed, this declaration block becomes invalid - throw new OutputException("Attempt to print declaration block with missing selector", $this->iLineNo); + throw new OutputException('Attempt to print declaration block with missing selector', $this->iLineNo); } $sResult .= $oOutputFormat->sBeforeDeclarationBlock; $sResult .= $oOutputFormat->implode( diff --git a/src/RuleSet/RuleSet.php b/src/RuleSet/RuleSet.php index 02388e47..e3ae93a0 100644 --- a/src/RuleSet/RuleSet.php +++ b/src/RuleSet/RuleSet.php @@ -63,7 +63,7 @@ public static function parseRuleSet(ParserState $oParserState, RuleSet $oRuleSet $oRule = Rule::parse($oParserState); } catch (UnexpectedTokenException $e) { try { - $sConsume = $oParserState->consumeUntil(["\n", ";", '}'], true); + $sConsume = $oParserState->consumeUntil(["\n", ';', '}'], true); // We need to “unfind” the matches to the end of the ruleSet as this will be matched later if ($oParserState->streql(\substr($sConsume, -1), '}')) { $oParserState->backtrack(1); diff --git a/src/Value/CSSString.php b/src/Value/CSSString.php index a39131d9..42e358c9 100644 --- a/src/Value/CSSString.php +++ b/src/Value/CSSString.php @@ -47,7 +47,7 @@ public static function parse(ParserState $oParserState): CSSString if ($sQuote !== null) { $oParserState->consume($sQuote); } - $sResult = ""; + $sResult = ''; $sContent = null; if ($sQuote === null) { // Unquoted strings end in whitespace or with braces, brackets, parentheses diff --git a/src/Value/Size.php b/src/Value/Size.php index ff175e01..bf40f4a0 100644 --- a/src/Value/Size.php +++ b/src/Value/Size.php @@ -203,9 +203,9 @@ public function render(OutputFormat $oOutputFormat): string { $l = \localeconv(); $sPoint = \preg_quote($l['decimal_point'], '/'); - $sSize = \preg_match("/[\\d\\.]+e[+-]?\\d+/i", (string) $this->fSize) - ? \preg_replace("/$sPoint?0+$/", "", \sprintf("%f", $this->fSize)) : $this->fSize; - return \preg_replace(["/$sPoint/", "/^(-?)0\\./"], ['.', '$1.'], $sSize) + $sSize = \preg_match('/[\\d\\.]+e[+-]?\\d+/i', (string) $this->fSize) + ? \preg_replace("/$sPoint?0+$/", '', \sprintf('%f', $this->fSize)) : $this->fSize; + return \preg_replace(["/$sPoint/", '/^(-?)0\\./'], ['.', '$1.'], $sSize) . ($this->sUnit ?? ''); } } diff --git a/src/Value/Value.php b/src/Value/Value.php index ae1477f8..5fb6e3d0 100644 --- a/src/Value/Value.php +++ b/src/Value/Value.php @@ -156,11 +156,11 @@ public static function parsePrimitiveValue(ParserState $oParserState) $oValue = Color::parse($oParserState); } elseif ($oParserState->comes("'") || $oParserState->comes('"')) { $oValue = CSSString::parse($oParserState); - } elseif ($oParserState->comes("progid:") && $oParserState->getSettings()->bLenientParsing) { + } elseif ($oParserState->comes('progid:') && $oParserState->getSettings()->bLenientParsing) { $oValue = self::parseMicrosoftFilter($oParserState); - } elseif ($oParserState->comes("[")) { + } elseif ($oParserState->comes('[')) { $oValue = LineName::parse($oParserState); - } elseif ($oParserState->comes("U+")) { + } elseif ($oParserState->comes('U+')) { $oValue = self::parseUnicodeRangeValue($oParserState); } else { $sNextChar = $oParserState->peek(1); @@ -196,14 +196,14 @@ private static function parseMicrosoftFilter(ParserState $oParserState): CSSFunc private static function parseUnicodeRangeValue(ParserState $oParserState): string { $iCodepointMaxLength = 6; // Code points outside BMP can use up to six digits - $sRange = ""; - $oParserState->consume("U+"); + $sRange = ''; + $oParserState->consume('U+'); do { if ($oParserState->comes('-')) { $iCodepointMaxLength = 13; // Max length is 2 six digit code points + the dash(-) between them } $sRange .= $oParserState->consume(1); - } while (\strlen($sRange) < $iCodepointMaxLength && \preg_match("/[A-Fa-f0-9\\?-]/", $oParserState->peek())); + } while (\strlen($sRange) < $iCodepointMaxLength && \preg_match('/[A-Fa-f0-9\\?-]/', $oParserState->peek())); return "U+{$sRange}"; } diff --git a/tests/OutputFormatTest.php b/tests/OutputFormatTest.php index 1c80c0c8..37fc14f8 100644 --- a/tests/OutputFormatTest.php +++ b/tests/OutputFormatTest.php @@ -90,7 +90,7 @@ public function spaceAfterListArgumentSeparator(): void '.main, .test {font: italic normal bold 16px/ 1.2 ' . '"Helvetica", Verdana, sans-serif;background: white;}' . "\n@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}", - $this->oDocument->render(OutputFormat::create()->setSpaceAfterListArgumentSeparator(" ")) + $this->oDocument->render(OutputFormat::create()->setSpaceAfterListArgumentSeparator(' ')) ); } diff --git a/tests/ParserTest.php b/tests/ParserTest.php index eb2174af..2c1c7287 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -122,7 +122,7 @@ public function colorParsing(): void 'r' => new Size(10.0, null, true, $oColor->getLineNo()), 'g' => new Size(100.0, null, true, $oColor->getLineNo()), 'b' => new Size(231.0, null, true, $oColor->getLineNo()), - 'a' => new Size("0000.3", null, true, $oColor->getLineNo()), + 'a' => new Size('0000.3', null, true, $oColor->getLineNo()), ], $oColor->getColor()); $aColorRule = $oRuleSet->getRules('outline-color'); $oColor = $aColorRule[0]->getValue(); @@ -225,7 +225,7 @@ public function unicodeParsing(): void public function unicodeRangeParsing(): void { $oDoc = self::parsedStructureForFile('unicode-range'); - $sExpected = "@font-face {unicode-range: U+0100-024F,U+0259,U+1E??-2EFF,U+202F;}"; + $sExpected = '@font-face {unicode-range: U+0100-024F,U+0259,U+1E??-2EFF,U+202F;}'; self::assertSame($sExpected, $oDoc->render()); } @@ -240,23 +240,23 @@ public function specificity(): void $aSelectors = $oDeclarationBlock->getSelectors(); foreach ($aSelectors as $oSelector) { switch ($oSelector->getSelector()) { - case "#test .help": + case '#test .help': self::assertSame(110, $oSelector->getSpecificity()); break; - case "#file": + case '#file': self::assertSame(100, $oSelector->getSpecificity()); break; - case ".help:hover": + case '.help:hover': self::assertSame(20, $oSelector->getSpecificity()); break; - case "ol li::before": + case 'ol li::before': self::assertSame(3, $oSelector->getSpecificity()); break; - case "li.green": + case 'li.green': self::assertSame(11, $oSelector->getSpecificity()); break; default: - self::fail("specificity: untested selector " . $oSelector->getSelector()); + self::fail('specificity: untested selector ' . $oSelector->getSelector()); } } self::assertEquals([new Selector('#test .help', true)], $oDoc->getSelectorsBySpecificity('> 100')); @@ -734,7 +734,7 @@ public function gridLineNameInFile(): void { $oDoc = self::parsedStructureForFile('grid-linename', Settings::create()->withMultibyteSupport(true)); $sExpected = "div {grid-template-columns: [linename] 100px;}\n" - . "span {grid-template-columns: [linename1 linename2] 100px;}"; + . 'span {grid-template-columns: [linename1 linename2] 100px;}'; self::assertSame($sExpected, $oDoc->render()); } @@ -754,7 +754,7 @@ public function emptyGridLineNameLenientInFile(): void public function invalidGridLineNameInFile(): void { $oDoc = self::parsedStructureForFile('invalid-grid-linename', Settings::create()->withMultibyteSupport(true)); - $sExpected = "div {}"; + $sExpected = 'div {}'; self::assertSame($sExpected, $oDoc->render()); } @@ -1122,7 +1122,7 @@ public function commentExtracting(): void $importComments = $aNodes[0]->getComments(); self::assertCount(2, $importComments); self::assertSame("*\n * Comments\n ", $importComments[0]->getComment()); - self::assertSame(" Hell ", $importComments[1]->getComment()); + self::assertSame(' Hell ', $importComments[1]->getComment()); // Declaration block. $fooBarBlock = $aNodes[1]; @@ -1137,7 +1137,7 @@ public function commentExtracting(): void $fooBarRule = $fooBarRules[0]; $fooBarRuleComments = $fooBarRule->getComments(); self::assertCount(1, $fooBarRuleComments); - self::assertSame(" Number 6 ", $fooBarRuleComments[0]->getComment()); + self::assertSame(' Number 6 ', $fooBarRuleComments[0]->getComment()); // Media property. $mediaComments = $aNodes[2]->getComments(); @@ -1147,13 +1147,13 @@ public function commentExtracting(): void $mediaRules = $aNodes[2]->getContents(); $fooBarComments = $mediaRules[0]->getComments(); self::assertCount(1, $fooBarComments); - self::assertSame("* Number 10 *", $fooBarComments[0]->getComment()); + self::assertSame('* Number 10 *', $fooBarComments[0]->getComment()); // Media -> declaration -> rule. $fooBarRules = $mediaRules[0]->getRules(); $fooBarChildComments = $fooBarRules[0]->getComments(); self::assertCount(1, $fooBarChildComments); - self::assertSame("* Number 10b *", $fooBarChildComments[0]->getComment()); + self::assertSame('* Number 10b *', $fooBarChildComments[0]->getComment()); } /** @@ -1167,7 +1167,7 @@ public function flatCommentExtracting(): void $divRules = $contents[0]->getRules(); $comments = $divRules[0]->getComments(); self::assertCount(1, $comments); - self::assertSame("Find Me!", $comments[0]->getComment()); + self::assertSame('Find Me!', $comments[0]->getComment()); } /** @@ -1180,7 +1180,7 @@ public function topLevelCommentExtracting(): void $contents = $doc->getContents(); $comments = $contents[0]->getComments(); self::assertCount(1, $comments); - self::assertSame("Find Me!", $comments[0]->getComment()); + self::assertSame('Find Me!', $comments[0]->getComment()); } /** @@ -1235,7 +1235,7 @@ public function scientificNotationSizeValuesInFile(): void public function lonelyImport(): void { $oDoc = self::parsedStructureForFile('lonely-import'); - $sExpected = "@import url(\"example.css\") only screen and (max-width: 600px);"; + $sExpected = '@import url("example.css") only screen and (max-width: 600px);'; self::assertSame($sExpected, $oDoc->render()); } diff --git a/tests/RuleSet/LenientParsingTest.php b/tests/RuleSet/LenientParsingTest.php index 5d4bf678..3d670f74 100644 --- a/tests/RuleSet/LenientParsingTest.php +++ b/tests/RuleSet/LenientParsingTest.php @@ -81,7 +81,7 @@ public function endTokenPositive(): void $sFile = __DIR__ . '/../fixtures/-end-token.css'; $oParser = new Parser(\file_get_contents($sFile), Settings::create()->withLenientParsing(true)); $oResult = $oParser->parse(); - self::assertSame("", $oResult->render()); + self::assertSame('', $oResult->render()); } /** @@ -103,7 +103,7 @@ public function endToken2Positive(): void */ public function localeTrap(): void { - \setlocale(LC_ALL, "pt_PT", "no"); + \setlocale(LC_ALL, 'pt_PT', 'no'); $sFile = __DIR__ . '/../fixtures/-fault-tolerance.css'; $oParser = new Parser(\file_get_contents($sFile), Settings::create()->withLenientParsing(true)); $oResult = $oParser->parse();