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

MyIntervals / PHP-CSS-Parser / 13708912684

06 Mar 2025 09:53PM UTC coverage: 55.626%. Remained the same
13708912684

push

github

web-flow
[CLEANUP] Avoid Hungarian notation in `CSSBlockList` (#1104)

Part of #756

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

1053 of 1893 relevant lines covered (55.63%)

12.21 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
                $result = \array_merge($result, $item->getAllDeclarationBlocks());
1✔
37
            }
38
        }
39

40
        return $result;
6✔
41
    }
42

43
    /**
44
     * @param array<int, 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 array<int, Value> $result
80
     * @param string|null $searchString
81
     * @param bool $searchInFunctionArguments
82
     */
83
    protected function allValues(
×
84
        $element,
85
        array &$result,
86
        $searchString = null,
87
        $searchInFunctionArguments = false
88
    ): void {
89
        if ($element instanceof CSSBlockList) {
×
NEW
90
            foreach ($element->getContents() as $content) {
×
NEW
91
                $this->allValues($content, $result, $searchString, $searchInFunctionArguments);
×
92
            }
93
        } elseif ($element instanceof RuleSet) {
×
94
            foreach ($element->getRules($searchString) as $rule) {
×
95
                $this->allValues($rule, $result, $searchString, $searchInFunctionArguments);
×
96
            }
97
        } elseif ($element instanceof Rule) {
×
98
            $this->allValues($element->getValue(), $result, $searchString, $searchInFunctionArguments);
×
99
        } elseif ($element instanceof ValueList) {
×
100
            if ($searchInFunctionArguments || !($element instanceof CSSFunction)) {
×
101
                foreach ($element->getListComponents() as $component) {
×
102
                    $this->allValues($component, $result, $searchString, $searchInFunctionArguments);
×
103
                }
104
            }
105
        } else {
106
            // Non-List `Value` or `CSSString` (CSS identifier)
107
            $result[] = $element;
×
108
        }
109
    }
×
110

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