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

MyIntervals / PHP-CSS-Parser / 13910832758

17 Mar 2025 10:09PM UTC coverage: 56.839% (-0.03%) from 56.87%
13910832758

push

github

web-flow
[TASK] Add native type declarations for `CSSBlockList` (#1183)

Also add some more type checks to ensure that the corresponding
types are actually returned.

Part of #811

0 of 2 new or added lines in 1 file covered. (0.0%)

2 existing lines in 1 file now uncovered.

1043 of 1835 relevant lines covered (56.84%)

12.88 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\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 list<DeclarationBlock> $result
45
     */
46
    protected function allDeclarationBlocks(array &$result): void
×
47
    {
48
        foreach ($this->contents as $item) {
×
49
            if ($item instanceof DeclarationBlock) {
×
50
                $result[] = $item;
×
51
            } elseif ($item instanceof CSSBlockList) {
×
52
                $item->allDeclarationBlocks($result);
×
53
            }
54
        }
55
    }
×
56

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

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

74
        return $result;
9✔
75
    }
76

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

108
    /**
109
     * @param list<Selector> $result
110
     */
NEW
111
    protected function allSelectors(array &$result, ?string $specificitySearch = null): void
×
112
    {
UNCOV
113
        $declarationBlocks = [];
×
114
        $this->allDeclarationBlocks($declarationBlocks);
×
115
        foreach ($declarationBlocks as $declarationBlock) {
×
116
            foreach ($declarationBlock->getSelectors() as $selector) {
×
117
                if ($specificitySearch === null) {
×
118
                    $result[] = $selector;
×
119
                } else {
120
                    $comparator = '===';
×
121
                    $expressionParts = \explode(' ', $specificitySearch);
×
122
                    $targetSpecificity = $expressionParts[0];
×
123
                    if (\count($expressionParts) > 1) {
×
124
                        $comparator = $expressionParts[0];
×
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;
×
142
                            break;
×
143
                        default:
144
                            $comparatorMatched = $selectorSpecificity === $targetSpecificity;
×
145
                            break;
×
146
                    }
147
                    if ($comparatorMatched) {
×
148
                        $result[] = $selector;
×
149
                    }
150
                }
151
            }
152
        }
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

© 2025 Coveralls, Inc