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

MyIntervals / PHP-CSS-Parser / 13442235950

20 Feb 2025 06:33PM UTC coverage: 52.281% (+0.6%) from 51.654%
13442235950

push

github

web-flow
[CLEANUP] Use accessors in `OutputFormatter` (#966)

Avoid direct access to `OutputFormat` properties as well
as variable access and magic methods.

Also drop some dead code.

We might possibly want to drop the `space` method altogether,
but that's out of scope for this change.

43 of 44 new or added lines in 1 file covered. (97.73%)

6 existing lines in 1 file now uncovered.

997 of 1907 relevant lines covered (52.28%)

11.87 hits per line

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

96.46
/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
class OutputFormatter
11
{
12
    /**
13
     * @var OutputFormat
14
     */
15
    private $outputFormat;
16

17
    public function __construct(OutputFormat $outputFormat)
52✔
18
    {
19
        $this->outputFormat = $outputFormat;
52✔
20
    }
52✔
21

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

70
        return $this->prepareSpace($sSpaceString);
20✔
71
    }
72

73
    public function spaceAfterRuleName(): string
3✔
74
    {
75
        return $this->space('AfterRuleName');
3✔
76
    }
77

78
    public function spaceBeforeRules(): string
3✔
79
    {
80
        return $this->space('BeforeRules');
3✔
81
    }
82

83
    public function spaceAfterRules(): string
3✔
84
    {
85
        return $this->space('AfterRules');
3✔
86
    }
87

88
    public function spaceBetweenRules(): string
1✔
89
    {
90
        return $this->space('BetweenRules');
1✔
91
    }
92

93
    public function spaceBeforeBlocks(): string
3✔
94
    {
95
        return $this->space('BeforeBlocks');
3✔
96
    }
97

98
    public function spaceAfterBlocks(): string
9✔
99
    {
100
        return $this->space('AfterBlocks');
9✔
101
    }
102

103
    public function spaceBetweenBlocks(): string
7✔
104
    {
105
        return $this->space('BetweenBlocks');
7✔
106
    }
107

108
    public function spaceBeforeSelectorSeparator(): string
3✔
109
    {
110
        return $this->space('BeforeSelectorSeparator');
3✔
111
    }
112

113
    public function spaceAfterSelectorSeparator(): string
3✔
114
    {
115
        return $this->space('AfterSelectorSeparator');
3✔
116
    }
117

118
    /**
119
     * @param non-empty-string $sSeparator
120
     */
121
    public function spaceBeforeListArgumentSeparator(string $sSeparator): string
2✔
122
    {
123
        $spaceForSeparator = $this->outputFormat->getSpaceBeforeListArgumentSeparators();
2✔
124

125
        return $spaceForSeparator[$sSeparator] ?? $this->space('BeforeListArgumentSeparator');
2✔
126
    }
127

128
    /**
129
     * @param non-empty-string $sSeparator
130
     */
131
    public function spaceAfterListArgumentSeparator(string $sSeparator): string
2✔
132
    {
133
        $spaceForSeparator = $this->outputFormat->getSpaceAfterListArgumentSeparators();
2✔
134

135
        return $spaceForSeparator[$sSeparator] ?? $this->space('AfterListArgumentSeparator');
2✔
136
    }
137

138
    public function spaceBeforeOpeningBrace(): string
3✔
139
    {
140
        return $this->space('BeforeOpeningBrace');
3✔
141
    }
142

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

161
    /**
162
     * Clone of the `implode` function, but calls `render` with the current output format instead of `__toString()`.
163
     *
164
     * @param array<array-key, Renderable|string> $aValues
165
     */
166
    public function implode(string $sSeparator, array $aValues, bool $bIncreaseLevel = false): string
9✔
167
    {
168
        $result = '';
9✔
169
        $outputFormat = $this->outputFormat;
9✔
170
        if ($bIncreaseLevel) {
9✔
171
            $outputFormat = $outputFormat->nextLevel();
1✔
172
        }
173
        $bIsFirst = true;
9✔
174
        foreach ($aValues as $mValue) {
9✔
175
            if ($bIsFirst) {
8✔
176
                $bIsFirst = false;
8✔
177
            } else {
178
                $result .= $sSeparator;
4✔
179
            }
180
            if ($mValue instanceof Renderable) {
8✔
181
                $result .= $mValue->render($outputFormat);
4✔
182
            } else {
183
                $result .= $mValue;
4✔
184
            }
185
        }
186
        return $result;
9✔
187
    }
188

189
    public function removeLastSemicolon(string $sString): string
18✔
190
    {
191
        if ($this->outputFormat->getSemicolonAfterLastRule()) {
18✔
192
            return $sString;
7✔
193
        }
194
        $sString = \explode(';', $sString);
11✔
195
        if (\count($sString) < 2) {
11✔
196
            return $sString[0];
2✔
197
        }
198
        $sLast = \array_pop($sString);
9✔
199
        $sNextToLast = \array_pop($sString);
9✔
200
        \array_push($sString, $sNextToLast . $sLast);
9✔
201
        return \implode(';', $sString);
9✔
202
    }
203

204
    public function comments(Commentable $oCommentable): string
15✔
205
    {
206
        if (!$this->outputFormat->getRenderComments()) {
15✔
207
            return '';
5✔
208
        }
209

210
        $result = '';
10✔
211
        $comments = $oCommentable->getComments();
10✔
212
        $iLastCommentIndex = \count($comments) - 1;
10✔
213

214
        foreach ($comments as $i => $oComment) {
10✔
215
            $result .= $oComment->render($this->outputFormat);
7✔
216
            $result .= $i === $iLastCommentIndex ? $this->spaceAfterBlocks() : $this->spaceBetweenBlocks();
7✔
217
        }
218
        return $result;
10✔
219
    }
220

221
    private function prepareSpace(string $sSpaceString): string
20✔
222
    {
223
        return \str_replace("\n", "\n" . $this->indent(), $sSpaceString);
20✔
224
    }
225

226
    private function indent(): string
20✔
227
    {
228
        return \str_repeat($this->outputFormat->sIndentation, $this->outputFormat->getIndentationLevel());
20✔
229
    }
230
}
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