From 1ec2d7f1cde97000f336234ac20b56c61aad62ee Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Wed, 12 Mar 2025 17:32:49 +0100 Subject: [PATCH 1/2] [CLEANUP] Avoid magic method forwarding in `RuleSet` Part of #1147 --- config/phpstan-baseline.neon | 30 ------------------------------ src/RuleSet/RuleSet.php | 16 +++++++++------- 2 files changed, 9 insertions(+), 37 deletions(-) diff --git a/config/phpstan-baseline.neon b/config/phpstan-baseline.neon index 01b5d708f..b527ad4b5 100644 --- a/config/phpstan-baseline.neon +++ b/config/phpstan-baseline.neon @@ -156,36 +156,6 @@ parameters: count: 1 path: ../src/RuleSet/DeclarationBlock.php - - - message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:removeLastSemicolon\(\)\.$#' - identifier: method.notFound - count: 1 - path: ../src/RuleSet/RuleSet.php - - - - message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:safely\(\)\.$#' - identifier: method.notFound - count: 1 - path: ../src/RuleSet/RuleSet.php - - - - message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:spaceAfterRules\(\)\.$#' - identifier: method.notFound - count: 1 - path: ../src/RuleSet/RuleSet.php - - - - message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:spaceBeforeRules\(\)\.$#' - identifier: method.notFound - count: 1 - path: ../src/RuleSet/RuleSet.php - - - - message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:spaceBetweenRules\(\)\.$#' - identifier: method.notFound - count: 1 - path: ../src/RuleSet/RuleSet.php - - message: '#^Only booleans are allowed in a negated boolean, string\|null given\.$#' identifier: booleanNot.exprNotBoolean diff --git a/src/RuleSet/RuleSet.php b/src/RuleSet/RuleSet.php index 5da5e4014..6d8c8e475 100644 --- a/src/RuleSet/RuleSet.php +++ b/src/RuleSet/RuleSet.php @@ -278,27 +278,29 @@ protected function renderRules(OutputFormat $outputFormat) $isFirst = true; $nextLevelFormat = $outputFormat->nextLevel(); foreach ($this->getRules() as $rule) { - $renderedRule = $nextLevelFormat->safely(static function () use ($rule, $nextLevelFormat): string { - return $rule->render($nextLevelFormat); - }); + $renderedRule = $nextLevelFormat->getFormatter() + ->safely(static function () use ($rule, $nextLevelFormat): string { + return $rule->render($nextLevelFormat); + }); if ($renderedRule === null) { continue; } if ($isFirst) { $isFirst = false; - $result .= $nextLevelFormat->spaceBeforeRules(); + $result .= $nextLevelFormat->getFormatter()->spaceBeforeRules(); } else { - $result .= $nextLevelFormat->spaceBetweenRules(); + $result .= $nextLevelFormat->getFormatter()->spaceBetweenRules(); } $result .= $renderedRule; } + $formatter = $outputFormat->getFormatter(); if (!$isFirst) { // Had some output - $result .= $outputFormat->spaceAfterRules(); + $result .= $formatter->spaceAfterRules(); } - return $outputFormat->removeLastSemicolon($result); + return $formatter->removeLastSemicolon($result); } /** From af71971ab691335d59d2f54bbf1cc40e24f5f455 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Wed, 12 Mar 2025 18:26:05 +0100 Subject: [PATCH 2/2] Introduce local variable --- src/RuleSet/RuleSet.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/RuleSet/RuleSet.php b/src/RuleSet/RuleSet.php index 6d8c8e475..a972dfd5a 100644 --- a/src/RuleSet/RuleSet.php +++ b/src/RuleSet/RuleSet.php @@ -278,18 +278,18 @@ protected function renderRules(OutputFormat $outputFormat) $isFirst = true; $nextLevelFormat = $outputFormat->nextLevel(); foreach ($this->getRules() as $rule) { - $renderedRule = $nextLevelFormat->getFormatter() - ->safely(static function () use ($rule, $nextLevelFormat): string { - return $rule->render($nextLevelFormat); - }); + $nextLevelFormatter = $nextLevelFormat->getFormatter(); + $renderedRule = $nextLevelFormatter->safely(static function () use ($rule, $nextLevelFormat): string { + return $rule->render($nextLevelFormat); + }); if ($renderedRule === null) { continue; } if ($isFirst) { $isFirst = false; - $result .= $nextLevelFormat->getFormatter()->spaceBeforeRules(); + $result .= $nextLevelFormatter->spaceBeforeRules(); } else { - $result .= $nextLevelFormat->getFormatter()->spaceBetweenRules(); + $result .= $nextLevelFormatter->spaceBetweenRules(); } $result .= $renderedRule; }