From 3e67e25a929f8c7983bba7db3bbb472d371058bd Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Sat, 15 Feb 2025 11:24:56 +0100 Subject: [PATCH 1/2] [TASK] Drop redundant constructor code - use default values for properties instead of setting them in the constructor - drop constructors that are redundant to the constructor of the parent class --- src/CSSList/CSSBlockList.php | 8 -------- src/CSSList/CSSList.php | 6 ++---- src/CSSList/Document.php | 8 -------- src/CSSList/KeyFrame.php | 14 ++------------ src/Parsing/ParserState.php | 3 +-- src/Property/CSSNamespace.php | 3 +-- src/Property/Charset.php | 3 +-- src/Property/Import.php | 3 +-- src/Rule/Rule.php | 12 ++++-------- src/RuleSet/DeclarationBlock.php | 11 +---------- src/RuleSet/RuleSet.php | 6 ++---- src/Value/PrimitiveValue.php | 11 +---------- 12 files changed, 16 insertions(+), 72 deletions(-) diff --git a/src/CSSList/CSSBlockList.php b/src/CSSList/CSSBlockList.php index bb5ff83a..76216279 100644 --- a/src/CSSList/CSSBlockList.php +++ b/src/CSSList/CSSBlockList.php @@ -20,14 +20,6 @@ */ abstract class CSSBlockList extends CSSList { - /** - * @param int<0, max> $lineNumber - */ - public function __construct($lineNumber = 0) - { - parent::__construct($lineNumber); - } - /** * @param array $result */ diff --git a/src/CSSList/CSSList.php b/src/CSSList/CSSList.php index 5e7cf294..e7acc842 100644 --- a/src/CSSList/CSSList.php +++ b/src/CSSList/CSSList.php @@ -38,14 +38,14 @@ abstract class CSSList implements Renderable, Commentable * * @internal since 8.8.0 */ - protected $comments; + protected $comments = []; /** * @var array * * @internal since 8.8.0 */ - protected $contents; + protected $contents = []; /** * @var int<0, max> @@ -59,8 +59,6 @@ abstract class CSSList implements Renderable, Commentable */ public function __construct($lineNumber = 0) { - $this->comments = []; - $this->contents = []; $this->lineNumber = $lineNumber; } diff --git a/src/CSSList/Document.php b/src/CSSList/Document.php index 5f073072..87595caa 100644 --- a/src/CSSList/Document.php +++ b/src/CSSList/Document.php @@ -18,14 +18,6 @@ */ class Document extends CSSBlockList { - /** - * @param int<0, max> $lineNumber - */ - public function __construct($lineNumber = 0) - { - parent::__construct($lineNumber); - } - /** * @throws SourceException * diff --git a/src/CSSList/KeyFrame.php b/src/CSSList/KeyFrame.php index 4ab558c6..b9cb493d 100644 --- a/src/CSSList/KeyFrame.php +++ b/src/CSSList/KeyFrame.php @@ -12,22 +12,12 @@ class KeyFrame extends CSSList implements AtRule /** * @var string|null */ - private $vendorKeyFrame; + private $vendorKeyFrame = null; /** * @var string|null */ - private $animationName; - - /** - * @param int<0, max> $lineNumber - */ - public function __construct($lineNumber = 0) - { - parent::__construct($lineNumber); - $this->vendorKeyFrame = null; - $this->animationName = null; - } + private $animationName = null; /** * @param string $vendorKeyFrame diff --git a/src/Parsing/ParserState.php b/src/Parsing/ParserState.php index 4f98c0ed..10187617 100644 --- a/src/Parsing/ParserState.php +++ b/src/Parsing/ParserState.php @@ -37,7 +37,7 @@ class ParserState /** * @var int */ - private $iCurrentPosition; + private $iCurrentPosition = 0; /** * will only be used if the CSS does not contain an `@charset` declaration @@ -64,7 +64,6 @@ public function __construct($sText, Settings $oParserSettings, $lineNumber = 1) { $this->oParserSettings = $oParserSettings; $this->sText = $sText; - $this->iCurrentPosition = 0; $this->lineNumber = $lineNumber; $this->setCharset($this->oParserSettings->sDefaultCharset); } diff --git a/src/Property/CSSNamespace.php b/src/Property/CSSNamespace.php index eed63782..81f24e34 100644 --- a/src/Property/CSSNamespace.php +++ b/src/Property/CSSNamespace.php @@ -32,7 +32,7 @@ class CSSNamespace implements AtRule * * @internal since 8.8.0 */ - protected $comments; + protected $comments = []; /** * @param string $mUrl @@ -44,7 +44,6 @@ public function __construct($mUrl, $prefix = null, $lineNumber = 0) $this->mUrl = $mUrl; $this->prefix = $prefix; $this->lineNumber = $lineNumber; - $this->comments = []; } /** diff --git a/src/Property/Charset.php b/src/Property/Charset.php index 387f92d1..9addf369 100644 --- a/src/Property/Charset.php +++ b/src/Property/Charset.php @@ -35,7 +35,7 @@ class Charset implements AtRule * * @internal since 8.8.0 */ - protected $comments; + protected $comments = []; /** * @param int<0, max> $lineNumber @@ -44,7 +44,6 @@ public function __construct(CSSString $oCharset, $lineNumber = 0) { $this->oCharset = $oCharset; $this->lineNumber = $lineNumber; - $this->comments = []; } /** diff --git a/src/Property/Import.php b/src/Property/Import.php index 62dcb2b4..bc97d949 100644 --- a/src/Property/Import.php +++ b/src/Property/Import.php @@ -35,7 +35,7 @@ class Import implements AtRule * * @internal since 8.8.0 */ - protected $comments; + protected $comments = []; /** * @param string $mediaQuery @@ -46,7 +46,6 @@ public function __construct(URL $location, $mediaQuery, $lineNumber = 0) $this->location = $location; $this->mediaQuery = $mediaQuery; $this->lineNumber = $lineNumber; - $this->comments = []; } /** diff --git a/src/Rule/Rule.php b/src/Rule/Rule.php index 014a5030..aba2a35f 100644 --- a/src/Rule/Rule.php +++ b/src/Rule/Rule.php @@ -29,17 +29,17 @@ class Rule implements Renderable, Commentable /** * @var RuleValueList|string|null */ - private $mValue; + private $mValue = null; /** * @var bool */ - private $bIsImportant; + private $bIsImportant = false; /** * @var array */ - private $aIeHack; + private $aIeHack = []; /** * @var int @@ -58,7 +58,7 @@ class Rule implements Renderable, Commentable * * @internal since 8.8.0 */ - protected $comments; + protected $comments = []; /** * @param string $sRule @@ -68,12 +68,8 @@ class Rule implements Renderable, Commentable public function __construct($sRule, $lineNumber = 0, $iColNo = 0) { $this->sRule = $sRule; - $this->mValue = null; - $this->bIsImportant = false; - $this->aIeHack = []; $this->lineNumber = $lineNumber; $this->iColNo = $iColNo; - $this->comments = []; } /** diff --git a/src/RuleSet/DeclarationBlock.php b/src/RuleSet/DeclarationBlock.php index 11e1982d..a39eaaa8 100644 --- a/src/RuleSet/DeclarationBlock.php +++ b/src/RuleSet/DeclarationBlock.php @@ -27,16 +27,7 @@ class DeclarationBlock extends RuleSet /** * @var array */ - private $aSelectors; - - /** - * @param int<0, max> $lineNumber - */ - public function __construct($lineNumber = 0) - { - parent::__construct($lineNumber); - $this->aSelectors = []; - } + private $aSelectors = []; /** * @param CSSList|null $list diff --git a/src/RuleSet/RuleSet.php b/src/RuleSet/RuleSet.php index 231f3365..bb40bd23 100644 --- a/src/RuleSet/RuleSet.php +++ b/src/RuleSet/RuleSet.php @@ -27,7 +27,7 @@ abstract class RuleSet implements Renderable, Commentable /** * @var array */ - private $aRules; + private $aRules = []; /** * @var int<0, max> @@ -41,16 +41,14 @@ abstract class RuleSet implements Renderable, Commentable * * @internal since 8.8.0 */ - protected $comments; + protected $comments = []; /** * @param int<0, max> $lineNumber */ public function __construct($lineNumber = 0) { - $this->aRules = []; $this->lineNumber = $lineNumber; - $this->comments = []; } /** diff --git a/src/Value/PrimitiveValue.php b/src/Value/PrimitiveValue.php index 42728c73..f7f94092 100644 --- a/src/Value/PrimitiveValue.php +++ b/src/Value/PrimitiveValue.php @@ -4,13 +4,4 @@ namespace Sabberworm\CSS\Value; -abstract class PrimitiveValue extends Value -{ - /** - * @param int<0, max> $lineNumber - */ - public function __construct($lineNumber = 0) - { - parent::__construct($lineNumber); - } -} +abstract class PrimitiveValue extends Value {} From b7989b0ad9ec1279220079b551f30921b163a543 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Sat, 15 Feb 2025 13:17:58 +0100 Subject: [PATCH 2/2] Avoid redundant default values for properties --- src/CSSList/KeyFrame.php | 4 ++-- src/OutputFormat.php | 4 ++-- src/Rule/Rule.php | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/CSSList/KeyFrame.php b/src/CSSList/KeyFrame.php index b9cb493d..1fce204b 100644 --- a/src/CSSList/KeyFrame.php +++ b/src/CSSList/KeyFrame.php @@ -12,12 +12,12 @@ class KeyFrame extends CSSList implements AtRule /** * @var string|null */ - private $vendorKeyFrame = null; + private $vendorKeyFrame; /** * @var string|null */ - private $animationName = null; + private $animationName; /** * @param string $vendorKeyFrame diff --git a/src/OutputFormat.php b/src/OutputFormat.php index 98f394b0..7f3786be 100644 --- a/src/OutputFormat.php +++ b/src/OutputFormat.php @@ -217,12 +217,12 @@ class OutputFormat /** * @var OutputFormatter|null */ - private $oFormatter = null; + private $oFormatter; /** * @var OutputFormat|null */ - private $oNextLevelFormat = null; + private $oNextLevelFormat; /** * @var int diff --git a/src/Rule/Rule.php b/src/Rule/Rule.php index aba2a35f..a2cbbb86 100644 --- a/src/Rule/Rule.php +++ b/src/Rule/Rule.php @@ -29,7 +29,7 @@ class Rule implements Renderable, Commentable /** * @var RuleValueList|string|null */ - private $mValue = null; + private $mValue; /** * @var bool