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

MyIntervals / PHP-CSS-Parser / 14362214877

09 Apr 2025 04:25PM UTC coverage: 54.382% (+1.1%) from 53.315%
14362214877

Pull #1237

github

web-flow
Merge dea50e5d0 into b74cf2e47
Pull Request #1237: [TASK] Add `assertInstanceOf` tests for `CSSListItem`

968 of 1780 relevant lines covered (54.38%)

7.65 hits per line

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

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

3
declare(strict_types=1);
4

5
namespace Sabberworm\CSS\CSSList;
6

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

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

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

41
        return $result;
6✔
42
    }
43

44
    /**
45
     * @param list<DeclarationBlock> $result
46
     */
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) {
×
53
                $item->allDeclarationBlocks($result);
×
54
            }
55
        }
56
    }
×
57

58
    /**
59
     * Returns all `RuleSet` objects recursively found in the tree, no matter how deeply nested the rule sets are.
60
     *
61
     * @return list<RuleSet>
62
     */
63
    public function getAllRuleSets(): array
9✔
64
    {
65
        $result = [];
9✔
66

67
        foreach ($this->contents as $item) {
9✔
68
            if ($item instanceof RuleSet) {
8✔
69
                $result[] = $item;
6✔
70
            } elseif ($item instanceof CSSBlockList) {
4✔
71
                $result = \array_merge($result, $item->getAllRuleSets());
2✔
72
            }
73
        }
74

75
        return $result;
9✔
76
    }
77

78
    /**
79
     * @param CSSElement|string $element
80
     * @param list<Value> $result
81
     */
82
    protected function allValues(
×
83
        $element,
84
        array &$result,
85
        ?string $searchString = null,
86
        bool $searchInFunctionArguments = false
87
    ): void {
88
        if ($element instanceof CSSBlockList) {
×
89
            foreach ($element->getContents() as $content) {
×
90
                $this->allValues($content, $result, $searchString, $searchInFunctionArguments);
×
91
            }
92
        } elseif ($element instanceof RuleSet) {
×
93
            foreach ($element->getRules($searchString) as $rule) {
×
94
                $this->allValues($rule, $result, $searchString, $searchInFunctionArguments);
×
95
            }
96
        } elseif ($element instanceof Rule) {
×
97
            $this->allValues($element->getValue(), $result, $searchString, $searchInFunctionArguments);
×
98
        } elseif ($element instanceof ValueList) {
×
99
            if ($searchInFunctionArguments || !($element instanceof CSSFunction)) {
×
100
                foreach ($element->getListComponents() as $component) {
×
101
                    $this->allValues($component, $result, $searchString, $searchInFunctionArguments);
×
102
                }
103
            }
104
        } elseif ($element instanceof Value) {
×
105
            $result[] = $element;
×
106
        }
107
    }
×
108

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