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

MyIntervals / PHP-CSS-Parser / 15453153158

04 Jun 2025 09:28PM UTC coverage: 57.682% (+0.4%) from 57.327%
15453153158

Pull #1270

github

web-flow
Merge 7642f68b3 into afdca11ed
Pull Request #1270: [BUGFIX] `AddRule` before sibling with different property name

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

4 existing lines in 1 file now uncovered.

1040 of 1803 relevant lines covered (57.68%)

11.33 hits per line

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

87.12
/src/RuleSet/RuleSet.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Sabberworm\CSS\RuleSet;
6

7
use Sabberworm\CSS\Comment\CommentContainer;
8
use Sabberworm\CSS\CSSElement;
9
use Sabberworm\CSS\CSSList\CSSListItem;
10
use Sabberworm\CSS\OutputFormat;
11
use Sabberworm\CSS\Parsing\ParserState;
12
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
13
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
14
use Sabberworm\CSS\Position\Position;
15
use Sabberworm\CSS\Position\Positionable;
16
use Sabberworm\CSS\Rule\Rule;
17

18
/**
19
 * This class is a container for individual 'Rule's.
20
 *
21
 * The most common form of a rule set is one constrained by a selector, i.e., a `DeclarationBlock`.
22
 * However, unknown `AtRule`s (like `@font-face`) are rule sets as well.
23
 *
24
 * If you want to manipulate a `RuleSet`, use the methods `addRule(Rule $rule)`, `getRules()` and `removeRule($rule)`
25
 * (which accepts either a `Rule` or a rule name; optionally suffixed by a dash to remove all related rules).
26
 *
27
 * Note that `CSSListItem` extends both `Commentable` and `Renderable`,
28
 * so those interfaces must also be implemented by concrete subclasses.
29
 */
