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

MyIntervals / PHP-CSS-Parser / 13334861064

14 Feb 2025 06:08PM UTC coverage: 50.421%. Remained the same
13334861064

push

github

web-flow
[CLEANUP] Avoid Hungarian notation for `result` (#924)

Part of #756

Co-authored-by: JakeQZ <jake.github@qzdesign.co.uk>

49 of 100 new or added lines in 13 files covered. (49.0%)

2 existing lines in 2 files now uncovered.

958 of 1900 relevant lines covered (50.42%)

11.51 hits per line

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

71.25
/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 $oFormat;
16

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

22
    /**
23
     * @param string $sName
24
     * @param string|null $sType
25
     */
26
    public function space($sName, $sType = null): string
2✔
27
    {
28
        $sSpaceString = $this->oFormat->get("Space$sName");
2✔
29
        // If $sSpaceString is an array, we have multiple values configured
30
        // depending on the type of object the space applies to
31
        if (\is_array($sSpaceString)) {
2✔
32
            if ($sType !== null && isset($sSpaceString[$sType])) {
×
33
                $sSpaceString = $sSpaceString[$sType];
×
34
            } else {
35
                $sSpaceString = \reset($sSpaceString);
×
36
            }
37
        }
38
        return $this->prepareSpace($sSpaceString);
2✔
39
    }
40

41
    public function spaceAfterRuleName(): string
2✔
42
    {
43
        return $this->space('AfterRuleName');
2✔
44
    }
45

46
    public function spaceBeforeRules(): string
2✔
47
    {
48
        return $this->space('BeforeRules');
2✔
49
    }
50

51
    public function spaceAfterRules(): string
2✔
52
    {
53
        return $this->space('AfterRules');
2✔
54
    }
55

56
    public function spaceBetweenRules(): string
×
57
    {
58
        return $this->space('BetweenRules');
×
59
    }
60

61
    public function spaceBeforeBlocks(): string
2✔
62
    {
63
        return $this->space('BeforeBlocks');
2✔
64
    }
65

66
    public function spaceAfterBlocks(): string
2✔
67
    {
68
        return $this->space('AfterBlocks');
2✔
69
    }
70

71
    public function spaceBetweenBlocks(): string
2✔
72
    {
73
        return $this->space('BetweenBlocks');
2✔
74
    }
75

76
    public function spaceBeforeSelectorSeparator(): string
2✔
77
    {
78
        return $this->space('BeforeSelectorSeparator');
2✔
79
    }
80

81
    /**
82
     * @return string
83
     */
84
    public function spaceAfterSelectorSeparator(): string
2✔
85
    {
86
        return $this->space('AfterSelectorSeparator');
2✔
87
    }
88

89
    /**
90
     * @param string $sSeparator
91
     */
92
    public function spaceBeforeListArgumentSeparator($sSeparator): string
×
93
    {
94
        $spaceForSeparator = $this->oFormat->getSpaceBeforeListArgumentSeparators();
×
95

96
        return $spaceForSeparator[$sSeparator] ?? $this->space('BeforeListArgumentSeparator', $sSeparator);
×
97
    }
98

99
    /**
100
     * @param string $sSeparator
101
     */
102
    public function spaceAfterListArgumentSeparator($sSeparator): string
×
103
    {
104
        $spaceForSeparator = $this->oFormat->getSpaceAfterListArgumentSeparators();
×
105

106
        return $spaceForSeparator[$sSeparator] ?? $this->space('AfterListArgumentSeparator', $sSeparator);
×
107
    }
108

109
    public function spaceBeforeOpeningBrace(): string
2✔
110
    {
111
        return $this->space('BeforeOpeningBrace');
2✔
112
    }
113

114
    /**
115
     * Runs the given code, either swallowing or passing exceptions, depending on the `bIgnoreExceptions` setting.
116
     *
117
     * @param string $cCode the name of the function to call
118
     *
119
     * @return string|null
120
     */
121
    public function safely($cCode)
2✔
122
    {
123
        if ($this->oFormat->get('IgnoreExceptions')) {
2✔
124
            // If output exceptions are ignored, run the code with exception guards
125
            try {
126
                return $cCode();
×
127
            } catch (OutputException $e) {
×
128
                return null;
×
129
            } // Do nothing
130
        } else {
131
            // Run the code as-is
132
            return $cCode();
2✔
133
        }
134
    }
135

136
    /**
137
     * Clone of the `implode` function, but calls `render` with the current output format instead of `__toString()`.
138
     *
139
     * @param array<array-key, Renderable|string> $aValues
140
     * @param bool $bIncreaseLevel
141
     */
142
    public function implode(string $sSeparator, array $aValues, $bIncreaseLevel = false): string
2✔
143
    {
144
        $result = '';
2✔
145
        $oFormat = $this->oFormat;
2✔
146
        if ($bIncreaseLevel) {
2✔
147
            $oFormat = $oFormat->nextLevel();
×
148
        }
149
        $bIsFirst = true;
2✔
150
        foreach ($aValues as $mValue) {
2✔
151
            if ($bIsFirst) {
2✔
152
                $bIsFirst = false;
2✔
153
            } else {
154
                $result .= $sSeparator;
2✔
155
            }
156
            if ($mValue instanceof Renderable) {
2✔
NEW
157
                $result .= $mValue->render($oFormat);
×
158
            } else {
159
                $result .= $mValue;
2✔
160
            }
161
        }
162
        return $result;
2✔
163
    }
164

165
    /**
166
     * @param string $sString
167
     *
168
     * @return string
169
     */
170
    public function removeLastSemicolon($sString)
2✔
171
    {
172
        if ($this->oFormat->get('SemicolonAfterLastRule')) {
2✔
173
            return $sString;
2✔
174
        }
175
        $sString = \explode(';', $sString);
×
176
        if (\count($sString) < 2) {
×
177
            return $sString[0];
×
178
        }
179
        $sLast = \array_pop($sString);
×
180
        $sNextToLast = \array_pop($sString);
×
181
        \array_push($sString, $sNextToLast . $sLast);
×
182
        return \implode(';', $sString);
×
183
    }
184

185
    public function comments(Commentable $oCommentable): string
2✔
186
    {
187
        if (!$this->oFormat->bRenderComments) {
2✔
188
            return '';
1✔
189
        }
190

191
        $result = '';
1✔
192
        $comments = $oCommentable->getComments();
1✔
193
        $iLastCommentIndex = \count($comments) - 1;
1✔
194

195
        foreach ($comments as $i => $oComment) {
1✔
196
            $result .= $oComment->render($this->oFormat);
1✔
197
            $result .= $i === $iLastCommentIndex ? $this->spaceAfterBlocks() : $this->spaceBetweenBlocks();
1✔
198
        }
199
        return $result;
1✔
200
    }
201

202
    /**
203
     * @param string $sSpaceString
204
     */
205
    private function prepareSpace($sSpaceString): string
2✔
206
    {
207
        return \str_replace("\n", "\n" . $this->indent(), $sSpaceString);
2✔
208
    }
209

210
    /**
211
     * @return string
212
     */
213
    private function indent(): string
2✔
214
    {
215
        return \str_repeat($this->oFormat->sIndentation, $this->oFormat->getIndentationLevel());
2✔
216
    }
217
}
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