• 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

93.81
/src/OutputFormatter.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Sabberworm\CSS;
6

7
use Sabberworm\CSS\Comment\Commentable;
8
use Sabberworm\CSS\Parsing\OutputException;
9

10
/**
11
 * @internal since 8.8.0
12
 */
13
class OutputFormatter
14
{
15
    /**
16
     * @var OutputFormat
17
     */
18
    private $outputFormat;
19

20
    public function __construct(OutputFormat $outputFormat)
50✔
21
    {
22
        $this->outputFormat = $outputFormat;
50✔
23
    }
50✔
24

25
    /**
26
     * @param non-empty-string $name
27
     *
28
     * @throws \InvalidArgumentException
29
     */
30
    public function space(string $name): string
18✔
31
    {
32
        switch ($name) {
18✔
33
            case 'AfterRuleName':
18✔
34
                $spaceString = $this->outputFormat->getSpaceAfterRuleName();
1✔
35
                break;
1✔
36
            case 'BeforeRules':
17✔
37
                $spaceString = $this->outputFormat->getSpaceBeforeRules();
1✔
38
                break;
1✔
39
            case 'AfterRules':
16✔
40
                $spaceString = $this->outputFormat->getSpaceAfterRules();
1✔
41
                break;
1✔
42
            case 'BetweenRules':
15✔
43
                $spaceString = $this->outputFormat->getSpaceBetweenRules();
1✔
44
                break;
1✔
45
            case 'BeforeBlocks':
14✔
46
                $spaceString = $this->outputFormat->getSpaceBeforeBlocks();
1✔
47
                break;
1✔
48
            case 'AfterBlocks':
13✔
49
                $spaceString = $this->outputFormat->getSpaceAfterBlocks();
7✔
50
                break;
7✔
51
            case 'BetweenBlocks':
10✔
52
                $spaceString = $this->outputFormat->getSpaceBetweenBlocks();
5✔
53
                break;
5✔
54
            case 'BeforeSelectorSeparator':
5✔
55
                $spaceString = $this->outputFormat->getSpaceBeforeSelectorSeparator();
1✔
56
                break;
1✔
57
            case 'AfterSelectorSeparator':
4✔
58
                $spaceString = $this->outputFormat->getSpaceAfterSelectorSeparator();
1✔
59
                break;
1✔
60
            case 'BeforeOpeningBrace':
3✔
61
                $spaceString = $this->outputFormat->getSpaceBeforeOpeningBrace();
1✔
62
                break;
1✔
63
            case 'BeforeListArgumentSeparator':
2✔
64
                $spaceString = $this->outputFormat->getSpaceBeforeListArgumentSeparator();
1✔
65
                break;
1✔
66
            case 'AfterListArgumentSeparator':
1✔
67
                $spaceString = $this->outputFormat->getSpaceAfterListArgumentSeparator();
1✔
68
                break;
1✔
69
            default:
70
                throw new \InvalidArgumentException("Unknown space type: $name", 1740049248);
×
71
        }
72

73
        return $this->prepareSpace($spaceString);
18✔
74
    }
75

76
    public function spaceAfterRuleName(): string
1✔
77
    {
78
        return $this->space('AfterRuleName');
1✔
79
    }
80

81
    public function spaceBeforeRules(): string
1✔
82
    {
83
        return $this->space('BeforeRules');
1✔
84
    }
85

86
    public function spaceAfterRules(): string
1✔
87
    {
88
        return $this->space('AfterRules');
1✔
89
    }
90

91
    public function spaceBetweenRules(): string
1✔
92
    {
93
        return $this->space('BetweenRules');
1✔
94
    }
95

96
    public function spaceBeforeBlocks(): string
1✔
97
    {
98
        return $this->space('BeforeBlocks');
1✔
99
    }
100

101
    public function spaceAfterBlocks(): string
7✔
102
    {
103
        return $this->space('AfterBlocks');
7✔
104
    }
105

106
    public function spaceBetweenBlocks(): string
5✔
107
    {
108
        return $this->space('BetweenBlocks');
5✔
109
    }
110

111
    public function spaceBeforeSelectorSeparator(): string
1✔
112
    {
113
        return $this->space('BeforeSelectorSeparator');
1✔
114
    }
115

116
    public function spaceAfterSelectorSeparator(): string
1✔
117
    {
118
        return $this->space('AfterSelectorSeparator');
1✔
119
    }
120

121
    /**
122
     * @param non-empty-string $separator
123
     */
124
    public function spaceBeforeListArgumentSeparator(string $separator): string
2✔
125
    {
126
        $spaceForSeparator = $this->outputFormat->getSpaceBeforeListArgumentSeparators();
2✔
127

128
        return $spaceForSeparator[$separator] ?? $this->space('BeforeListArgumentSeparator');
2✔
129
    }
130

131
    /**
132
     * @param non-empty-string $separator
133
     */
134
    public function spaceAfterListArgumentSeparator(string $separator): string
2✔
135
    {
136
        $spaceForSeparator = $this->outputFormat->getSpaceAfterListArgumentSeparators();
2✔
137

138
        return $spaceForSeparator[$separator] ?? $this->space('AfterListArgumentSeparator');
2✔
139
    }
140

141
    public function spaceBeforeOpeningBrace(): string
1✔
142
    {
143
        return $this->space('BeforeOpeningBrace');
1✔
144
    }
145

146
    /**
147
     * Runs the given code, either swallowing or passing exceptions, depending on the `ignoreExceptions` setting.
148
     */
149
    public function safely(callable $callable): ?string
×
150
    {
151
        if ($this->outputFormat->shouldIgnoreExceptions()) {
×
152
            // If output exceptions are ignored, run the code with exception guards
153
            try {
154
                return $callable();
×
155
            } catch (OutputException $e) {
×
156
                return null;
×
157
            } // Do nothing
158
        } else {
159
            // Run the code as-is
160
            return $callable();
×
161
        }
162
    }
163

164
    /**
165
     * Clone of the `implode` function, but calls `render` with the current output format.
166
     *
167
     * @param array<array-key, Renderable|string> $values
168
     */
169
    public function implode(string $separator, array $values, bool $increaseLevel = false): string
7✔
170
    {
171
        $result = '';
7✔
172
        $outputFormat = $this->outputFormat;
7✔
173
        if ($increaseLevel) {
7✔
174
            $outputFormat = $outputFormat->nextLevel();
1✔
175
        }
176
        $isFirst = true;
7✔
177
        foreach ($values as $value) {
7✔
178
            if ($isFirst) {
6✔
179
                $isFirst = false;
6✔
180
            } else {
181
                $result .= $separator;
2✔
182
            }
183
            if ($value instanceof Renderable) {
6✔
184
                $result .= $value->render($outputFormat);
4✔
185
            } else {
186
                $result .= $value;
2✔
187
            }
188
        }
189
        return $result;
7✔
190
    }
191

192
    public function removeLastSemicolon(string $string): string
16✔
193
    {
194
        if ($this->outputFormat->shouldRenderSemicolonAfterLastRule()) {
16✔
195
            return $string;
5✔
196
        }
197

198
        $parts = \explode(';', $string);
11✔
199
        if (\count($parts) < 2) {
11✔
200
            return $parts[0];
2✔
201
        }
202
        $lastPart = \array_pop($parts);
9✔
203
        $nextToLastPart = \array_pop($parts);
9✔
204
        \array_push($parts, $nextToLastPart . $lastPart);
9✔
205

206
        return \implode(';', $parts);
9✔
207
    }
208

209
    public function comments(Commentable $commentable): string
13✔
210
    {
211
        if (!$this->outputFormat->shouldRenderComments()) {
13✔
212
            return '';
4✔
213
        }
214

215
        $result = '';
9✔
216
        $comments = $commentable->getComments();
9✔
217
        $lastCommentIndex = \count($comments) - 1;
9✔
218

219
        foreach ($comments as $i => $comment) {
9✔
220
            $result .= $comment->render($this->outputFormat);
6✔
221
            $result .= $i === $lastCommentIndex ? $this->spaceAfterBlocks() : $this->spaceBetweenBlocks();
6✔
222
        }
223
        return $result;
9✔
224
    }
225

226
    private function prepareSpace(string $spaceString): string
18✔
227
    {
228
        return \str_replace("\n", "\n" . $this->indent(), $spaceString);
18✔
229
    }
230

231
    private function indent(): string
18✔
232
    {
233
        return \str_repeat($this->outputFormat->getIndentation(), $this->outputFormat->getIndentationLevel());
18✔
234
    }
235
}
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