From dc0bd3bf742e0e3ccc749f6c9c4daeece677898c Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Wed, 26 Feb 2025 08:55:46 +0100 Subject: [PATCH 1/2] [CLEANUP] Avoid Hungarian notation in `DeclarationBlock` Also fix some variable name collisions. Part of #756 --- config/phpstan-baseline.neon | 6 ---- src/RuleSet/DeclarationBlock.php | 48 ++++++++++++++++---------------- 2 files changed, 24 insertions(+), 30 deletions(-) diff --git a/config/phpstan-baseline.neon b/config/phpstan-baseline.neon index 0de2d16a..535469d6 100644 --- a/config/phpstan-baseline.neon +++ b/config/phpstan-baseline.neon @@ -318,12 +318,6 @@ parameters: count: 1 path: ../src/RuleSet/DeclarationBlock.php - - - message: '#^Foreach overwrites \$mSelector with its value variable\.$#' - identifier: foreach.valueOverwrite - count: 1 - path: ../src/RuleSet/DeclarationBlock.php - - message: '#^Loose comparison via "\!\=" is not allowed\.$#' identifier: notEqual.notAllowed diff --git a/src/RuleSet/DeclarationBlock.php b/src/RuleSet/DeclarationBlock.php index 4dc0ce2d..024de10d 100644 --- a/src/RuleSet/DeclarationBlock.php +++ b/src/RuleSet/DeclarationBlock.php @@ -27,7 +27,7 @@ class DeclarationBlock extends RuleSet /** * @var array */ - private $aSelectors = []; + private $selectors = []; /** * @param CSSList|null $list @@ -76,38 +76,38 @@ public static function parse(ParserState $parserState, $list = null) } /** - * @param array|string $mSelector + * @param array|string $selectors * @param CSSList|null $list * * @throws UnexpectedTokenException */ - public function setSelectors($mSelector, $list = null): void + public function setSelectors($selectors, $list = null): void { - if (\is_array($mSelector)) { - $this->aSelectors = $mSelector; + if (\is_array($selectors)) { + $this->selectors = $selectors; } else { - $this->aSelectors = \explode(',', $mSelector); + $this->selectors = \explode(',', $selectors); } - foreach ($this->aSelectors as $key => $mSelector) { - if (!($mSelector instanceof Selector)) { + foreach ($this->selectors as $key => $selector) { + if (!($selector instanceof Selector)) { if ($list === null || !($list instanceof KeyFrame)) { - if (!Selector::isValid($mSelector)) { + if (!Selector::isValid($selector)) { throw new UnexpectedTokenException( "Selector did not match '" . Selector::SELECTOR_VALIDATION_RX . "'.", - $mSelector, + $selectors, 'custom' ); } - $this->aSelectors[$key] = new Selector($mSelector); + $this->selectors[$key] = new Selector($selector); } else { - if (!KeyframeSelector::isValid($mSelector)) { + if (!KeyframeSelector::isValid($selector)) { throw new UnexpectedTokenException( "Selector did not match '" . KeyframeSelector::SELECTOR_VALIDATION_RX . "'.", - $mSelector, + $selector, 'custom' ); } - $this->aSelectors[$key] = new KeyframeSelector($mSelector); + $this->selectors[$key] = new KeyframeSelector($selector); } } } @@ -116,16 +116,16 @@ public function setSelectors($mSelector, $list = null): void /** * Remove one of the selectors of the block. * - * @param Selector|string $mSelector + * @param Selector|string $selectorToRemove */ - public function removeSelector($mSelector): bool + public function removeSelector($selectorToRemove): bool { - if ($mSelector instanceof Selector) { - $mSelector = $mSelector->getSelector(); + if ($selectorToRemove instanceof Selector) { + $selectorToRemove = $selectorToRemove->getSelector(); } - foreach ($this->aSelectors as $key => $selector) { - if ($selector->getSelector() === $mSelector) { - unset($this->aSelectors[$key]); + foreach ($this->selectors as $key => $subSelector) { + if ($subSelector->getSelector() === $selectorToRemove) { + unset($this->selectors[$key]); return true; } } @@ -137,7 +137,7 @@ public function removeSelector($mSelector): bool */ public function getSelectors() { - return $this->aSelectors; + return $this->selectors; } /** @@ -154,14 +154,14 @@ public function __toString(): string public function render(OutputFormat $outputFormat): string { $result = $outputFormat->comments($this); - if (\count($this->aSelectors) === 0) { + if (\count($this->selectors) === 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->lineNumber); } $result .= $outputFormat->sBeforeDeclarationBlock; $result .= $outputFormat->implode( $outputFormat->spaceBeforeSelectorSeparator() . ',' . $outputFormat->spaceAfterSelectorSeparator(), - $this->aSelectors + $this->selectors ); $result .= $outputFormat->sAfterDeclarationBlockSelectors; $result .= $outputFormat->spaceBeforeOpeningBrace() . '{'; From dfba00715f1a1c03cc91c3f0aaf31c0df53d2a76 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Wed, 26 Feb 2025 11:12:08 +0100 Subject: [PATCH 2/2] Changes suggested in code review --- src/RuleSet/DeclarationBlock.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/RuleSet/DeclarationBlock.php b/src/RuleSet/DeclarationBlock.php index 024de10d..5bcc32e1 100644 --- a/src/RuleSet/DeclarationBlock.php +++ b/src/RuleSet/DeclarationBlock.php @@ -123,8 +123,8 @@ public function removeSelector($selectorToRemove): bool if ($selectorToRemove instanceof Selector) { $selectorToRemove = $selectorToRemove->getSelector(); } - foreach ($this->selectors as $key => $subSelector) { - if ($subSelector->getSelector() === $selectorToRemove) { + foreach ($this->selectors as $key => $selector) { + if ($selector->getSelector() === $selectorToRemove) { unset($this->selectors[$key]); return true; }