Skip to content

Commit d607453

Browse files
authored
[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.
1 parent f28fc1f commit d607453

File tree

1 file changed

+52
-16
lines changed

1 file changed

+52
-16
lines changed

src/OutputFormatter.php

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,54 @@ public function __construct(OutputFormat $outputFormat)
1919
$this->outputFormat = $outputFormat;
2020
}
2121

22-
public function space(string $sName, ?string $sType = null): string
23-
{
24-
$sSpaceString = $this->outputFormat->get("Space$sName");
25-
// If $sSpaceString is an array, we have multiple values configured
26-
// depending on the type of object the space applies to
27-
if (\is_array($sSpaceString)) {
28-
if ($sType !== null && isset($sSpaceString[$sType])) {
29-
$sSpaceString = $sSpaceString[$sType];
30-
} else {
31-
$sSpaceString = \reset($sSpaceString);
32-
}
22+
/**
23+
* @param non-empty-string $sName
24+
*
25+
* @throws \InvalidArgumentException
26+
*/
27+
public function space(string $sName): string
28+
{
29+
switch ($sName) {
30+
case 'AfterRuleName':
31+
$sSpaceString = $this->outputFormat->getSpaceAfterRuleName();
32+
break;
33+
case 'BeforeRules':
34+
$sSpaceString = $this->outputFormat->getSpaceBeforeRules();
35+
break;
36+
case 'AfterRules':
37+
$sSpaceString = $this->outputFormat->getSpaceAfterRules();
38+
break;
39+
case 'BetweenRules':
40+
$sSpaceString = $this->outputFormat->getSpaceBetweenRules();
41+
break;
42+
case 'BeforeBlocks':
43+
$sSpaceString = $this->outputFormat->getSpaceBeforeBlocks();
44+
break;
45+
case 'AfterBlocks':
46+
$sSpaceString = $this->outputFormat->getSpaceAfterBlocks();
47+
break;
48+
case 'BetweenBlocks':
49+
$sSpaceString = $this->outputFormat->getSpaceBetweenBlocks();
50+
break;
51+
case 'BeforeSelectorSeparator':
52+
$sSpaceString = $this->outputFormat->getSpaceBeforeSelectorSeparator();
53+
break;
54+
case 'AfterSelectorSeparator':
55+
$sSpaceString = $this->outputFormat->getSpaceAfterSelectorSeparator();
56+
break;
57+
case 'BeforeOpeningBrace':
58+
$sSpaceString = $this->outputFormat->getSpaceBeforeOpeningBrace();
59+
break;
60+
case 'BeforeListArgumentSeparator':
61+
$sSpaceString = $this->outputFormat->getSpaceBeforeListArgumentSeparator();
62+
break;
63+
case 'AfterListArgumentSeparator':
64+
$sSpaceString = $this->outputFormat->getSpaceAfterListArgumentSeparator();
65+
break;
66+
default:
67+
throw new \InvalidArgumentException("Unknown space type: $sName", 1740049248);
3368
}
69+
3470
return $this->prepareSpace($sSpaceString);
3571
}
3672

@@ -86,7 +122,7 @@ public function spaceBeforeListArgumentSeparator(string $sSeparator): string
86122
{
87123
$spaceForSeparator = $this->outputFormat->getSpaceBeforeListArgumentSeparators();
88124

89-
return $spaceForSeparator[$sSeparator] ?? $this->space('BeforeListArgumentSeparator', $sSeparator);
125+
return $spaceForSeparator[$sSeparator] ?? $this->space('BeforeListArgumentSeparator');
90126
}
91127

92128
/**
@@ -96,7 +132,7 @@ public function spaceAfterListArgumentSeparator(string $sSeparator): string
96132
{
97133
$spaceForSeparator = $this->outputFormat->getSpaceAfterListArgumentSeparators();
98134

99-
return $spaceForSeparator[$sSeparator] ?? $this->space('AfterListArgumentSeparator', $sSeparator);
135+
return $spaceForSeparator[$sSeparator] ?? $this->space('AfterListArgumentSeparator');
100136
}
101137

102138
public function spaceBeforeOpeningBrace(): string
@@ -109,7 +145,7 @@ public function spaceBeforeOpeningBrace(): string
109145
*/
110146
public function safely(callable $cCode): ?string
111147
{
112-
if ($this->outputFormat->get('IgnoreExceptions')) {
148+
if ($this->outputFormat->getIgnoreExceptions()) {
113149
// If output exceptions are ignored, run the code with exception guards
114150
try {
115151
return $cCode();
@@ -152,7 +188,7 @@ public function implode(string $sSeparator, array $aValues, bool $bIncreaseLevel
152188

153189
public function removeLastSemicolon(string $sString): string
154190
{
155-
if ($this->outputFormat->get('SemicolonAfterLastRule')) {
191+
if ($this->outputFormat->getSemicolonAfterLastRule()) {
156192
return $sString;
157193
}
158194
$sString = \explode(';', $sString);
@@ -167,7 +203,7 @@ public function removeLastSemicolon(string $sString): string
167203

168204
public function comments(Commentable $oCommentable): string
169205
{
170-
if (!$this->outputFormat->bRenderComments) {
206+
if (!$this->outputFormat->getRenderComments()) {
171207
return '';
172208
}
173209

0 commit comments

Comments
 (0)