Skip to content
Merged
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
88 changes: 44 additions & 44 deletions src/OutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,54 +20,54 @@ public function __construct(OutputFormat $outputFormat)
}

/**
* @param non-empty-string $sName
* @param non-empty-string $name
*
* @throws \InvalidArgumentException
*/
public function space(string $sName): string
public function space(string $name): string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$name is not very descriptive, but that is beyond the scope of this PR.

{
switch ($sName) {
switch ($name) {
case 'AfterRuleName':
$sSpaceString = $this->outputFormat->getSpaceAfterRuleName();
$spaceString = $this->outputFormat->getSpaceAfterRuleName();
break;
case 'BeforeRules':
$sSpaceString = $this->outputFormat->getSpaceBeforeRules();
$spaceString = $this->outputFormat->getSpaceBeforeRules();
break;
case 'AfterRules':
$sSpaceString = $this->outputFormat->getSpaceAfterRules();
$spaceString = $this->outputFormat->getSpaceAfterRules();
break;
case 'BetweenRules':
$sSpaceString = $this->outputFormat->getSpaceBetweenRules();
$spaceString = $this->outputFormat->getSpaceBetweenRules();
break;
case 'BeforeBlocks':
$sSpaceString = $this->outputFormat->getSpaceBeforeBlocks();
$spaceString = $this->outputFormat->getSpaceBeforeBlocks();
break;
case 'AfterBlocks':
$sSpaceString = $this->outputFormat->getSpaceAfterBlocks();
$spaceString = $this->outputFormat->getSpaceAfterBlocks();
break;
case 'BetweenBlocks':
$sSpaceString = $this->outputFormat->getSpaceBetweenBlocks();
$spaceString = $this->outputFormat->getSpaceBetweenBlocks();
break;
case 'BeforeSelectorSeparator':
$sSpaceString = $this->outputFormat->getSpaceBeforeSelectorSeparator();
$spaceString = $this->outputFormat->getSpaceBeforeSelectorSeparator();
break;
case 'AfterSelectorSeparator':
$sSpaceString = $this->outputFormat->getSpaceAfterSelectorSeparator();
$spaceString = $this->outputFormat->getSpaceAfterSelectorSeparator();
break;
case 'BeforeOpeningBrace':
$sSpaceString = $this->outputFormat->getSpaceBeforeOpeningBrace();
$spaceString = $this->outputFormat->getSpaceBeforeOpeningBrace();
break;
case 'BeforeListArgumentSeparator':
$sSpaceString = $this->outputFormat->getSpaceBeforeListArgumentSeparator();
$spaceString = $this->outputFormat->getSpaceBeforeListArgumentSeparator();
break;
case 'AfterListArgumentSeparator':
$sSpaceString = $this->outputFormat->getSpaceAfterListArgumentSeparator();
$spaceString = $this->outputFormat->getSpaceAfterListArgumentSeparator();
break;
default:
throw new \InvalidArgumentException("Unknown space type: $sName", 1740049248);
throw new \InvalidArgumentException("Unknown space type: $name", 1740049248);
}

return $this->prepareSpace($sSpaceString);
return $this->prepareSpace($spaceString);
}

public function spaceAfterRuleName(): string
Expand Down Expand Up @@ -143,62 +143,62 @@ public function spaceBeforeOpeningBrace(): string
/**
* Runs the given code, either swallowing or passing exceptions, depending on the `ignoreExceptions` setting.
*/
public function safely(callable $cCode): ?string
public function safely(callable $callable): ?string
{
if ($this->outputFormat->getIgnoreExceptions()) {
// If output exceptions are ignored, run the code with exception guards
try {
return $cCode();
return $callable();
} catch (OutputException $e) {
return null;
} // Do nothing
} else {
// Run the code as-is
return $cCode();
return $callable();
}
}

/**
* Clone of the `implode` function, but calls `render` with the current output format instead of `__toString()`.
*
* @param array<array-key, Renderable|string> $aValues
* @param array<array-key, Renderable|string> $values
*/
public function implode(string $sSeparator, array $aValues, bool $bIncreaseLevel = false): string
public function implode(string $separator, array $values, bool $increaseLevel = false): string
{
$result = '';
$outputFormat = $this->outputFormat;
if ($bIncreaseLevel) {
if ($increaseLevel) {
$outputFormat = $outputFormat->nextLevel();
}
$bIsFirst = true;
foreach ($aValues as $mValue) {
if ($bIsFirst) {
$bIsFirst = false;
$isFirst = true;
foreach ($values as $value) {
if ($isFirst) {
$isFirst = false;
} else {
$result .= $sSeparator;
$result .= $separator;
}
if ($mValue instanceof Renderable) {
$result .= $mValue->render($outputFormat);
if ($value instanceof Renderable) {
$result .= $value->render($outputFormat);
} else {
$result .= $mValue;
$result .= $value;
}
}
return $result;
}

public function removeLastSemicolon(string $sString): string
public function removeLastSemicolon(string $string): string
{
if ($this->outputFormat->getSemicolonAfterLastRule()) {
return $sString;
return $string;
}
$sString = \explode(';', $sString);
if (\count($sString) < 2) {
return $sString[0];
$parts = \explode(';', $string);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also noted the rewriting of the parameter and thought a fresh local variable would be better 👍

if (\count($parts) < 2) {
return $parts[0];
}
$sLast = \array_pop($sString);
$sNextToLast = \array_pop($sString);
\array_push($sString, $sNextToLast . $sLast);
return \implode(';', $sString);
$sLast = \array_pop($parts);
$sNextToLast = \array_pop($parts);
\array_push($parts, $sNextToLast . $sLast);
return \implode(';', $parts);
}

public function comments(Commentable $commentable): string
Expand All @@ -209,18 +209,18 @@ public function comments(Commentable $commentable): string

$result = '';
$comments = $commentable->getComments();
$iLastCommentIndex = \count($comments) - 1;
$lastCommentIndex = \count($comments) - 1;

foreach ($comments as $i => $comment) {
$result .= $comment->render($this->outputFormat);
$result .= $i === $iLastCommentIndex ? $this->spaceAfterBlocks() : $this->spaceBetweenBlocks();
$result .= $i === $lastCommentIndex ? $this->spaceAfterBlocks() : $this->spaceBetweenBlocks();
}
return $result;
}

private function prepareSpace(string $sSpaceString): string
private function prepareSpace(string $spaceString): string
{
return \str_replace("\n", "\n" . $this->indent(), $sSpaceString);
return \str_replace("\n", "\n" . $this->indent(), $spaceString);
}

private function indent(): string
Expand Down