Skip to content

[CLEANUP] Use single-quoted string literals whenever possible #642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
'strict_param' => true,

// string notation
'single_quote' => true,
'string_implicit_backslashes' => ['single_quoted' => 'escape'],
]
);
10 changes: 5 additions & 5 deletions src/CSSList/CSSList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/OutputFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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('')
Expand Down
2 changes: 1 addition & 1 deletion src/Parsing/ParserState.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Property/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) . ';';
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
Expand Down
16 changes: 8 additions & 8 deletions src/RuleSet/DeclarationBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/RuleSet/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Value/CSSString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/Value/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? '');
}
}
12 changes: 6 additions & 6 deletions src/Value/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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}";
}

Expand Down
2 changes: 1 addition & 1 deletion tests/OutputFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(' '))
);
}

Expand Down
34 changes: 17 additions & 17 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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());
}

Expand All @@ -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'));
Expand Down Expand Up @@ -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());
}

Expand All @@ -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());
}

Expand Down Expand Up @@ -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];
Expand All @@ -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();
Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -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());
}

Expand Down
4 changes: 2 additions & 2 deletions tests/RuleSet/LenientParsingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand All @@ -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();
Expand Down