• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MyIntervals / PHP-CSS-Parser / 13563048992

27 Feb 2025 09:24AM UTC coverage: 54.512% (-0.3%) from 54.784%
13563048992

push

github

web-flow
[CLEANUP] Refactor `::getAllDeclarationBlocks()` (#990)

Part of #994.

5 of 5 new or added lines in 1 file covered. (100.0%)

7 existing lines in 1 file now uncovered.

1039 of 1906 relevant lines covered (54.51%)

12.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

21.13
/src/CSSList/CSSBlockList.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Sabberworm\CSS\CSSList;
6

7
use Sabberworm\CSS\Property\Selector;
8
use Sabberworm\CSS\Rule\Rule;
9
use Sabberworm\CSS\RuleSet\DeclarationBlock;
10
use Sabberworm\CSS\RuleSet\RuleSet;
11
use Sabberworm\CSS\Value\CSSFunction;
12
use Sabberworm\CSS\Value\Value;
13
use Sabberworm\CSS\Value\ValueList;
14

15
/**
16
 * A `CSSBlockList` is a `CSSList` whose `DeclarationBlock`s are guaranteed to contain valid declaration blocks or
17
 * at-rules.
18
 *
19
 * Most `CSSList`s conform to this category but some at-rules (such as `@keyframes`) do not.
20
 */
21
abstract class CSSBlockList extends CSSList
22
{
23
    /**
24
     * Gets all `DeclarationBlock` objects recursively, no matter how deeply nested the selectors are.
25
     *
26
     * @return list<DeclarationBlock>
27
     */
28
    public function getAllDeclarationBlocks(): array
6✔
29
    {
30
        $result = [];
6✔
31

32
        foreach ($this->contents as $item) {
6✔
33
            if ($item instanceof DeclarationBlock) {
5✔
34
                $result[] = $item;
3✔
35
            } elseif ($item instanceof CSSBlockList) {
3✔
36
                $result = \array_merge($result, $item->getAllDeclarationBlocks());
1✔
37
            }
38
        }
39

40
        return $result;
6✔
41
    }
42

43
    /**
44
     * @param array<int, DeclarationBlock> $result
45
     */
UNCOV
46
    protected function allDeclarationBlocks(array &$result): void
×
47
    {
UNCOV
48
        foreach ($this->contents as $item) {
×
UNCOV
49
            if ($item instanceof DeclarationBlock) {
×
UNCOV
50
                $result[] = $item;
×
UNCOV
51
            } elseif ($item instanceof CSSBlockList) {
×
UNCOV
52
                $item->allDeclarationBlocks($result);
×
53
            }
54
        }
UNCOV
55
    }
×
56

57
    /**
58
     * @param array<int, RuleSet> $result
59
     */
60
    protected function allRuleSets(array &$result): void
9✔
61
    {
62
        foreach ($this->contents as $item) {
9✔
63
            if ($item instanceof RuleSet) {
8✔
64
                $result[] = $item;
6✔
65
            } elseif ($item instanceof CSSBlockList) {
4✔
66
                $item->allRuleSets($result);
2✔
67
            }
68
        }
69
    }
9✔
70

71
    /**
72
     * @param CSSList|Rule|RuleSet|Value $element
73
     * @param array<int, Value> $result
74
     * @param string|null $searchString
75
     * @param bool $searchInFunctionArguments
76
     */
77
    protected function allValues(
×
78
        $element,
79
        array &$result,
80
        $searchString = null,
81
        $searchInFunctionArguments = false
82
    ): void {
83
        if ($element instanceof CSSBlockList) {
×
84
            foreach ($element->getContents() as $oContent) {
×
85
                $this->allValues($oContent, $result, $searchString, $searchInFunctionArguments);
×
86
            }
87
        } elseif ($element instanceof RuleSet) {
×
88
            foreach ($element->getRules($searchString) as $rule) {
×
89
                $this->allValues($rule, $result, $searchString, $searchInFunctionArguments);
×
90
            }
91
        } elseif ($element instanceof Rule) {
×
92
            $this->allValues($element->getValue(), $result, $searchString, $searchInFunctionArguments);
×
93
        } elseif ($element instanceof ValueList) {
×
94
            if ($searchInFunctionArguments || !($element instanceof CSSFunction)) {
×
95
                foreach ($element->getListComponents() as $component) {
×
96
                    $this->allValues($component, $result, $searchString, $searchInFunctionArguments);
×
97
                }
98
            }
99
        } else {
100
            // Non-List `Value` or `CSSString` (CSS identifier)
101
            $result[] = $element;
×
102
        }
103
    }
×
104

105
    /**
106
     * @param array<int, Selector> $result
107
     * @param string|null $specificitySearch
108
     */
109
    protected function allSelectors(array &$result, $specificitySearch = null): void
×
110
    {
111
        /** @var array<int, DeclarationBlock> $declarationBlocks */
112
        $declarationBlocks = [];
×
113
        $this->allDeclarationBlocks($declarationBlocks);
×
114
        foreach ($declarationBlocks as $oBlock) {
×
115
            foreach ($oBlock->getSelectors() as $selector) {
×
116
                if ($specificitySearch === null) {
×
117
                    $result[] = $selector;
×
118
                } else {
119
                    $comparator = '===';
×
120
                    $expressionParts = \explode(' ', $specificitySearch);
×
121
                    $targetSpecificity = $expressionParts[0];
×
122
                    if (\count($expressionParts) > 1) {
×
123
                        $comparator = $expressionParts[0];
×
124
                        $targetSpecificity = $expressionParts[1];
×
125
                    }
126
                    $targetSpecificity = (int) $targetSpecificity;
×
127
                    $selectorSpecificity = $selector->getSpecificity();
×
128
                    $comparatorMatched = false;
×
129
                    switch ($comparator) {
×
130
                        case '<=':
×
131
                            $comparatorMatched = $selectorSpecificity <= $targetSpecificity;
×
132
                            break;
×
133
                        case '<':
×
134
                            $comparatorMatched = $selectorSpecificity < $targetSpecificity;
×
135
                            break;
×
136
                        case '>=':
×
137
                            $comparatorMatched = $selectorSpecificity >= $targetSpecificity;
×
138
                            break;
×
139
                        case '>':
×
140
                            $comparatorMatched = $selectorSpecificity > $targetSpecificity;
×
141
                            break;
×
142
                        default:
143
                            $comparatorMatched = $selectorSpecificity === $targetSpecificity;
×
144
                            break;
×
145
                    }
146
                    if ($comparatorMatched) {
×
147
                        $result[] = $selector;
×
148
                    }
149
                }
150
            }
151
        }
152
    }
×
153
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc