From e108d47496dada90834622bccb2ba68cd0c384e2 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Sun, 10 Nov 2024 19:52:49 +0100 Subject: [PATCH 1/3] [TASK] Clean up the code with Rector Just the Rector changes, and some redundancy removal related to those, but nothing more. --- src/CSSList/CSSList.php | 11 ++++++----- src/OutputFormat.php | 10 +++------- src/OutputFormatter.php | 3 +-- src/Parsing/Anchor.php | 3 +-- src/Parsing/ParserState.php | 4 +--- src/RuleSet/RuleSet.php | 2 +- src/Settings.php | 16 ++++++++-------- src/Value/CSSFunction.php | 5 +---- src/Value/CSSString.php | 2 +- src/Value/Color.php | 7 ++----- 10 files changed, 25 insertions(+), 38 deletions(-) diff --git a/src/CSSList/CSSList.php b/src/CSSList/CSSList.php index 1267af29..eab8122e 100644 --- a/src/CSSList/CSSList.php +++ b/src/CSSList/CSSList.php @@ -169,7 +169,11 @@ private static function parseAtRule(ParserState $oParserState) $sMediaQuery = \trim($oParserState->consumeUntil([';', ParserState::EOF])); } $oParserState->consumeUntil([';', ParserState::EOF], true, true); - return new Import($oLocation, $sMediaQuery ?: null, $iIdentifierLineNum); + return new Import( + $oLocation, + $sMediaQuery !== null && $sMediaQuery !== '' && $sMediaQuery !== '0' ? $sMediaQuery : null, + $iIdentifierLineNum + ); } elseif ($sIdentifier === 'charset') { $oCharsetString = CSSString::parse($oParserState); $oParserState->consumeWhiteSpace(); @@ -238,11 +242,8 @@ private static function parseAtRule(ParserState $oParserState) /** * Tests an identifier for a given value. Since identifiers are all keywords, they can be vendor-prefixed. * We need to check for these versions too. - * - * @param string $sIdentifier - * @param string $sMatch */ - private static function identifierIs($sIdentifier, $sMatch): bool + private static function identifierIs($sIdentifier, string $sMatch): bool { return (\strcasecmp($sIdentifier, $sMatch) === 0) ?: \preg_match("/^(-\\w+-)?$sMatch$/i", $sIdentifier) === 1; diff --git a/src/OutputFormat.php b/src/OutputFormat.php index 2b5607db..8809f11b 100644 --- a/src/OutputFormat.php +++ b/src/OutputFormat.php @@ -230,7 +230,7 @@ public function set($aNames, $mValue) * * @throws \Exception */ - public function __call($sMethodName, array $aArguments) + public function __call(string $sMethodName, array $aArguments) { if (\strpos($sMethodName, 'set') === 0) { return $this->set(\substr($sMethodName, 3), $aArguments[0]); @@ -310,10 +310,8 @@ public static function create(): OutputFormat /** * Creates an instance of this class with a preset for compact formatting. - * - * @return self */ - public static function createCompact() + public static function createCompact(): self { $format = self::create(); $format->set('Space*Rules', '') @@ -327,10 +325,8 @@ public static function createCompact() /** * Creates an instance of this class with a preset for pretty formatting. - * - * @return self */ - public static function createPretty() + public static function createPretty(): self { $format = self::create(); $format->set('Space*Rules', "\n") diff --git a/src/OutputFormatter.php b/src/OutputFormatter.php index 4349f664..6e68037f 100644 --- a/src/OutputFormatter.php +++ b/src/OutputFormatter.php @@ -132,11 +132,10 @@ public function safely($cCode) /** * Clone of the `implode` function, but calls `render` with the current output format instead of `__toString()`. * - * @param string $sSeparator * @param array $aValues * @param bool $bIncreaseLevel */ - public function implode($sSeparator, array $aValues, $bIncreaseLevel = false): string + public function implode(string $sSeparator, array $aValues, $bIncreaseLevel = false): string { $sResult = ''; $oFormat = $this->oFormat; diff --git a/src/Parsing/Anchor.php b/src/Parsing/Anchor.php index 1a1d42ae..545cb035 100644 --- a/src/Parsing/Anchor.php +++ b/src/Parsing/Anchor.php @@ -15,13 +15,12 @@ class Anchor private $iPosition; /** - * @var \Sabberworm\CSS\Parsing\ParserState + * @var ParserState */ private $oParserState; /** * @param int $iPosition - * @param \Sabberworm\CSS\Parsing\ParserState $oParserState */ public function __construct($iPosition, ParserState $oParserState) { diff --git a/src/Parsing/ParserState.php b/src/Parsing/ParserState.php index fa146d7e..22152fbd 100644 --- a/src/Parsing/ParserState.php +++ b/src/Parsing/ParserState.php @@ -362,12 +362,10 @@ public function isEnd(): bool * @param string $consumeEnd * @param array $comments * - * @return string - * * @throws UnexpectedEOFException * @throws UnexpectedTokenException */ - public function consumeUntil($aEnd, $bIncludeEnd = false, $consumeEnd = false, array &$comments = []) + public function consumeUntil($aEnd, $bIncludeEnd = false, $consumeEnd = false, array &$comments = []): string { $aEnd = \is_array($aEnd) ? $aEnd : [$aEnd]; $out = ''; diff --git a/src/RuleSet/RuleSet.php b/src/RuleSet/RuleSet.php index c2fd6e30..e53abd52 100644 --- a/src/RuleSet/RuleSet.php +++ b/src/RuleSet/RuleSet.php @@ -82,7 +82,7 @@ public static function parseRuleSet(ParserState $oParserState, RuleSet $oRuleSet } else { $oRule = Rule::parse($oParserState); } - if ($oRule) { + if ($oRule instanceof Rule) { $oRuleSet->addRule($oRule); } } diff --git a/src/Settings.php b/src/Settings.php index b5934a05..17e5fa00 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -56,9 +56,9 @@ public static function create(): Settings * * @param bool $bMultibyteSupport * - * @return self fluent interface + * @return $this fluent interface */ - public function withMultibyteSupport($bMultibyteSupport = true) + public function withMultibyteSupport($bMultibyteSupport = true): self { $this->bMultibyteSupport = $bMultibyteSupport; return $this; @@ -69,9 +69,9 @@ public function withMultibyteSupport($bMultibyteSupport = true) * * @param string $sDefaultCharset * - * @return self fluent interface + * @return $this fluent interface */ - public function withDefaultCharset($sDefaultCharset) + public function withDefaultCharset($sDefaultCharset): self { $this->sDefaultCharset = $sDefaultCharset; return $this; @@ -82,9 +82,9 @@ public function withDefaultCharset($sDefaultCharset) * * @param bool $bLenientParsing * - * @return self fluent interface + * @return $this fluent interface */ - public function withLenientParsing($bLenientParsing = true) + public function withLenientParsing($bLenientParsing = true): self { $this->bLenientParsing = $bLenientParsing; return $this; @@ -93,9 +93,9 @@ public function withLenientParsing($bLenientParsing = true) /** * Configures the parser to choke on invalid rules. * - * @return self fluent interface + * @return $this fluent interface */ - public function beStrict() + public function beStrict(): self { return $this->withLenientParsing(false); } diff --git a/src/Value/CSSFunction.php b/src/Value/CSSFunction.php index 5902cf77..82b21e8a 100644 --- a/src/Value/CSSFunction.php +++ b/src/Value/CSSFunction.php @@ -103,10 +103,7 @@ public function __toString(): string return $this->render(new OutputFormat()); } - /** - * @return string - */ - public function render(OutputFormat $oOutputFormat) + public function render(OutputFormat $oOutputFormat): string { $aArguments = parent::render($oOutputFormat); return "{$this->sName}({$aArguments})"; diff --git a/src/Value/CSSString.php b/src/Value/CSSString.php index 3385f48c..c21932df 100644 --- a/src/Value/CSSString.php +++ b/src/Value/CSSString.php @@ -53,7 +53,7 @@ public static function parse(ParserState $oParserState): CSSString $sContent = null; if ($sQuote === null) { // Unquoted strings end in whitespace or with braces, brackets, parentheses - while (!\preg_match('/[\\s{}()<>\\[\\]]/isu', $oParserState->peek())) { + while (\in_array(\preg_match('/[\\s{}()<>\\[\\]]/isu', $oParserState->peek()), [0, false], true)) { $sResult .= $oParserState->parseCharacter(false); } } else { diff --git a/src/Value/Color.php b/src/Value/Color.php index 706d1d1f..2d924cbd 100644 --- a/src/Value/Color.php +++ b/src/Value/Color.php @@ -111,7 +111,7 @@ public static function parse(ParserState $oParserState, bool $bIgnoreCase = fals * * @return float */ - private static function mapRange($fVal, $fFromMin, $fFromMax, $fToMin, $fToMax) + private static function mapRange(float $fVal, float $fFromMin, float $fFromMax, float $fToMin, float $fToMax) { $fFromRange = $fFromMax - $fFromMin; $fToRange = $fToMax - $fToMin; @@ -151,10 +151,7 @@ public function __toString(): string return $this->render(new OutputFormat()); } - /** - * @return string - */ - public function render(OutputFormat $oOutputFormat) + public function render(OutputFormat $oOutputFormat): string { // Shorthand RGB color values if ($oOutputFormat->getRGBHashNotation() && \implode('', \array_keys($this->aComponents)) === 'rgb') { From 2d2ac4e37fa5bc4b2d9f54c72243c958dbba5984 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Mon, 11 Nov 2024 09:24:24 +0100 Subject: [PATCH 2/3] Changes suggested in code review --- src/CSSList/CSSList.php | 9 ++++----- src/Value/CSSString.php | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/CSSList/CSSList.php b/src/CSSList/CSSList.php index eab8122e..1e4a51d9 100644 --- a/src/CSSList/CSSList.php +++ b/src/CSSList/CSSList.php @@ -167,13 +167,12 @@ private static function parseAtRule(ParserState $oParserState) $sMediaQuery = null; if (!$oParserState->comes(';')) { $sMediaQuery = \trim($oParserState->consumeUntil([';', ParserState::EOF])); + if ($sMediaQuery === '') { + $sMediaQuery = null; + } } $oParserState->consumeUntil([';', ParserState::EOF], true, true); - return new Import( - $oLocation, - $sMediaQuery !== null && $sMediaQuery !== '' && $sMediaQuery !== '0' ? $sMediaQuery : null, - $iIdentifierLineNum - ); + return new Import($oLocation, $sMediaQuery, $iIdentifierLineNum); } elseif ($sIdentifier === 'charset') { $oCharsetString = CSSString::parse($oParserState); $oParserState->consumeWhiteSpace(); diff --git a/src/Value/CSSString.php b/src/Value/CSSString.php index c21932df..f212bdd8 100644 --- a/src/Value/CSSString.php +++ b/src/Value/CSSString.php @@ -53,7 +53,7 @@ public static function parse(ParserState $oParserState): CSSString $sContent = null; if ($sQuote === null) { // Unquoted strings end in whitespace or with braces, brackets, parentheses - while (\in_array(\preg_match('/[\\s{}()<>\\[\\]]/isu', $oParserState->peek()), [0, false], true)) { + while (\preg_match('/[\\s{}()<>\\[\\]]/isu', $oParserState->peek()) !== 1) { $sResult .= $oParserState->parseCharacter(false); } } else { From bd8b90762c125a84d36c268b985941df2372b3ab Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Mon, 11 Nov 2024 09:26:32 +0100 Subject: [PATCH 3/3] More cleanup of the Rector changes --- src/CSSList/CSSList.php | 2 ++ src/OutputFormat.php | 3 +-- src/Value/Color.php | 6 ------ 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/CSSList/CSSList.php b/src/CSSList/CSSList.php index 1e4a51d9..6f31def8 100644 --- a/src/CSSList/CSSList.php +++ b/src/CSSList/CSSList.php @@ -241,6 +241,8 @@ private static function parseAtRule(ParserState $oParserState) /** * Tests an identifier for a given value. Since identifiers are all keywords, they can be vendor-prefixed. * We need to check for these versions too. + * + * @param string $sIdentifier */ private static function identifierIs($sIdentifier, string $sMatch): bool { diff --git a/src/OutputFormat.php b/src/OutputFormat.php index 8809f11b..3f126346 100644 --- a/src/OutputFormat.php +++ b/src/OutputFormat.php @@ -223,7 +223,6 @@ public function set($aNames, $mValue) } /** - * @param string $sMethodName * @param array $aArguments * * @return mixed @@ -303,7 +302,7 @@ public function level() /** * Creates an instance of this class without any particular formatting settings. */ - public static function create(): OutputFormat + public static function create(): self { return new OutputFormat(); } diff --git a/src/Value/Color.php b/src/Value/Color.php index 2d924cbd..55a6ce3d 100644 --- a/src/Value/Color.php +++ b/src/Value/Color.php @@ -103,12 +103,6 @@ public static function parse(ParserState $oParserState, bool $bIgnoreCase = fals } /** - * @param float $fVal - * @param float $fFromMin - * @param float $fFromMax - * @param float $fToMin - * @param float $fToMax - * * @return float */ private static function mapRange(float $fVal, float $fFromMin, float $fFromMax, float $fToMin, float $fToMax)