From 456e4d53f1ffe7183fb5eecd78950756299d808b Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Sun, 2 Feb 2025 10:09:12 +0100 Subject: [PATCH 1/3] [FEATURE] Add dedicated property accessors for `OutputFormat` --- config/phpstan-baseline.neon | 7 +- src/OutputFormat.php | 488 ++++++++++++++++++++++++++++++++++- tests/OutputFormatTest.php | 2 +- 3 files changed, 479 insertions(+), 18 deletions(-) diff --git a/config/phpstan-baseline.neon b/config/phpstan-baseline.neon index a7a2f211..846a8dfe 100644 --- a/config/phpstan-baseline.neon +++ b/config/phpstan-baseline.neon @@ -1,8 +1,3 @@ parameters: - ignoreErrors: - - - message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:setIndentation\(\)\.$#' - identifier: method.notFound - count: 2 - path: ../src/OutputFormat.php + ignoreErrors: [] diff --git a/src/OutputFormat.php b/src/OutputFormat.php index 1722b738..5955b28d 100644 --- a/src/OutputFormat.php +++ b/src/OutputFormat.php @@ -4,12 +4,6 @@ namespace Sabberworm\CSS; -/** - * Class OutputFormat - * - * @method OutputFormat setSemicolonAfterLastRule(bool $bSemicolonAfterLastRule) Set whether semicolons are added after - * last rule. - */ class OutputFormat { /** @@ -251,17 +245,489 @@ public function set($aNames, $mValue) */ public function __call(string $sMethodName, array $aArguments) { - if (\strpos($sMethodName, 'set') === 0) { - return $this->set(\substr($sMethodName, 3), $aArguments[0]); - } elseif (\strpos($sMethodName, 'get') === 0) { - return $this->get(\substr($sMethodName, 3)); - } elseif (\method_exists(OutputFormatter::class, $sMethodName)) { + if (\method_exists(OutputFormatter::class, $sMethodName)) { return \call_user_func_array([$this->getFormatter(), $sMethodName], $aArguments); } else { throw new \Exception('Unknown OutputFormat method called: ' . $sMethodName); } } + /** + * @internal + */ + public function getStringQuotingType(): string + { + return $this->sStringQuotingType; + } + + /** + * @return $this fluent interface + */ + public function setStringQuotingType(string $quotingType): self + { + $this->sStringQuotingType = $quotingType; + + return $this; + } + + /** + * @internal + */ + public function getRGBHashNotation(): bool + { + return $this->bRGBHashNotation; + } + + /** + * @return $this fluent interface + */ + public function setRGBHashNotation(bool $rgbHashNotation): self + { + $this->bRGBHashNotation = $rgbHashNotation; + + return $this; + } + + /** + * @internal + */ + public function getSemicolonAfterLastRule(): bool + { + return $this->bSemicolonAfterLastRule; + } + + /** + * @return $this fluent interface + */ + public function setSemicolonAfterLastRule(bool $semicolonAfterLastRule): self + { + $this->bSemicolonAfterLastRule = $semicolonAfterLastRule; + + return $this; + } + + /** + * @internal + */ + public function getSpaceAfterRuleName(): string + { + return $this->sSpaceAfterRuleName; + } + + /** + * @return $this fluent interface + */ + public function setSpaceAfterRuleName(string $whitespace): self + { + $this->sSpaceAfterRuleName = $whitespace; + + return $this; + } + + /** + * @internal + */ + public function getSpaceBeforeRules(): string + { + return $this->sSpaceBeforeRules; + } + + /** + * @return $this fluent interface + */ + public function setSpaceBeforeRules(string $whitespace): self + { + $this->sSpaceBeforeRules = $whitespace; + + return $this; + } + + /** + * @internal + */ + public function getSpaceAfterRules(): string + { + return $this->sSpaceAfterRules; + } + + /** + * @return $this fluent interface + */ + public function setSpaceAfterRules(string $whitespace): self + { + $this->sSpaceAfterRules = $whitespace; + + return $this; + } + + /** + * @internal + */ + public function getSpaceBetweenRules(): string + { + return $this->sSpaceBetweenRules; + } + + /** + * @return $this fluent interface + */ + public function setSpaceBetweenRules(string $whitespace): self + { + $this->sSpaceBetweenRules = $whitespace; + + return $this; + } + + /** + * @internal + */ + public function getSpaceBeforeBlocks(): string + { + return $this->sSpaceBeforeBlocks; + } + + /** + * @return $this fluent interface + */ + public function setSpaceBeforeBlocks(string $whitespace): self + { + $this->sSpaceBeforeBlocks = $whitespace; + + return $this; + } + + /** + * @internal + */ + public function getSpaceAfterBlocks(): string + { + return $this->sSpaceAfterBlocks; + } + + /** + * @return $this fluent interface + */ + public function setSpaceAfterBlocks(string $whitespace): self + { + $this->sSpaceAfterBlocks = $whitespace; + + return $this; + } + + /** + * @internal + */ + public function getSpaceBetweenBlocks(): string + { + return $this->sSpaceBetweenBlocks; + } + + /** + * @return $this fluent interface + */ + public function setSpaceBetweenBlocks(string $whitespace): self + { + $this->sSpaceBetweenBlocks = $whitespace; + + return $this; + } + + /** + * @internal + */ + public function getBeforeAtRuleBlock(): string + { + return $this->sBeforeAtRuleBlock; + } + + /** + * @return $this fluent interface + */ + public function setBeforeAtRuleBlock(string $whitespace): self + { + $this->sBeforeAtRuleBlock = $whitespace; + + return $this; + } + + /** + * @internal + */ + public function getAfterAtRuleBlock(): string + { + return $this->sAfterAtRuleBlock; + } + + /** + * @return $this fluent interface + */ + public function setAfterAtRuleBlock(string $whitespace): self + { + $this->sAfterAtRuleBlock = $whitespace; + + return $this; + } + + /** + * @internal + */ + public function getSpaceBeforeSelectorSeparator(): string + { + return $this->sSpaceBeforeSelectorSeparator; + } + + /** + * @return $this fluent interface + */ + public function setSpaceBeforeSelectorSeparator(string $whitespace): self + { + $this->sSpaceBeforeSelectorSeparator = $whitespace; + + return $this; + } + + /** + * @internal + */ + public function getSpaceAfterSelectorSeparator(): string + { + return $this->sSpaceAfterSelectorSeparator; + } + + /** + * @return $this fluent interface + */ + public function setSpaceAfterSelectorSeparator(string $whitespace): self + { + $this->sSpaceAfterSelectorSeparator = $whitespace; + + return $this; + } + + /** + * @internal + */ + public function getSpaceBeforeListArgumentSeparator(): string + { + return $this->sSpaceBeforeListArgumentSeparator; + } + + /** + * @return $this fluent interface + */ + public function setSpaceBeforeListArgumentSeparator(string $whitespace): self + { + $this->sSpaceBeforeListArgumentSeparator = $whitespace; + + return $this; + } + + /** + * @return array + * + * @internal + */ + public function getSpaceBeforeListArgumentSeparators(): array + { + return $this->aSpaceBeforeListArgumentSeparators; + } + + /** + * @param array $separators + * + * @return $this fluent interface + */ + public function setSpaceBeforeListArgumentSeparators(array $separators): self + { + $this->aSpaceBeforeListArgumentSeparators = $separators; + + return $this; + } + + /** + * @internal + */ + public function getSpaceAfterListArgumentSeparator(): string + { + return $this->sSpaceAfterListArgumentSeparator; + } + + /** + * @return $this fluent interface + */ + public function setSpaceAfterListArgumentSeparator(string $whitespace): self + { + $this->sSpaceAfterListArgumentSeparator = $whitespace; + + return $this; + } + + /** + * @return array + * + * @internal + */ + public function getSpaceAfterListArgumentSeparators(): array + { + return $this->aSpaceAfterListArgumentSeparators; + } + + /** + * @param array $separators + * + * @return $this fluent interface + */ + public function setSpaceAfterListArgumentSeparators(array $separators): self + { + $this->aSpaceAfterListArgumentSeparators = $separators; + + return $this; + } + + /** + * @internal + */ + public function getSpaceBeforeOpeningBrace(): string + { + return $this->sSpaceBeforeOpeningBrace; + } + + /** + * @return $this fluent interface + */ + public function setSpaceBeforeOpeningBrace(string $whitespace): self + { + $this->sSpaceBeforeOpeningBrace = $whitespace; + + return $this; + } + + /** + * @internal + */ + public function getBeforeDeclarationBlock(): string + { + return $this->sBeforeDeclarationBlock; + } + + /** + * @return $this fluent interface + */ + public function setBeforeDeclarationBlock(string $whitespace): self + { + $this->sBeforeDeclarationBlock = $whitespace; + + return $this; + } + + /** + * @internal + */ + public function getAfterDeclarationBlockSelectors(): string + { + return $this->sAfterDeclarationBlockSelectors; + } + + /** + * @return $this fluent interface + */ + public function setAfterDeclarationBlockSelectors(string $whitespace): self + { + $this->sAfterDeclarationBlockSelectors = $whitespace; + + return $this; + } + + /** + * @internal + */ + public function getAfterDeclarationBlock(): string + { + return $this->sAfterDeclarationBlock; + } + + /** + * @return $this fluent interface + */ + public function setAfterDeclarationBlock(string $whitespace): self + { + $this->sAfterDeclarationBlock = $whitespace; + + return $this; + } + + /** + * @internal + */ + public function getIndentation(): string + { + return $this->sIndentation; + } + + /** + * @return $this fluent interface + */ + public function setIndentation(string $sIndentation): self + { + $this->sIndentation = $sIndentation; + + return $this; + } + + /** + * @internal + */ + public function getIgnoreExceptions(): bool + { + return $this->bIgnoreExceptions; + } + + /** + * @return $this fluent interface + */ + public function setIgnoreExceptions(bool $ignoreExceptions): self + { + $this->bIgnoreExceptions = $ignoreExceptions; + + return $this; + } + + /** + * @internal + */ + public function getRenderComments(): bool + { + return $this->bRenderComments; + } + + /** + * @return $this fluent interface + */ + public function setRenderComments(bool $renderComments): self + { + $this->bRenderComments = $renderComments; + + return $this; + } + + /** + * @internal + */ + public function getIndentationLevel(): int + { + return $this->iIndentationLevel; + } + + /** + * @return $this fluent interface + */ + public function setIndentationLevel(int $indentationLevel): self + { + $this->iIndentationLevel = $indentationLevel; + + return $this; + } + /** * @return $this fluent interface */ diff --git a/tests/OutputFormatTest.php b/tests/OutputFormatTest.php index d34b08fc..63388de7 100644 --- a/tests/OutputFormatTest.php +++ b/tests/OutputFormatTest.php @@ -107,7 +107,7 @@ public function spaceAfterListArgumentSeparatorComplexDeprecated(): void 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->document->render(OutputFormat::create()->setSpaceAfterListArgumentSeparator([ + $this->document->render(OutputFormat::create()->setSpaceAfterListArgumentSeparators([ 'default' => ' ', ',' => "\t", '/' => '', From 5d536561fe4dd32306cc4b1a6009ced08402d39e Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Thu, 6 Feb 2025 23:28:52 +0100 Subject: [PATCH 2/3] Changes suggested in code review --- src/OutputFormat.php | 44 +++++++++++++++++++++----------------- tests/OutputFormatTest.php | 2 +- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/OutputFormat.php b/src/OutputFormat.php index 5955b28d..7f851449 100644 --- a/src/OutputFormat.php +++ b/src/OutputFormat.php @@ -443,9 +443,9 @@ public function getBeforeAtRuleBlock(): string /** * @return $this fluent interface */ - public function setBeforeAtRuleBlock(string $whitespace): self + public function setBeforeAtRuleBlock(string $content): self { - $this->sBeforeAtRuleBlock = $whitespace; + $this->sBeforeAtRuleBlock = $content; return $this; } @@ -461,9 +461,9 @@ public function getAfterAtRuleBlock(): string /** * @return $this fluent interface */ - public function setAfterAtRuleBlock(string $whitespace): self + public function setAfterAtRuleBlock(string $content): self { - $this->sAfterAtRuleBlock = $whitespace; + $this->sAfterAtRuleBlock = $content; return $this; } @@ -533,29 +533,33 @@ public function getSpaceBeforeListArgumentSeparators(): array } /** - * @param array $separators + * @param array $separatorSpaces * * @return $this fluent interface */ - public function setSpaceBeforeListArgumentSeparators(array $separators): self + public function setSpaceBeforeListArgumentSeparators(array $separatorSpaces): self { - $this->aSpaceBeforeListArgumentSeparators = $separators; + $this->aSpaceBeforeListArgumentSeparators = $separatorSpaces; return $this; } /** + * @return string|array + * * @internal */ - public function getSpaceAfterListArgumentSeparator(): string + public function getSpaceAfterListArgumentSeparator() { return $this->sSpaceAfterListArgumentSeparator; } /** + * @param string|array $whitespace + * * @return $this fluent interface */ - public function setSpaceAfterListArgumentSeparator(string $whitespace): self + public function setSpaceAfterListArgumentSeparator($whitespace): self { $this->sSpaceAfterListArgumentSeparator = $whitespace; @@ -573,13 +577,13 @@ public function getSpaceAfterListArgumentSeparators(): array } /** - * @param array $separators + * @param array $separatorSpaces * * @return $this fluent interface */ - public function setSpaceAfterListArgumentSeparators(array $separators): self + public function setSpaceAfterListArgumentSeparators(array $separatorSpaces): self { - $this->aSpaceAfterListArgumentSeparators = $separators; + $this->aSpaceAfterListArgumentSeparators = $separatorSpaces; return $this; } @@ -613,9 +617,9 @@ public function getBeforeDeclarationBlock(): string /** * @return $this fluent interface */ - public function setBeforeDeclarationBlock(string $whitespace): self + public function setBeforeDeclarationBlock(string $content): self { - $this->sBeforeDeclarationBlock = $whitespace; + $this->sBeforeDeclarationBlock = $content; return $this; } @@ -631,9 +635,9 @@ public function getAfterDeclarationBlockSelectors(): string /** * @return $this fluent interface */ - public function setAfterDeclarationBlockSelectors(string $whitespace): self + public function setAfterDeclarationBlockSelectors(string $content): self { - $this->sAfterDeclarationBlockSelectors = $whitespace; + $this->sAfterDeclarationBlockSelectors = $content; return $this; } @@ -649,9 +653,9 @@ public function getAfterDeclarationBlock(): string /** * @return $this fluent interface */ - public function setAfterDeclarationBlock(string $whitespace): self + public function setAfterDeclarationBlock(string $content): self { - $this->sAfterDeclarationBlock = $whitespace; + $this->sAfterDeclarationBlock = $content; return $this; } @@ -667,9 +671,9 @@ public function getIndentation(): string /** * @return $this fluent interface */ - public function setIndentation(string $sIndentation): self + public function setIndentation(string $indentation): self { - $this->sIndentation = $sIndentation; + $this->sIndentation = $indentation; return $this; } diff --git a/tests/OutputFormatTest.php b/tests/OutputFormatTest.php index 63388de7..d34b08fc 100644 --- a/tests/OutputFormatTest.php +++ b/tests/OutputFormatTest.php @@ -107,7 +107,7 @@ public function spaceAfterListArgumentSeparatorComplexDeprecated(): void 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->document->render(OutputFormat::create()->setSpaceAfterListArgumentSeparators([ + $this->document->render(OutputFormat::create()->setSpaceAfterListArgumentSeparator([ 'default' => ' ', ',' => "\t", '/' => '', From ec1aa5afa0fb953a501b18c7d359a3dec847f2e3 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Thu, 6 Feb 2025 23:36:01 +0100 Subject: [PATCH 3/3] Drop `setIndentationLevel() again` --- src/OutputFormat.php | 10 ---------- tests/Unit/OutputFormatTest.php | 22 +--------------------- 2 files changed, 1 insertion(+), 31 deletions(-) diff --git a/src/OutputFormat.php b/src/OutputFormat.php index 7f851449..979f600a 100644 --- a/src/OutputFormat.php +++ b/src/OutputFormat.php @@ -722,16 +722,6 @@ public function getIndentationLevel(): int return $this->iIndentationLevel; } - /** - * @return $this fluent interface - */ - public function setIndentationLevel(int $indentationLevel): self - { - $this->iIndentationLevel = $indentationLevel; - - return $this; - } - /** * @return $this fluent interface */ diff --git a/tests/Unit/OutputFormatTest.php b/tests/Unit/OutputFormatTest.php index 2e5abce9..79ff2712 100644 --- a/tests/Unit/OutputFormatTest.php +++ b/tests/Unit/OutputFormatTest.php @@ -721,25 +721,6 @@ public function getIndentationLevelInitiallyReturnsZero(): void self::assertSame(0, $this->subject->getIndentationLevel()); } - /** - * @test - */ - public function setIndentationLevelSetsIndentationLevel(): void - { - $value = 4; - $this->subject->setIndentationLevel($value); - - self::assertSame($value, $this->subject->getIndentationLevel()); - } - - /** - * @test - */ - public function setIndentationLevelProvidesFluentInterface(): void - { - self::assertSame($this->subject, $this->subject->setIndentationLevel(4)); - } - /** * @test */ @@ -850,8 +831,7 @@ public function nextLevelReturnsDifferentInstance(): void */ public function nextLevelReturnsInstanceWithIndentationLevelIncreasedByOne(): void { - $originalIndentationLevel = 2; - $this->subject->setIndentationLevel($originalIndentationLevel); + $originalIndentationLevel = $this->subject->getIndentationLevel(); self::assertSame($originalIndentationLevel + 1, $this->subject->nextLevel()->getIndentationLevel()); }