30
abstract class RuleSet implements CSSElement, CSSListItem, Positionable, RuleContainer
31
{
32
    use CommentContainer;
33
    use Position;
34

35
    /**
36
     * the rules in this rule set, using the property name as the key,
37
     * with potentially multiple rules per property name.
38
     *
39
     * @var array<string, array<int<0, max>, Rule>>
40
     */
41
    private $rules = [];
42

43
    /**
44
     * @param int<0, max> $lineNumber
45
     */
46
    public function __construct(int $lineNumber = 0)
132✔
47
    {
48
        $this->setPosition($lineNumber);
132✔
49
    }
132✔
50

51
    /**
52
     * @throws UnexpectedTokenException
53
     * @throws UnexpectedEOFException
54
     *
55
     * @internal since V8.8.0
56
     */
57
    public static function parseRuleSet(ParserState $parserState, RuleSet $ruleSet): void
6✔
58
    {
59
        while ($parserState->comes(';')) {
6✔
60
            $parserState->consume(';');
×
61
        }
62
        while (true) {
6✔
63
            $commentsBeforeRule = $parserState->consumeWhiteSpace();
6✔
64
            if ($parserState->comes('}')) {
6✔
65
                break;
6✔
66
            }
67
            $rule = null;
6✔
68
            if ($parserState->getSettings()->usesLenientParsing()) {
6✔
69
                try {
70
                    $rule = Rule::parse($parserState, $commentsBeforeRule);
4✔
71
                } catch (UnexpectedTokenException $e) {
×
72
                    try {
73
                        $consumedText = $parserState->consumeUntil(["\n", ';', '}'], true);
×
74
                        // We need to “unfind” the matches to the end of the ruleSet as this will be matched later
75
                        if ($parserState->streql(\substr($consumedText, -1), '}')) {
×
76
                            $parserState->backtrack(1);
×
77
                        } else {
78
                            while ($parserState->comes(';')) {
×
79
                                $parserState->consume(';');
×
80
                            }
81
                        }
82
                    } catch (UnexpectedTokenException $e) {
×
83
                        // We’ve reached the end of the document. Just close the RuleSet.
84
                        return;
4✔
85
                    }
86
                }
87
            } else {
88
                $rule = Rule::parse($parserState, $commentsBeforeRule);
2✔
89
            }
90
            if ($rule instanceof Rule) {
6✔
91
                $ruleSet->addRule($rule);
6✔
92
            }
93
        }
94
        $parserState->consume('}');
6✔
95
    }
6✔
96

97
    public function addRule(Rule $ruleToAdd, ?Rule $sibling = null): void
127✔
98
    {
99
        $propertyName = $ruleToAdd->getRule();
127✔
100
        if (!isset($this->rules[$propertyName])) {
127✔
101
            $this->rules[$propertyName] = [];
127✔
102
        }
103

104
        $position = \count($this->rules[$propertyName]);
127✔
105

106
        if ($sibling !== null) {
127✔
107
            $siblingLineNumber = $sibling->getLineNumber();
58✔
108
            $siblingColumnNumber = $sibling->getColumnNumber();
58✔
109
            $siblingIsInSet = false;
58✔
110
            $siblingPosition = \array_search($sibling, $this->rules[$propertyName], true);
58✔
111
            if ($siblingPosition !== false) {
58✔
112
                $siblingIsInSet = true;
16✔
113
                $position = $siblingPosition;
16✔
114
            } elseif ($siblingIsInSet = $this->hasRule($sibling)) {
42✔
115
                // Maintain ordering within `$this->rules[$propertyName]`
116
                // by inserting before first `Rule` with a same-or-later position than the sibling.
117
                foreach ($this->rules[$propertyName] as $index => $rule) {
30✔
118
                    if (
119
                        $rule->getLineNumber() > $siblingLineNumber ||
6✔
120
                        $rule->getLineNumber() === $siblingLineNumber &&
3✔
121
                        $rule->getColumnNumber() >= $siblingColumnNumber
6✔
122
                    ) {
123
                        $position = $index;
3✔
124
                        break;
3✔
125
                    }
126
                }
127
            }
128
            if ($siblingIsInSet) {
58✔
129
                // Increment column number of all existing rules on same line, starting at sibling
130
                foreach ($this->rules as $rulesForAProperty) {
46✔
131
                    foreach ($rulesForAProperty as $rule) {
46✔
132
                        if (
133
                            $rule->getLineNumber() === $siblingLineNumber &&
46✔
134
                            $rule->getColumnNumber() >= $siblingColumnNumber
46✔
135
                        ) {
136
                            $rule->setPosition($siblingLineNumber, $rule->getColumnNumber() + 1);
46✔
137
                        }
138
                    }
139
                }
140
                $ruleToAdd->setPosition($siblingLineNumber, $siblingColumnNumber);
46✔
141
            }
142
        }
143

144
        if ($ruleToAdd->getLineNumber() === null) {
127✔
145
            //this node is added manually, give it the next best line
146
            $columnNumber = $ruleToAdd->getColumnNumber() ?? 0;
117✔
147
            $rules = $this->getRules();
117✔
148
            $rulesCount = \count($rules);
117✔
149
            if ($rulesCount > 0) {
117✔
150
                $last = $rules[$rulesCount - 1];
88✔
151
                $ruleToAdd->setPosition($last->getLineNo() + 1, $columnNumber);
88✔
152
            } else {
153
                $ruleToAdd->setPosition(1, $columnNumber);
117✔
154
            }
155
        } elseif ($ruleToAdd->getColumnNumber() === null) {
75✔
156
            $ruleToAdd->setPosition($ruleToAdd->getLineNumber(), 0);
12✔
157
        }
158

159
        \array_splice($this->rules[$propertyName], $position, 0, [$ruleToAdd]);
127✔
160
    }
127✔
161

162
    /**
163
     * Returns all rules matching the given rule name
164
     *
165
     * @example $ruleSet->getRules('font') // returns array(0 => $rule, …) or array().
166
     *
167
     * @example $ruleSet->getRules('font-')
168
     *          //returns an array of all rules either beginning with font- or matching font.
169
     *
170
     * @param string|null $searchPattern
171
     *        Pattern to search for. If null, returns all rules.
172
     *        If the pattern ends with a dash, all rules starting with the pattern are returned
173
     *        as well as one matching the pattern with the dash excluded.
174
     *
175
     * @return array<int<0, max>, Rule>
176
     */
177
    public function getRules(?string $searchPattern = null): array
129✔
178
    {
179
        $result = [];
129✔
180
        foreach ($this->rules as $propertyName => $rules) {
129✔
181
            // Either no search rule is given or the search rule matches the found rule exactly
182
            // or the search rule ends in “-” and the found rule starts with the search rule.
183
            if (
184
                $searchPattern === null || $propertyName === $searchPattern
127✔
185
                || (
186
                    \strrpos($searchPattern, '-') === \strlen($searchPattern) - \strlen('-')
1✔
187
                    && (\strpos($propertyName, $searchPattern) === 0
1✔
188
                        || $propertyName === \substr($searchPattern, 0, -1))
127✔
189
                )
190
            ) {
191
                $result = \array_merge($result, $rules);
127✔
192
            }
193
        }
194
        \usort($result, [self::class, 'comparePositionable']);
129✔
195

196
        return $result;
129✔
197
    }
198

199
    /**
200
     * Overrides all the rules of this set.
201
     *
202
     * @param array<Rule> $rules The rules to override with.
203
     */
204
    public function setRules(array $rules): void
124✔
205
    {
206
        $this->rules = [];
124✔
207
        foreach ($rules as $rule) {
124✔
208
            $this->addRule($rule);
107✔
209
        }
210
    }
124✔
211

212
    /**
213
     * Returns all rules matching the given pattern and returns them in an associative array with the rule’s name
214
     * as keys. This method exists mainly for backwards-compatibility and is really only partially useful.
215
     *
216
     * Note: This method loses some information: Calling this (with an argument of `background-`) on a declaration block
217
     * like `{ background-color: green; background-color; rgba(0, 127, 0, 0.7); }` will only yield an associative array
218
     * containing the rgba-valued rule while `getRules()` would yield an indexed array containing both.
219
     *
220
     * @param string|null $searchPattern
221
     *        Pattern to search for. If null, returns all rules. If the pattern ends with a dash,
222
     *        all rules starting with the pattern are returned as well as one matching the pattern with the dash
223
     *        excluded.
224
     *
225
     * @return array<string, Rule>
226
     */
227
    public function getRulesAssoc(?string $searchPattern = null): array
14✔
228
    {
229
        /** @var array<string, Rule> $result */
230
        $result = [];
14✔
231
        foreach ($this->getRules($searchPattern) as $rule) {
14✔
232
            $result[$rule->getRule()] = $rule;
8✔
233
        }
234

235
        return $result;
14✔
236
    }
237

238
    /**
239
     * Removes a `Rule` from this `RuleSet` by identity.
240
     */
241
    public function removeRule(Rule $ruleToRemove): void
×
242
    {
243
        $nameOfPropertyToRemove = $ruleToRemove->getRule();
×
244
        if (!isset($this->rules[$nameOfPropertyToRemove])) {
×
245
            return;
×
246
        }
UNCOV
247
        foreach ($this->rules[$nameOfPropertyToRemove] as $key => $rule) {
×
248
            if ($rule === $ruleToRemove) {
×
UNCOV
249
                unset($this->rules[$nameOfPropertyToRemove][$key]);
×
250
            }
251
        }
UNCOV
252
    }
×
253

254
    /**
255
     * Removes rules by property name or search pattern.
256
     *
257
     * @param string $searchPattern
258
     *        pattern to remove.
259
     *        If the pattern ends in a dash,
260
     *        all rules starting with the pattern are removed as well as one matching the pattern with the dash
261
     *        excluded.
262
     */
263
    public function removeMatchingRules(string $searchPattern): void
14✔
264
    {
265
        foreach ($this->rules as $propertyName => $rules) {
14✔
266
            // Either the search rule matches the found rule exactly
267
            // or the search rule ends in “-” and the found rule starts with the search rule or equals it
268
            // (without the trailing dash).
269
            if (
270
                $propertyName === $searchPattern
13✔
271
                || (\strrpos($searchPattern, '-') === \strlen($searchPattern) - \strlen('-')
11✔
272
                    && (\strpos($propertyName, $searchPattern) === 0
6✔
273
                        || $propertyName === \substr($searchPattern, 0, -1)))
13✔
274
            ) {
275
                unset($this->rules[$propertyName]);
12✔
276
            }
277
        }
278
    }
14✔
279

280
    public function removeAllRules(): void
4✔
281
    {
282
        $this->rules = [];
4✔
283
    }
4✔
284

285
    protected function renderRules(OutputFormat $outputFormat): string
5✔
286
    {
287
        $result = '';
5✔
288
        $isFirst = true;
5✔
289
        $nextLevelFormat = $outputFormat->nextLevel();
5✔
290
        foreach ($this->getRules() as $rule) {
5✔
291
            $nextLevelFormatter = $nextLevelFormat->getFormatter();
5✔
292
            $renderedRule = $nextLevelFormatter->safely(static function () use ($rule, $nextLevelFormat): string {
293
                return $rule->render($nextLevelFormat);
5✔
294
            });
5✔
295
            if ($renderedRule === null) {
5✔
UNCOV
296
                continue;
×
297
            }
298
            if ($isFirst) {
5✔
299
                $isFirst = false;
5✔
300
                $result .= $nextLevelFormatter->spaceBeforeRules();
5✔
301
            } else {
302
                $result .= $nextLevelFormatter->spaceBetweenRules();
1✔
303
            }
304
            $result .= $renderedRule;
5✔
305
        }
306

307
        $formatter = $outputFormat->getFormatter();
5✔
308
        if (!$isFirst) {
5✔
309
            // Had some output
310
            $result .= $formatter->spaceAfterRules();
5✔
311
        }
312

313
        return $formatter->removeLastSemicolon($result);
5✔
314
    }
315

316
    /**
317
     * @return int negative if `$first` is before `$second`; zero if they have the same position; positive otherwise
318
     */
319
    private static function comparePositionable(Positionable $first, Positionable $second): int
90✔
320
    {
321
        if ($first->getLineNo() === $second->getLineNo()) {
90✔
322
            return $first->getColNo() - $second->getColNo();
17✔
323
        }
324
        return $first->getLineNo() - $second->getLineNo();
86✔
325
    }
326

327
    private function hasRule(Rule $rule): bool
42✔
328
    {
329
        foreach ($this->rules as $rulesForAProperty) {
42✔
330
            if (\in_array($rule, $rulesForAProperty, true)) {
42✔
331
                return true;
30✔
332
            }
333
        }
334

335
        return false;
12✔
336
    }
337
}
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