Skip to content

[TASK] Refactor getAllValues() #1244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 33 additions & 21 deletions src/CSSList/CSSBlockList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, Value>
* @return list<Value>
*
* @see RuleSet->getRules()
*/
Expand All @@ -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<Value> $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;
}

/**
Expand Down