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

MyIntervals / PHP-CSS-Parser / 13539110933

26 Feb 2025 07:59AM UTC coverage: 54.416% (-0.4%) from 54.784%
13539110933

Pull #996

github

web-flow
Merge ae0d88373 into b3abf1a5d
Pull Request #996: [TASK] Move up `getAllDeclarationBlocks` to `CSSBlockList`

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

7 existing lines in 1 file now uncovered.

1035 of 1902 relevant lines covered (54.42%)

12.03 hits per line

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

16.42
/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 array<int, DeclarationBlock>
27
     */
28
    public function getAllDeclarationBlocks(): array
6✔
29
    {
30
        /** @var array<int, DeclarationBlock> $result */
31
        $result = [];
6✔
32
        $this->allDeclarationBlocks($result);
6✔
33
        return $result;
6✔
34
    }
35

36
    /**
37
     * @param array<int, DeclarationBlock> $result
38
     */
39
    protected function allDeclarationBlocks(array &$result): void
6✔
40
    {
41
        foreach ($this->contents as $item) {
6✔
42
            if ($item instanceof DeclarationBlock) {
5✔
43
                $result[] = $item;
3✔
44
            } elseif ($item instanceof CSSBlockList) {
3✔
45
                $item->allDeclarationBlocks($result);
1✔
46
            }
47
        }
48
    }
6✔
49

50
    /**
51
     * @param array<int, RuleSet> $result
52
     */
UNCOV
53
    protected function allRuleSets(array &$result): void
×
54
    {
UNCOV
55
        foreach ($this->contents as $item) {
×
UNCOV
56
            if ($item instanceof RuleSet) {
×
UNCOV
57
                $result[] = $item;
×
UNCOV
58
            } elseif ($item instanceof CSSBlockList) {
×
UNCOV
59
                $item->allRuleSets($result);
×
60
            }
61
        }
UNCOV
62
    }
×
63

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

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