From b4ed61cb6d5d258fb89056b39d5a54dd9df4001e Mon Sep 17 00:00:00 2001 From: Jake Hotson Date: Wed, 25 Jun 2025 00:34:43 +0100 Subject: [PATCH] [TASK] Set line number to `null` by default No longer allow or support `0` as a default line or column number. Part of #974 --- CHANGELOG.md | 1 + src/CSSList/AtRuleBlockList.php | 4 ++-- src/CSSList/CSSList.php | 4 ++-- src/Comment/Comment.php | 4 ++-- src/Parsing/SourceException.php | 4 ++-- src/Parsing/UnexpectedTokenException.php | 4 ++-- src/Position/Position.php | 5 ++--- src/Position/Positionable.php | 4 +--- src/Property/CSSNamespace.php | 4 ++-- src/Property/Charset.php | 4 ++-- src/Property/Import.php | 4 ++-- src/Rule/Rule.php | 6 +++--- src/RuleSet/AtRuleSet.php | 4 ++-- src/RuleSet/RuleSet.php | 4 ++-- src/Value/CSSFunction.php | 4 ++-- src/Value/CSSString.php | 4 ++-- src/Value/CalcRuleValueList.php | 4 ++-- src/Value/Color.php | 4 ++-- src/Value/LineName.php | 4 ++-- src/Value/RuleValueList.php | 4 ++-- src/Value/Size.php | 4 ++-- src/Value/URL.php | 4 ++-- src/Value/Value.php | 4 ++-- src/Value/ValueList.php | 4 ++-- tests/UnitDeprecated/Position/PositionTest.php | 12 ------------ 25 files changed, 47 insertions(+), 61 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e317a1e2b..ba74cfa56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ Please also have a look at our ### Changed +- The default line (and column) number is now `null` (not zero) (#1288) - `setPosition()` (in `Rule` and other classes) now has fluent interface, returning itself (#1259) - `RuleSet::removeRule()` now only allows `Rule` as the parameter diff --git a/src/CSSList/AtRuleBlockList.php b/src/CSSList/AtRuleBlockList.php index 8ee62ae4f..e9487cf25 100644 --- a/src/CSSList/AtRuleBlockList.php +++ b/src/CSSList/AtRuleBlockList.php @@ -24,9 +24,9 @@ class AtRuleBlockList extends CSSBlockList implements AtRule /** * @param non-empty-string $type - * @param int<0, max> $lineNumber + * @param int<1, max>|null $lineNumber */ - public function __construct(string $type, string $arguments = '', int $lineNumber = 0) + public function __construct(string $type, string $arguments = '', ?int $lineNumber = null) { parent::__construct($lineNumber); $this->type = $type; diff --git a/src/CSSList/CSSList.php b/src/CSSList/CSSList.php index 1942d8c95..342468138 100644 --- a/src/CSSList/CSSList.php +++ b/src/CSSList/CSSList.php @@ -48,9 +48,9 @@ abstract class CSSList implements CSSElement, CSSListItem, Positionable protected $contents = []; /** - * @param int<0, max> $lineNumber + * @param int<1, max>|null $lineNumber */ - public function __construct(int $lineNumber = 0) + public function __construct(?int $lineNumber = null) { $this->setPosition($lineNumber); } diff --git a/src/Comment/Comment.php b/src/Comment/Comment.php index 7a56624c4..33188988f 100644 --- a/src/Comment/Comment.php +++ b/src/Comment/Comment.php @@ -21,9 +21,9 @@ class Comment implements Positionable, Renderable protected $commentText; /** - * @param int<0, max> $lineNumber + * @param int<1, max>|null $lineNumber */ - public function __construct(string $commentText = '', int $lineNumber = 0) + public function __construct(string $commentText = '', ?int $lineNumber = null) { $this->commentText = $commentText; $this->setPosition($lineNumber); diff --git a/src/Parsing/SourceException.php b/src/Parsing/SourceException.php index ca07cc48d..b96c1878c 100644 --- a/src/Parsing/SourceException.php +++ b/src/Parsing/SourceException.php @@ -12,9 +12,9 @@ class SourceException extends \Exception implements Positionable use Position; /** - * @param int<0, max> $lineNumber + * @param int<1, max>|null $lineNumber */ - public function __construct(string $message, int $lineNumber = 0) + public function __construct(string $message, ?int $lineNumber = null) { $this->setPosition($lineNumber); if ($lineNumber !== 0) { diff --git a/src/Parsing/UnexpectedTokenException.php b/src/Parsing/UnexpectedTokenException.php index 8a16794c1..e485d8228 100644 --- a/src/Parsing/UnexpectedTokenException.php +++ b/src/Parsing/UnexpectedTokenException.php @@ -11,9 +11,9 @@ class UnexpectedTokenException extends SourceException { /** * @param 'literal'|'identifier'|'count'|'expression'|'search'|'custom' $matchType - * @param int<0, max> $lineNumber + * @param int<1, max>|null $lineNumber */ - public function __construct(string $expected, string $found, string $matchType = 'literal', int $lineNumber = 0) + public function __construct(string $expected, string $found, string $matchType = 'literal', ?int $lineNumber = null) { $message = "Token “{$expected}” ({$matchType}) not found. Got “{$found}”."; if ($matchType === 'search') { diff --git a/src/Position/Position.php b/src/Position/Position.php index 0729b05e2..12afc5e10 100644 --- a/src/Position/Position.php +++ b/src/Position/Position.php @@ -48,15 +48,14 @@ public function getColumnNumber(): ?int } /** - * @param int<0, max>|null $lineNumber + * @param int<1, max>|null $lineNumber * @param int<0, max>|null $columnNumber * * @return $this fluent interface */ public function setPosition(?int $lineNumber, ?int $columnNumber = null): Positionable { - // The conditional is for backwards compatibility (backcompat); `0` will not be allowed in future. - $this->lineNumber = $lineNumber !== 0 ? $lineNumber : null; + $this->lineNumber = $lineNumber; $this->columnNumber = $columnNumber; return $this; diff --git a/src/Position/Positionable.php b/src/Position/Positionable.php index 65ba40fbb..c9f829a96 100644 --- a/src/Position/Positionable.php +++ b/src/Position/Positionable.php @@ -29,9 +29,7 @@ public function getLineNo(): int; public function getColumnNumber(): ?int; /** - * @param int<0, max>|null $lineNumber - * Providing zero for this parameter is deprecated in version 8.9.0, and will not be supported from v9.0. - * Use `null` instead when no line number is available. + * @param int<1, max>|null $lineNumber * @param int<0, max>|null $columnNumber * * @return $this fluent interface diff --git a/src/Property/CSSNamespace.php b/src/Property/CSSNamespace.php index e6a2983c5..66164a318 100644 --- a/src/Property/CSSNamespace.php +++ b/src/Property/CSSNamespace.php @@ -31,9 +31,9 @@ class CSSNamespace implements AtRule, Positionable /** * @param CSSString|URL $url - * @param int<0, max> $lineNumber + * @param int<1, max>|null $lineNumber */ - public function __construct($url, ?string $prefix = null, int $lineNumber = 0) + public function __construct($url, ?string $prefix = null, ?int $lineNumber = null) { $this->url = $url; $this->prefix = $prefix; diff --git a/src/Property/Charset.php b/src/Property/Charset.php index 90e7d5fbd..c9488ad1e 100644 --- a/src/Property/Charset.php +++ b/src/Property/Charset.php @@ -29,9 +29,9 @@ class Charset implements AtRule, Positionable private $charset; /** - * @param int<0, max> $lineNumber + * @param int<1, max>|null $lineNumber */ - public function __construct(CSSString $charset, int $lineNumber = 0) + public function __construct(CSSString $charset, ?int $lineNumber = null) { $this->charset = $charset; $this->setPosition($lineNumber); diff --git a/src/Property/Import.php b/src/Property/Import.php index 51c0e4ea8..a9dabe979 100644 --- a/src/Property/Import.php +++ b/src/Property/Import.php @@ -29,9 +29,9 @@ class Import implements AtRule, Positionable private $mediaQuery; /** - * @param int<0, max> $lineNumber + * @param int<1, max>|null $lineNumber */ - public function __construct(URL $location, ?string $mediaQuery, int $lineNumber = 0) + public function __construct(URL $location, ?string $mediaQuery, ?int $lineNumber = null) { $this->location = $location; $this->mediaQuery = $mediaQuery; diff --git a/src/Rule/Rule.php b/src/Rule/Rule.php index 304e66283..2ccfa7c17 100644 --- a/src/Rule/Rule.php +++ b/src/Rule/Rule.php @@ -44,10 +44,10 @@ class Rule implements Commentable, CSSElement, Positionable /** * @param non-empty-string $rule - * @param int<0, max> $lineNumber - * @param int<0, max> $columnNumber + * @param int<1, max>|null $lineNumber + * @param int<0, max>|null $columnNumber */ - public function __construct(string $rule, int $lineNumber = 0, int $columnNumber = 0) + public function __construct(string $rule, ?int $lineNumber = null, ?int $columnNumber = null) { $this->rule = $rule; $this->setPosition($lineNumber, $columnNumber); diff --git a/src/RuleSet/AtRuleSet.php b/src/RuleSet/AtRuleSet.php index 0fda96388..4cd5acc2a 100644 --- a/src/RuleSet/AtRuleSet.php +++ b/src/RuleSet/AtRuleSet.php @@ -27,9 +27,9 @@ class AtRuleSet extends RuleSet implements AtRule /** * @param non-empty-string $type - * @param int<0, max> $lineNumber + * @param int<1, max>|null $lineNumber */ - public function __construct(string $type, string $arguments = '', int $lineNumber = 0) + public function __construct(string $type, string $arguments = '', ?int $lineNumber = null) { parent::__construct($lineNumber); $this->type = $type; diff --git a/src/RuleSet/RuleSet.php b/src/RuleSet/RuleSet.php index 2a9e2846e..9ab5e203e 100644 --- a/src/RuleSet/RuleSet.php +++ b/src/RuleSet/RuleSet.php @@ -41,9 +41,9 @@ abstract class RuleSet implements CSSElement, CSSListItem, Positionable, RuleCon private $rules = []; /** - * @param int<0, max> $lineNumber + * @param int<1, max>|null $lineNumber */ - public function __construct(int $lineNumber = 0) + public function __construct(?int $lineNumber = null) { $this->setPosition($lineNumber); } diff --git a/src/Value/CSSFunction.php b/src/Value/CSSFunction.php index f78f7cb62..86b56d9b1 100644 --- a/src/Value/CSSFunction.php +++ b/src/Value/CSSFunction.php @@ -27,9 +27,9 @@ class CSSFunction extends ValueList * @param non-empty-string $name * @param RuleValueList|array $arguments * @param non-empty-string $separator - * @param int<0, max> $lineNumber + * @param int<1, max>|null $lineNumber */ - public function __construct(string $name, $arguments, string $separator = ',', int $lineNumber = 0) + public function __construct(string $name, $arguments, string $separator = ',', ?int $lineNumber = null) { if ($arguments instanceof RuleValueList) { $separator = $arguments->getListSeparator(); diff --git a/src/Value/CSSString.php b/src/Value/CSSString.php index 52b521e6b..8c4cf6565 100644 --- a/src/Value/CSSString.php +++ b/src/Value/CSSString.php @@ -23,9 +23,9 @@ class CSSString extends PrimitiveValue private $string; /** - * @param int<0, max> $lineNumber + * @param int<1, max>|null $lineNumber */ - public function __construct(string $string, int $lineNumber = 0) + public function __construct(string $string, ?int $lineNumber = null) { $this->string = $string; parent::__construct($lineNumber); diff --git a/src/Value/CalcRuleValueList.php b/src/Value/CalcRuleValueList.php index 3c0f24ce0..f904f12c7 100644 --- a/src/Value/CalcRuleValueList.php +++ b/src/Value/CalcRuleValueList.php @@ -9,9 +9,9 @@ class CalcRuleValueList extends RuleValueList { /** - * @param int<0, max> $lineNumber + * @param int<1, max>|null $lineNumber */ - public function __construct(int $lineNumber = 0) + public function __construct(?int $lineNumber = null) { parent::__construct(',', $lineNumber); } diff --git a/src/Value/Color.php b/src/Value/Color.php index 028ce8561..74e7d1bf7 100644 --- a/src/Value/Color.php +++ b/src/Value/Color.php @@ -17,9 +17,9 @@ class Color extends CSSFunction { /** * @param array $colorValues - * @param int<0, max> $lineNumber + * @param int<1, max>|null $lineNumber */ - public function __construct(array $colorValues, int $lineNumber = 0) + public function __construct(array $colorValues, ?int $lineNumber = null) { parent::__construct(\implode('', \array_keys($colorValues)), $colorValues, ',', $lineNumber); } diff --git a/src/Value/LineName.php b/src/Value/LineName.php index 791f0cc3c..763cc48ea 100644 --- a/src/Value/LineName.php +++ b/src/Value/LineName.php @@ -13,9 +13,9 @@ class LineName extends ValueList { /** * @param array $components - * @param int<0, max> $lineNumber + * @param int<1, max>|null $lineNumber */ - public function __construct(array $components = [], int $lineNumber = 0) + public function __construct(array $components = [], ?int $lineNumber = null) { parent::__construct($components, ' ', $lineNumber); } diff --git a/src/Value/RuleValueList.php b/src/Value/RuleValueList.php index 9884f5cf5..37aa7cd66 100644 --- a/src/Value/RuleValueList.php +++ b/src/Value/RuleValueList.php @@ -13,9 +13,9 @@ class RuleValueList extends ValueList { /** * @param non-empty-string $separator - * @param int<0, max> $lineNumber + * @param int<1, max>|null $lineNumber */ - public function __construct(string $separator = ',', int $lineNumber = 0) + public function __construct(string $separator = ',', ?int $lineNumber = null) { parent::__construct([], $separator, $lineNumber); } diff --git a/src/Value/Size.php b/src/Value/Size.php index a5e15497a..3882cea14 100644 --- a/src/Value/Size.php +++ b/src/Value/Size.php @@ -69,9 +69,9 @@ class Size extends PrimitiveValue /** * @param float|int|string $size - * @param int<0, max> $lineNumber + * @param int<1, max>|null $lineNumber */ - public function __construct($size, ?string $unit = null, bool $isColorComponent = false, int $lineNumber = 0) + public function __construct($size, ?string $unit = null, bool $isColorComponent = false, ?int $lineNumber = null) { parent::__construct($lineNumber); $this->size = (float) $size; diff --git a/src/Value/URL.php b/src/Value/URL.php index 4b4fb4c89..f6e7b974a 100644 --- a/src/Value/URL.php +++ b/src/Value/URL.php @@ -21,9 +21,9 @@ class URL extends PrimitiveValue private $url; /** - * @param int<0, max> $lineNumber + * @param int<1, max>|null $lineNumber */ - public function __construct(CSSString $url, int $lineNumber = 0) + public function __construct(CSSString $url, ?int $lineNumber = null) { parent::__construct($lineNumber); $this->url = $url; diff --git a/src/Value/Value.php b/src/Value/Value.php index e33a2949f..9f4fe1e63 100644 --- a/src/Value/Value.php +++ b/src/Value/Value.php @@ -21,9 +21,9 @@ abstract class Value implements CSSElement, Positionable use Position; /** - * @param int<0, max> $lineNumber + * @param int<1, max>|null $lineNumber */ - public function __construct(int $lineNumber = 0) + public function __construct(?int $lineNumber = null) { $this->setPosition($lineNumber); } diff --git a/src/Value/ValueList.php b/src/Value/ValueList.php index 1d26b74d3..6f85f895e 100644 --- a/src/Value/ValueList.php +++ b/src/Value/ValueList.php @@ -31,9 +31,9 @@ abstract class ValueList extends Value /** * @param array|Value|string $components * @param non-empty-string $separator - * @param int<0, max> $lineNumber + * @param int<1, max>|null $lineNumber */ - public function __construct($components = [], $separator = ',', int $lineNumber = 0) + public function __construct($components = [], $separator = ',', ?int $lineNumber = null) { parent::__construct($lineNumber); if (!\is_array($components)) { diff --git a/tests/UnitDeprecated/Position/PositionTest.php b/tests/UnitDeprecated/Position/PositionTest.php index ef77d4435..ec34c1f1f 100644 --- a/tests/UnitDeprecated/Position/PositionTest.php +++ b/tests/UnitDeprecated/Position/PositionTest.php @@ -77,18 +77,6 @@ public function getLineNoReturnsZeroAfterLineNumberCleared(): void self::assertSame(0, $this->subject->getLineNo()); } - /** - * @test - */ - public function setPositionWithZeroClearsLineNumber(): void - { - $this->subject->setPosition(99); - - $this->subject->setPosition(0); - - self::assertNull($this->subject->getLineNumber()); - } - /** * @test */