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

MyIntervals / PHP-CSS-Parser / 13603693513

01 Mar 2025 10:08AM UTC coverage: 55.776% (+0.02%) from 55.753%
13603693513

Pull #1045

github

web-flow
Merge 79e1892cf into 7ef82db15
Pull Request #1045: [CLEANUP] Simplify some array handling

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

16 existing lines in 1 file now uncovered.

1067 of 1913 relevant lines covered (55.78%)

12.32 hits per line

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

22.22
/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
                $subBlocks = $item->getAllDeclarationBlocks();
1✔
37
                \array_push($result, ...$subBlocks);
1✔
38
            }
39
        }
40

41
        return $result;
6✔
42
    }
43

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

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

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

106
    /**
107
     * @param array<int, Selector> $result
108
     * @param string|null $specificitySearch
109
     */
UNCOV
110
    protected function allSelectors(array &$result, $specificitySearch = null): void
×
111
    {
112
        /** @var array<int, DeclarationBlock> $declarationBlocks */
113
        $declarationBlocks = [];
×
114
        $this->allDeclarationBlocks($declarationBlocks);
×
115
        foreach ($declarationBlocks as $oBlock) {
×
116
            foreach ($oBlock->getSelectors() as $selector) {
×
117
                if ($specificitySearch === null) {
×
UNCOV
118
                    $result[] = $selector;
×
119
                } else {
120
                    $comparator = '===';
×
121
                    $expressionParts = \explode(' ', $specificitySearch);
×
122
                    $targetSpecificity = $expressionParts[0];
×
123
                    if (\count($expressionParts) > 1) {
×
124
                        $comparator = $expressionParts[0];
×
UNCOV
125
                        $targetSpecificity = $expressionParts[1];
×
126
                    }
127
                    $targetSpecificity = (int) $targetSpecificity;
×
128
                    $selectorSpecificity = $selector->getSpecificity();
×
129
                    $comparatorMatched = false;
×
130
                    switch ($comparator) {
×
131
                        case '<=':
×
132
                            $comparatorMatched = $selectorSpecificity <= $targetSpecificity;
×
133
                            break;
×
134
                        case '<':
×
135
                            $comparatorMatched = $selectorSpecificity < $targetSpecificity;
×
136
                            break;
×
137
                        case '>=':
×
138
                            $comparatorMatched = $selectorSpecificity >= $targetSpecificity;
×
139
                            break;
×
140
                        case '>':
×
141
                            $comparatorMatched = $selectorSpecificity > $targetSpecificity;
×
UNCOV
142
                            break;
×
143
                        default:
144
                            $comparatorMatched = $selectorSpecificity === $targetSpecificity;
×
UNCOV
145
                            break;
×
146
                    }
147
                    if ($comparatorMatched) {
×
UNCOV
148
                        $result[] = $selector;
×
149
                    }
150
                }
151
            }
152
        }
UNCOV
153
    }
×
154
}
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

© 2026 Coveralls, Inc