From c02b02b2d0f6e1e95b5acdcd54e03b33ef0379e4 Mon Sep 17 00:00:00 2001 From: Jake Hotson Date: Sun, 13 Apr 2025 22:45:04 +0100 Subject: [PATCH] [TASK] Refactor `getAllValues()` Move functionality from `allValues()` directly into to `getAllValues()`, so as to avoid passing array by reference, removing `allValues()`. Avoid making the recursive call for nested items that are not `CSSElement`s. Part of #994. Closes #1230. --- src/CSSList/CSSBlockList.php | 54 ++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/src/CSSList/CSSBlockList.php b/src/CSSList/CSSBlockList.php index 5e1312b7d..53004b901 100644 --- a/src/CSSList/CSSBlockList.php +++ b/src/CSSList/CSSBlockList.php @@ -73,7 +73,7 @@ public function getAllRuleSets(): array * will be returned). * @param bool $searchInFunctionArguments whether to also return `Value` objects used as `CSSFunction` arguments. * - * @return array + * @return list * * @see RuleSet->getRules() */ @@ -82,40 +82,52 @@ public function getAllValues( ?string $ruleSearchPattern = null, bool $searchInFunctionArguments = false ): array { - $result = []; - $this->allValues($element ?? $this, $result, $ruleSearchPattern, $searchInFunctionArguments); - return $result; - } + $element = $element ?? $this; - /** - * @param CSSElement|string $element - * @param list $result - */ - protected function allValues( - $element, - array &$result, - ?string $searchString = null, - bool $searchInFunctionArguments = false - ): void { + $result = []; if ($element instanceof CSSBlockList) { - foreach ($element->getContents() as $content) { - $this->allValues($content, $result, $searchString, $searchInFunctionArguments); + foreach ($element->getContents() as $contentItem) { + // Statement at-rules are skipped since they do not contain values. + if ($contentItem instanceof CSSElement) { + $result = \array_merge( + $result, + $this->getAllValues($contentItem, $ruleSearchPattern, $searchInFunctionArguments) + ); + } } } elseif ($element instanceof RuleSet) { - foreach ($element->getRules($searchString) as $rule) { - $this->allValues($rule, $result, $searchString, $searchInFunctionArguments); + foreach ($element->getRules($ruleSearchPattern) as $rule) { + $result = \array_merge( + $result, + $this->getAllValues($rule, $ruleSearchPattern, $searchInFunctionArguments) + ); } } elseif ($element instanceof Rule) { - $this->allValues($element->getValue(), $result, $searchString, $searchInFunctionArguments); + $value = $element->getValue(); + // `string` values are discarded. + if ($value instanceof CSSElement) { + $result = \array_merge( + $result, + $this->getAllValues($value, $ruleSearchPattern, $searchInFunctionArguments) + ); + } } elseif ($element instanceof ValueList) { if ($searchInFunctionArguments || !($element instanceof CSSFunction)) { foreach ($element->getListComponents() as $component) { - $this->allValues($component, $result, $searchString, $searchInFunctionArguments); + // `string` components are discarded. + if ($component instanceof CSSElement) { + $result = \array_merge( + $result, + $this->getAllValues($component, $ruleSearchPattern, $searchInFunctionArguments) + ); + } } } } elseif ($element instanceof Value) { $result[] = $element; } + + return $result; } /**