Skip to content

Commit 4bcd15c

Browse files
committed
[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.
1 parent 4429cc2 commit 4bcd15c

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

src/CSSList/CSSBlockList.php

+33-21
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function getAllRuleSets(): array
7373
* will be returned).
7474
* @param bool $searchInFunctionArguments whether to also return `Value` objects used as `CSSFunction` arguments.
7575
*
76-
* @return array<int, Value>
76+
* @return list<Value>
7777
*
7878
* @see RuleSet->getRules()
7979
*/
@@ -82,40 +82,52 @@ public function getAllValues(
8282
?string $ruleSearchPattern = null,
8383
bool $searchInFunctionArguments = false
8484
): array {
85-
$result = [];
86-
$this->allValues($element ?? $this, $result, $ruleSearchPattern, $searchInFunctionArguments);
87-
return $result;
88-
}
85+
$element = $element ?? $this;
8986

90-
/**
91-
* @param CSSElement|string $element
92-
* @param list<Value> $result
93-
*/
94-
protected function allValues(
95-
$element,
96-
array &$result,
97-
?string $searchString = null,
98-
bool $searchInFunctionArguments = false
99-
): void {
87+
$result = [];
10088
if ($element instanceof CSSBlockList) {
101-
foreach ($element->getContents() as $content) {
102-
$this->allValues($content, $result, $searchString, $searchInFunctionArguments);
89+
foreach ($element->getContents() as $contentItem) {
90+
// Statement at-rules are skipped since they do not contain values.
91+
if ($contentItem instanceof CSSElement) {
92+
$result = \array_merge(
93+
$result,
94+
$this->getAllValues($contentItem, $ruleSearchPattern, $searchInFunctionArguments)
95+
);
96+
}
10397
}
10498
} elseif ($element instanceof RuleSet) {
105-
foreach ($element->getRules($searchString) as $rule) {
106-
$this->allValues($rule, $result, $searchString, $searchInFunctionArguments);
99+
foreach ($element->getRules($ruleSearchPattern) as $rule) {
100+
$result = \array_merge(
101+
$result,
102+
$this->getAllValues($rule, $ruleSearchPattern, $searchInFunctionArguments)
103+
);
107104
}
108105
} elseif ($element instanceof Rule) {
109-
$this->allValues($element->getValue(), $result, $searchString, $searchInFunctionArguments);
106+
$value = $element->getValue();
107+
// `string` values are discarded.
108+
if ($value instanceof CSSElement) {
109+
$result = \array_merge(
110+
$result,
111+
$this->getAllValues($value, $ruleSearchPattern, $searchInFunctionArguments)
112+
);
113+
}
110114
} elseif ($element instanceof ValueList) {
111115
if ($searchInFunctionArguments || !($element instanceof CSSFunction)) {
112116
foreach ($element->getListComponents() as $component) {
113-
$this->allValues($component, $result, $searchString, $searchInFunctionArguments);
117+
// `string` components are discarded.
118+
if ($component instanceof CSSElement) {
119+
$result = \array_merge(
120+
$result,
121+
$this->getAllValues($component, $ruleSearchPattern, $searchInFunctionArguments)
122+
);
123+
}
114124
}
115125
}
116126
} elseif ($element instanceof Value) {
117127
$result[] = $element;
118128
}
129+
130+
return $result;
119131
}
120132

121133
/**

0 commit comments

Comments
 (0)