From 065578379a57b145ff518c8092973cead842eac7 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Tue, 18 Mar 2025 09:28:32 +0100 Subject: [PATCH 1/2] [TASK] Add native type declarations for `CSSNamespace` Part of #811 --- CHANGELOG.md | 3 ++- config/phpstan-baseline.neon | 12 ------------ src/Property/CSSNamespace.php | 27 +++++++++++---------------- 3 files changed, 13 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e39699b8..65250de3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,7 +34,8 @@ Please also have a look at our - Use more native type declarations and strict mode (#641, #772, #774, #778, #804, #841, #873, #875, #891, #922, #923, #933, #958, #964, #967, #1000, #1044, #1134, #1136, #1137, #1139, #1140, #1141, #1145, - #1162, #1163, #1166, #1172, #1174, #1178, #1179, #1181, #1183, #1184, #1186) + #1162, #1163, #1166, #1172, #1174, #1178, #1179, #1181, #1183, #1184, #1186, + #1187) - Add visibility to all class/interface constants (#469) ### Deprecated diff --git a/config/phpstan-baseline.neon b/config/phpstan-baseline.neon index 889cc859..722454ba 100644 --- a/config/phpstan-baseline.neon +++ b/config/phpstan-baseline.neon @@ -30,18 +30,6 @@ parameters: count: 1 path: ../src/CSSList/Document.php - - - message: '#^Cannot call method render\(\) on string\.$#' - identifier: method.nonObject - count: 1 - path: ../src/Property/CSSNamespace.php - - - - message: '#^Only booleans are allowed in an if condition, string given\.$#' - identifier: if.condNotBoolean - count: 1 - path: ../src/Property/CSSNamespace.php - - message: '#^Only booleans are allowed in an if condition, Sabberworm\\CSS\\Value\\RuleValueList\|string\|null given\.$#' identifier: if.condNotBoolean diff --git a/src/Property/CSSNamespace.php b/src/Property/CSSNamespace.php index 75336249..f55554be 100644 --- a/src/Property/CSSNamespace.php +++ b/src/Property/CSSNamespace.php @@ -6,6 +6,8 @@ use Sabberworm\CSS\Comment\Comment; use Sabberworm\CSS\OutputFormat; +use Sabberworm\CSS\Value\CSSString; +use Sabberworm\CSS\Value\URL; /** * `CSSNamespace` represents an `@namespace` rule. @@ -13,7 +15,7 @@ class CSSNamespace implements AtRule { /** - * @var string + * @var CSSString|URL */ private $url; @@ -35,11 +37,10 @@ class CSSNamespace implements AtRule protected $comments = []; /** - * @param string $url - * @param string|null $prefix + * @param CSSString|URL $url * @param int<0, max> $lineNumber */ - public function __construct($url, $prefix = null, int $lineNumber = 0) + public function __construct($url, ?string $prefix = null, int $lineNumber = 0) { $this->url = $url; $this->prefix = $prefix; @@ -61,33 +62,27 @@ public function render(OutputFormat $outputFormat): string } /** - * @return string + * @return CSSString|URL */ public function getUrl() { return $this->url; } - /** - * @return string|null - */ - public function getPrefix() + public function getPrefix(): ?string { return $this->prefix; } /** - * @param string $url + * @param CSSString|URL $url */ public function setUrl($url): void { $this->url = $url; } - /** - * @param string $prefix - */ - public function setPrefix($prefix): void + public function setPrefix(string $prefix): void { $this->prefix = $prefix; } @@ -101,12 +96,12 @@ public function atRuleName(): string } /** - * @return array + * @return array{0: CSSString|URL|string, 1?: CSSString|URL} */ public function atRuleArgs(): array { $result = [$this->url]; - if ($this->prefix) { + if ($this->prefix !== '') { \array_unshift($result, $this->prefix); } return $result; From 2adb289c4f0cd840c58a9a4fd4c9afe874993a8b Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Tue, 18 Mar 2025 11:26:53 +0100 Subject: [PATCH 2/2] Changes suggested in code review --- src/Property/CSSNamespace.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Property/CSSNamespace.php b/src/Property/CSSNamespace.php index f55554be..5b174861 100644 --- a/src/Property/CSSNamespace.php +++ b/src/Property/CSSNamespace.php @@ -20,7 +20,7 @@ class CSSNamespace implements AtRule private $url; /** - * @var string + * @var string|null */ private $prefix; @@ -96,12 +96,12 @@ public function atRuleName(): string } /** - * @return array{0: CSSString|URL|string, 1?: CSSString|URL} + * @return array{0: CSSString|URL|non-empty-string, 1?: CSSString|URL} */ public function atRuleArgs(): array { $result = [$this->url]; - if ($this->prefix !== '') { + if (\is_string($this->prefix) && $this->prefix !== '') { \array_unshift($result, $this->prefix); } return $result;