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

MyIntervals / PHP-CSS-Parser / 13921221957

18 Mar 2025 10:41AM UTC coverage: 51.499% (-5.3%) from 56.839%
13921221957

push

github

web-flow
[TASK] Reduce and finetune the scope of `@covers` annotations (#1188)

The legacy tests are not very focused. Until we have split them
up, try to avoid false positives for code coverage.

Also add `@covers` annotations for the parent classes of the
tested classes.

945 of 1835 relevant lines covered (51.5%)

6.71 hits per line

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

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

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

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

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

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

165
        return $result;
6✔
166
    }
167
}
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