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

MyIntervals / PHP-CSS-Parser / 15946496682

28 Jun 2025 05:24PM UTC coverage: 57.912% (+0.006%) from 57.906%
15946496682

Pull #1294

github

web-flow
Merge a24115faa into 11e634c0b
Pull Request #1294: [CLEANUP] Tidy up `DeclarationBlock::parse()`

5 of 11 new or added lines in 1 file covered. (45.45%)

11 existing lines in 1 file now uncovered.

1054 of 1820 relevant lines covered (57.91%)

16.67 hits per line

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

66.22
/src/RuleSet/DeclarationBlock.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Sabberworm\CSS\RuleSet;
6

7
use Sabberworm\CSS\CSSList\CSSList;
8
use Sabberworm\CSS\CSSList\KeyFrame;
9
use Sabberworm\CSS\OutputFormat;
10
use Sabberworm\CSS\Parsing\OutputException;
11
use Sabberworm\CSS\Parsing\ParserState;
12
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
13
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
14
use Sabberworm\CSS\Property\KeyframeSelector;
15
use Sabberworm\CSS\Property\Selector;
16

17
/**
18
 * This class represents a `RuleSet` constrained by a `Selector`.
19
 *
20
 * It contains an array of selector objects (comma-separated in the CSS) as well as the rules to be applied to the
21
 * matching elements.
22
 *
23
 * Declaration blocks usually appear directly inside a `Document` or another `CSSList` (mostly a `MediaQuery`).
24
 */
25
class DeclarationBlock extends RuleSet
26
{
27
    /**
28
     * @var array<Selector|string>
29
     */
30
    private $selectors = [];
31

32
    /**
33
     * @throws UnexpectedTokenException
34
     * @throws UnexpectedEOFException
35
     *
36
     * @internal since V8.8.0
37
     */
38
    public static function parse(ParserState $parserState, ?CSSList $list = null): ?DeclarationBlock
9✔
39
    {
40
        $comments = [];
9✔
41
        $result = new DeclarationBlock($parserState->currentLine());
9✔
42
        try {
43
            $selectorParts = [];
9✔
44
            do {
45
                $selectorParts[] = $parserState->consume(1)
9✔
46
                    . $parserState->consumeUntil(['{', '}', '\'', '"'], false, false, $comments);
9✔
47
                $nextCharacter = $parserState->peek();
9✔
48
                switch ($nextCharacter) {
9✔
49
                    case '\'':
9✔
50
                    case '"':
9✔
NEW
51
                        if (!isset($stringWrapperCharacter)) {
×
NEW
52
                            $stringWrapperCharacter = $nextCharacter;
×
NEW
53
                        } elseif ($stringWrapperCharacter === $nextCharacter) {
×
NEW
54
                            if (\substr(\end($selectorParts), -1) !== '\\') {
×
NEW
55
                                unset($stringWrapperCharacter);
×
56
                            }
57
                        }
NEW
58
                        break;
×
59
                }
60
            } while (!\in_array($nextCharacter, ['{', '}'], true) || isset($stringWrapperCharacter));
9✔
61
            $result->setSelectors(\implode('', $selectorParts), $list);
9✔
62
            if ($parserState->comes('{')) {
6✔
63
                $parserState->consume(1);
6✔
64
            }
65
        } catch (UnexpectedTokenException $e) {
3✔
66
            if ($parserState->getSettings()->usesLenientParsing()) {
3✔
67
                if (!$parserState->comes('}')) {
3✔
68
                    $parserState->consumeUntil('}', false, true);
1✔
69
                }
70
                return null;
3✔
71
            } else {
UNCOV
72
                throw $e;
×
73
            }
74
        }
75
        $result->setComments($comments);
6✔
76
        RuleSet::parseRuleSet($parserState, $result);
6✔
77
        return $result;
6✔
78
    }
79

80
    /**
81
     * @param array<Selector|string>|string $selectors
82
     *
83
     * @throws UnexpectedTokenException
84
     */
85
    public function setSelectors($selectors, ?CSSList $list = null): void
10✔
86
    {
87
        if (\is_array($selectors)) {
10✔
88
            $this->selectors = $selectors;
1✔
89
        } else {
90
            $this->selectors = \explode(',', $selectors);
9✔
91
        }
92
        foreach ($this->selectors as $key => $selector) {
10✔
93
            if (!($selector instanceof Selector)) {
10✔
94
                if ($list === null || !($list instanceof KeyFrame)) {
9✔
95
                    if (!Selector::isValid($selector)) {
9✔
96
                        throw new UnexpectedTokenException(
3✔
97
                            "Selector did not match '" . Selector::SELECTOR_VALIDATION_RX . "'.",
3✔
98
                            $selectors,
99
                            'custom'
3✔
100
                        );
101
                    }
102
                    $this->selectors[$key] = new Selector($selector);
6✔
103
                } else {
UNCOV
104
                    if (!KeyframeSelector::isValid($selector)) {
×
UNCOV
105
                        throw new UnexpectedTokenException(
×
106
                            "Selector did not match '" . KeyframeSelector::SELECTOR_VALIDATION_RX . "'.",
×
107
                            $selector,
108
                            'custom'
×
109
                        );
110
                    }
UNCOV
111
                    $this->selectors[$key] = new KeyframeSelector($selector);
×
112
                }
113
            }
114
        }
115
    }
7✔
116

117
    /**
118
     * Remove one of the selectors of the block.
119
     *
120
     * @param Selector|string $selectorToRemove
121
     */
UNCOV
122
    public function removeSelector($selectorToRemove): bool
×
123
    {
124
        if ($selectorToRemove instanceof Selector) {
×
UNCOV
125
            $selectorToRemove = $selectorToRemove->getSelector();
×
126
        }
127
        foreach ($this->selectors as $key => $selector) {
×
UNCOV
128
            if ($selector->getSelector() === $selectorToRemove) {
×
129
                unset($this->selectors[$key]);
×
130
                return true;
×
131
            }
132
        }
UNCOV
133
        return false;
×
134
    }
135

136
    /**
137
     * @return array<Selector>
138
     */
UNCOV
139
    public function getSelectors(): array
×
140
    {
141
        return $this->selectors;
×
142
    }
143

144
    /**
145
     * @return non-empty-string
146
     *
147
     * @throws OutputException
148
     */
149
    public function render(OutputFormat $outputFormat): string
6✔
150
    {
151
        $formatter = $outputFormat->getFormatter();
6✔
152
        $result = $formatter->comments($this);
6✔
153
        if (\count($this->selectors) === 0) {
6✔
154
            // If all the selectors have been removed, this declaration block becomes invalid
UNCOV
155
            throw new OutputException(
×
UNCOV
156
                'Attempt to print declaration block with missing selector',
×
157
                $this->getLineNumber()
×
158
            );
159
        }
160
        $result .= $outputFormat->getContentBeforeDeclarationBlock();
6✔
161
        $result .= $formatter->implode(
6✔
162
            $formatter->spaceBeforeSelectorSeparator() . ',' . $formatter->spaceAfterSelectorSeparator(),
6✔
163
            $this->selectors
6✔
164
        );
165
        $result .= $outputFormat->getContentAfterDeclarationBlockSelectors();
6✔
166
        $result .= $formatter->spaceBeforeOpeningBrace() . '{';
6✔
167
        $result .= $this->renderRules($outputFormat);
6✔
168
        $result .= '}';
6✔
169
        $result .= $outputFormat->getContentAfterDeclarationBlock();
6✔
170

171
        return $result;
6✔
172
    }
173
}
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