Skip to content

[CLEANUP] Add separate OutputFormat properties for arrays #883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ This project adheres to [Semantic Versioning](https://semver.org/).

### Added

- `OutputFormat` properties for space around specific list separators (#880)

### Changed

### Deprecated

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

### Removed
Expand Down
30 changes: 26 additions & 4 deletions src/OutputFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,39 @@ class OutputFormat
public $sSpaceAfterSelectorSeparator = ' ';

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

/**
* @var string
* Keys are separators (e.g. `,`). Values are the space sequence to insert, or an empty string.
*
* @var array<non-empty-string, string>
*/
public $aSpaceBeforeListArgumentSeparators = [];

/**
* This is what’s inserted after the separator in value lists, by default.
*
* `array` is deprecated in version 8.8.0, and will be removed in version 9.0.0.
* To set the spacing for specific separators, use {@see $aSpaceAfterListArgumentSeparators} instead.
*
* @var string|array<non-empty-string, string>
*/
public $sSpaceAfterListArgumentSeparator = '';

/**
* Keys are separators (e.g. `,`). Values are the space sequence to insert, or an empty string.
*
* @var array<non-empty-string, string>
*/
public $aSpaceAfterListArgumentSeparators = [];

/**
* @var string
*/
Expand Down Expand Up @@ -343,7 +365,7 @@ public static function createPretty()
$format->set('Space*Rules', "\n")
->set('Space*Blocks', "\n")
->setSpaceBetweenBlocks("\n\n")
->set('SpaceAfterListArgumentSeparator', ['default' => '', ',' => ' '])
->set('SpaceAfterListArgumentSeparators', [',' => ' '])
->setRenderComments(true);
return $format;
}
Expand Down
10 changes: 10 additions & 0 deletions src/OutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ public function spaceAfterSelectorSeparator()
*/
public function spaceBeforeListArgumentSeparator($sSeparator)
{
$spaceForSeparator = $this->oFormat->getSpaceBeforeListArgumentSeparators();
if (isset($spaceForSeparator[$sSeparator])) {
return $spaceForSeparator[$sSeparator];
}

return $this->space('BeforeListArgumentSeparator', $sSeparator);
}

Expand All @@ -127,6 +132,11 @@ public function spaceBeforeListArgumentSeparator($sSeparator)
*/
public function spaceAfterListArgumentSeparator($sSeparator)
{
$spaceForSeparator = $this->oFormat->getSpaceAfterListArgumentSeparators();
if (isset($spaceForSeparator[$sSeparator])) {
return $spaceForSeparator[$sSeparator];
}

return $this->space('AfterListArgumentSeparator', $sSeparator);
}

Expand Down
27 changes: 26 additions & 1 deletion tests/OutputFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@ public function spaceAfterListArgumentSeparator()

/**
* @test
*
* @deprecated since version 8.8.0; will be removed in version 9.0.
* Use `setSpaceAfterListArgumentSeparators()` to set different spacing per separator.
*/
public function spaceAfterListArgumentSeparatorComplex()
public function spaceAfterListArgumentSeparatorComplexDeprecated()
{
$this->setUpTestcase();

Expand All @@ -121,6 +124,28 @@ public function spaceAfterListArgumentSeparatorComplex()
);
}

/**
* @test
*/
public function spaceAfterListArgumentSeparatorComplex()
{
$this->setUpTestcase();

self::assertSame(
'.main, .test {font: italic normal bold 16px/1.2 "Helvetica", Verdana, sans-serif;background: white;}'
. "\n@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}",
$this->oDocument->render(
OutputFormat::create()
->setSpaceAfterListArgumentSeparator(' ')
->setSpaceAfterListArgumentSeparators([
',' => "\t",
'/' => '',
' ' => '',
])
)
);
}

/**
* @test
*/
Expand Down