From 83ec4545be4137252cbc0e0f32152fcf8a32b7f7 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Tue, 25 Feb 2025 10:00:56 +0100 Subject: [PATCH 1/2] [CLEANUP] Refactor `::getAllDeclarationBlocks()` Part of #994. --- src/CSSList/CSSBlockList.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/CSSList/CSSBlockList.php b/src/CSSList/CSSBlockList.php index 0f2817c3..c3152b1d 100644 --- a/src/CSSList/CSSBlockList.php +++ b/src/CSSList/CSSBlockList.php @@ -23,13 +23,23 @@ abstract class CSSBlockList extends CSSList /** * Gets all `DeclarationBlock` objects recursively, no matter how deeply nested the selectors are. * - * @return array + * @return list */ public function getAllDeclarationBlocks(): array { - /** @var array $result */ + /** @var list $result */ $result = []; - $this->allDeclarationBlocks($result); + + foreach ($this->contents as $directSibling) { + if ($directSibling instanceof DeclarationBlock) { + $result[] = $directSibling; + } elseif ($directSibling instanceof CSSBlockList) { + foreach ($directSibling->getAllDeclarationBlocks() as $grandchild) { + $result[] = $grandchild; + } + } + } + return $result; } From 4a7e38c75ac526f1a0c9032a11b6726f9da388dd Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Thu, 27 Feb 2025 09:34:37 +0100 Subject: [PATCH 2/2] Changes suggested in code review --- src/CSSList/CSSBlockList.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/CSSList/CSSBlockList.php b/src/CSSList/CSSBlockList.php index c3152b1d..cae6d9f0 100644 --- a/src/CSSList/CSSBlockList.php +++ b/src/CSSList/CSSBlockList.php @@ -27,16 +27,13 @@ abstract class CSSBlockList extends CSSList */ public function getAllDeclarationBlocks(): array { - /** @var list $result */ $result = []; - foreach ($this->contents as $directSibling) { - if ($directSibling instanceof DeclarationBlock) { - $result[] = $directSibling; - } elseif ($directSibling instanceof CSSBlockList) { - foreach ($directSibling->getAllDeclarationBlocks() as $grandchild) { - $result[] = $grandchild; - } + foreach ($this->contents as $item) { + if ($item instanceof DeclarationBlock) { + $result[] = $item; + } elseif ($item instanceof CSSBlockList) { + $result = \array_merge($result, $item->getAllDeclarationBlocks()); } }