From ebbb247b555601c34841d206aef58323665a24b8 Mon Sep 17 00:00:00 2001 From: Jake Hotson Date: Thu, 12 Feb 2026 02:37:48 +0000 Subject: [PATCH 1/2] [TASK] Rename `Rule::$rule` to `Rule::$propertyName` This has become a misnomer. Rules are the outer enclosing blocks. Deprecate the existing setters and getters and add new ones. (The name of the `Rule` class itself is also a misnomer.) Ref: https://developer.mozilla.org/en-US/docs/Web/API/CSS_Object_Model/CSS_Declaration --- CHANGELOG.md | 5 +++++ src/Rule/Rule.php | 48 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60e79bae..106d3717 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ Please also have a look at our ### Changed +- `Rule::setRule()` and `getRule()` are replaced with `setPropertyName()` and + `getPropertyName()` (#1506) - `Selector` is now represented as a sequence of `Selector\Component` objects which can be accessed via `getComponents()`, manipulated individually, or set via `setComponents()` (#1478, #1486, #1487, #1488, #1494, #1496) @@ -29,6 +31,9 @@ Please also have a look at our ### Deprecated +- `Rule::setRule()` and `getRule()` are deprecated and replaced with + `setPropertyName()` and `getPropertyName()` (#1506) + ### Removed ### Fixed diff --git a/src/Rule/Rule.php b/src/Rule/Rule.php index 26e68b23..835e619c 100644 --- a/src/Rule/Rule.php +++ b/src/Rule/Rule.php @@ -20,7 +20,7 @@ use function Safe\preg_match; /** - * `Rule`s just have a string key (the rule) and a 'Value'. + * `Rule`s just have a string key (the property name) and a 'Value'. * * In CSS, `Rule`s are expressed as follows: “key: value[0][0] value[0][1], value[1][0] value[1][1];” */ @@ -32,7 +32,7 @@ class Rule implements Commentable, CSSElement, Positionable /** * @var non-empty-string */ - private $rule; + private $propertyName; /** * @var RuleValueList|string|null @@ -45,13 +45,13 @@ class Rule implements Commentable, CSSElement, Positionable private $isImportant = false; /** - * @param non-empty-string $rule + * @param non-empty-string $propertyName * @param int<1, max>|null $lineNumber * @param int<0, max>|null $columnNumber */ - public function __construct(string $rule, ?int $lineNumber = null, ?int $columnNumber = null) + public function __construct(string $propertyName, ?int $lineNumber = null, ?int $columnNumber = null) { - $this->rule = $rule; + $this->propertyName = $propertyName; $this->setPosition($lineNumber, $columnNumber); } @@ -97,17 +97,17 @@ public static function parse(ParserState $parserState, array $commentsBeforeRule * The first item is the innermost separator (or, put another way, the highest-precedence operator). * The sequence continues to the outermost separator (or lowest-precedence operator). * - * @param non-empty-string $rule + * @param non-empty-string $propertyName * * @return list */ - private static function listDelimiterForRule(string $rule): array + private static function listDelimiterForRule(string $propertyName): array { - if (preg_match('/^font($|-)/', $rule) === 1) { + if (preg_match('/^font($|-)/', $propertyName) === 1) { return [',', '/', ' ']; } - switch ($rule) { + switch ($propertyName) { case 'src': return [' ', ',']; default: @@ -116,19 +116,39 @@ private static function listDelimiterForRule(string $rule): array } /** - * @param non-empty-string $rule + * @param non-empty-string $propertyName */ - public function setRule(string $rule): void + public function setPropertyName(string $propertyName): void { - $this->rule = $rule; + $this->propertyName = $propertyName; } /** * @return non-empty-string */ + public function getPropertyName(): string + { + return $this->propertyName; + } + + /** + * @param non-empty-string $propertyName + * + * @deprecated in v9.2, will be removed in v10.0; use `setPropertyName()` instead. + */ + public function setRule(string $propertyName): void + { + $this->propertyName = $propertyName; + } + + /** + * @return non-empty-string + * + * @deprecated in v9.2, will be removed in v10.0; use `getPropertyName()` instead. + */ public function getRule(): string { - return $this->rule; + return $this->propertyName; } /** @@ -186,7 +206,7 @@ public function getIsImportant(): bool public function render(OutputFormat $outputFormat): string { $formatter = $outputFormat->getFormatter(); - $result = "{$formatter->comments($this)}{$this->rule}:{$formatter->spaceAfterRuleName()}"; + $result = "{$formatter->comments($this)}{$this->propertyName}:{$formatter->spaceAfterRuleName()}"; if ($this->value instanceof Value) { // Can also be a ValueList $result .= $this->value->render($outputFormat); } else { From 87993eaf1499d4a08aedc7bf0b4ea24fe06edb69 Mon Sep 17 00:00:00 2001 From: Jake Hotson Date: Thu, 12 Feb 2026 12:21:04 +0000 Subject: [PATCH 2/2] Rename `listDelimiterForRule()` -> `getDelimitersForPropertyValue()` --- src/Rule/Rule.php | 4 ++-- tests/Functional/Value/ValueTest.php | 2 +- tests/Unit/Value/ValueTest.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Rule/Rule.php b/src/Rule/Rule.php index 835e619c..804a5890 100644 --- a/src/Rule/Rule.php +++ b/src/Rule/Rule.php @@ -75,7 +75,7 @@ public static function parse(ParserState $parserState, array $commentsBeforeRule $parserState->consumeWhiteSpace($comments); $rule->setComments($comments); $parserState->consume(':'); - $value = Value::parseValue($parserState, self::listDelimiterForRule($rule->getRule())); + $value = Value::parseValue($parserState, self::getDelimitersForPropertyValue($rule->getRule())); $rule->setValue($value); $parserState->consumeWhiteSpace(); if ($parserState->comes('!')) { @@ -101,7 +101,7 @@ public static function parse(ParserState $parserState, array $commentsBeforeRule * * @return list */ - private static function listDelimiterForRule(string $propertyName): array + private static function getDelimitersForPropertyValue(string $propertyName): array { if (preg_match('/^font($|-)/', $propertyName) === 1) { return [',', '/', ' ']; diff --git a/tests/Functional/Value/ValueTest.php b/tests/Functional/Value/ValueTest.php index b8dd5b4f..287361a8 100644 --- a/tests/Functional/Value/ValueTest.php +++ b/tests/Functional/Value/ValueTest.php @@ -19,7 +19,7 @@ final class ValueTest extends TestCase /** * the default set of delimiters for parsing most values * - * @see \Sabberworm\CSS\Rule\Rule::listDelimiterForRule + * @see \Sabberworm\CSS\Rule\Rule::getDelimitersForPropertyValue */ private const DEFAULT_DELIMITERS = [',', ' ', '/']; diff --git a/tests/Unit/Value/ValueTest.php b/tests/Unit/Value/ValueTest.php index dfe46917..e3ab12c7 100644 --- a/tests/Unit/Value/ValueTest.php +++ b/tests/Unit/Value/ValueTest.php @@ -21,7 +21,7 @@ final class ValueTest extends TestCase /** * the default set of delimiters for parsing most values * - * @see \Sabberworm\CSS\Rule\Rule::listDelimiterForRule + * @see \Sabberworm\CSS\Rule\Rule::getDelimitersForPropertyValue */ private const DEFAULT_DELIMITERS = [',', ' ', '/'];