Skip to content

Commit a806d0a

Browse files
authored
[CLEANUP] Add separate OutputFormat properties for arrays (#883)
`SpaceBeforeListArgumentSeparators` and `SpaceAfterListArgumentSeparators` are added to allow for different spacing for different separators. `SpaceBeforeListArgumentSeparator` and `SpaceAfterListArgumentSeparator` will specify the default spacing. Setting these as an array is deprecated. This is the backport of #880.
1 parent 35e48cd commit a806d0a

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ This project adheres to [Semantic Versioning](https://semver.org/).
77

88
### Added
99

10+
- `OutputFormat` properties for space around specific list separators (#880)
11+
1012
### Changed
1113

1214
### Deprecated
1315

16+
- `OutputFormat` properties for space around list separators as an array (#880)
1417
- Deprecate `OutputFormat::level()` (#870)
1518

1619
### Removed

src/OutputFormat.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,39 @@ class OutputFormat
9696
public $sSpaceAfterSelectorSeparator = ' ';
9797

9898
/**
99-
* This is what’s printed after the comma of value lists
99+
* This is what’s inserted before the separator in value lists, by default.
100100
*
101-
* @var string
101+
* `array` is deprecated in version 8.8.0, and will be removed in version 9.0.0.
102+
* To set the spacing for specific separators, use {@see $aSpaceBeforeListArgumentSeparators} instead.
103+
*
104+
* @var string|array<non-empty-string, string>
102105
*/
103106
public $sSpaceBeforeListArgumentSeparator = '';
104107

105108
/**
106-
* @var string
109+
* Keys are separators (e.g. `,`). Values are the space sequence to insert, or an empty string.
110+
*
111+
* @var array<non-empty-string, string>
112+
*/
113+
public $aSpaceBeforeListArgumentSeparators = [];
114+
115+
/**
116+
* This is what’s inserted after the separator in value lists, by default.
117+
*
118+
* `array` is deprecated in version 8.8.0, and will be removed in version 9.0.0.
119+
* To set the spacing for specific separators, use {@see $aSpaceAfterListArgumentSeparators} instead.
120+
*
121+
* @var string|array<non-empty-string, string>
107122
*/
108123
public $sSpaceAfterListArgumentSeparator = '';
109124

125+
/**
126+
* Keys are separators (e.g. `,`). Values are the space sequence to insert, or an empty string.
127+
*
128+
* @var array<non-empty-string, string>
129+
*/
130+
public $aSpaceAfterListArgumentSeparators = [];
131+
110132
/**
111133
* @var string
112134
*/
@@ -343,7 +365,7 @@ public static function createPretty()
343365
$format->set('Space*Rules', "\n")
344366
->set('Space*Blocks', "\n")
345367
->setSpaceBetweenBlocks("\n\n")
346-
->set('SpaceAfterListArgumentSeparator', ['default' => '', ',' => ' '])
368+
->set('SpaceAfterListArgumentSeparators', [',' => ' '])
347369
->setRenderComments(true);
348370
return $format;
349371
}

src/OutputFormatter.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ public function spaceAfterSelectorSeparator()
117117
*/
118118
public function spaceBeforeListArgumentSeparator($sSeparator)
119119
{
120+
$spaceForSeparator = $this->oFormat->getSpaceBeforeListArgumentSeparators();
121+
if (isset($spaceForSeparator[$sSeparator])) {
122+
return $spaceForSeparator[$sSeparator];
123+
}
124+
120125
return $this->space('BeforeListArgumentSeparator', $sSeparator);
121126
}
122127

@@ -127,6 +132,11 @@ public function spaceBeforeListArgumentSeparator($sSeparator)
127132
*/
128133
public function spaceAfterListArgumentSeparator($sSeparator)
129134
{
135+
$spaceForSeparator = $this->oFormat->getSpaceAfterListArgumentSeparators();
136+
if (isset($spaceForSeparator[$sSeparator])) {
137+
return $spaceForSeparator[$sSeparator];
138+
}
139+
130140
return $this->space('AfterListArgumentSeparator', $sSeparator);
131141
}
132142

tests/OutputFormatTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,11 @@ public function spaceAfterListArgumentSeparator()
104104

105105
/**
106106
* @test
107+
*
108+
* @deprecated since version 8.8.0; will be removed in version 9.0.
109+
* Use `setSpaceAfterListArgumentSeparators()` to set different spacing per separator.
107110
*/
108-
public function spaceAfterListArgumentSeparatorComplex()
111+
public function spaceAfterListArgumentSeparatorComplexDeprecated()
109112
{
110113
$this->setUpTestcase();
111114

@@ -121,6 +124,28 @@ public function spaceAfterListArgumentSeparatorComplex()
121124
);
122125
}
123126

127+
/**
128+
* @test
129+
*/
130+
public function spaceAfterListArgumentSeparatorComplex()
131+
{
132+
$this->setUpTestcase();
133+
134+
self::assertSame(
135+
'.main, .test {font: italic normal bold 16px/1.2 "Helvetica", Verdana, sans-serif;background: white;}'
136+
. "\n@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}",
137+
$this->oDocument->render(
138+
OutputFormat::create()
139+
->setSpaceAfterListArgumentSeparator(' ')
140+
->setSpaceAfterListArgumentSeparators([
141+
',' => "\t",
142+
'/' => '',
143+
' ' => '',
144+
])
145+
)
146+
);
147+
}
148+
124149
/**
125150
* @test
126151
*/

0 commit comments

Comments
 (0)