Skip to content

Commit c79eac9

Browse files
committed
[TASK] Stop caching the selector specificity
This avoids additional state that makes it hard to compare selectors for equality.
1 parent 7ef82db commit c79eac9

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

config/phpstan-baseline.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ parameters:
222222
count: 1
223223
path: ../src/Property/Import.php
224224

225+
-
226+
message: '#^Constructor of class Sabberworm\\CSS\\Property\\Selector has an unused parameter \$calculateSpecificity\.$#'
227+
identifier: constructor.unusedParameter
228+
count: 1
229+
path: ../src/Property/Selector.php
230+
225231
-
226232
message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:comments\(\)\.$#'
227233
identifier: method.notFound

src/Property/Selector.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,6 @@ class Selector
6969
*/
7070
private $selector;
7171

72-
/**
73-
* @var int|null
74-
*/
75-
private $specificity;
76-
7772
/**
7873
* @param string $selector
7974
*
@@ -93,9 +88,6 @@ public static function isValid($selector)
9388
public function __construct($selector, $calculateSpecificity = false)
9489
{
9590
$this->setSelector($selector);
96-
if ($calculateSpecificity) {
97-
$this->getSpecificity();
98-
}
9991
}
10092

10193
/**
@@ -112,7 +104,6 @@ public function getSelector()
112104
public function setSelector($selector): void
113105
{
114106
$this->selector = \trim($selector);
115-
$this->specificity = null;
116107
}
117108

118109
/**
@@ -124,19 +115,16 @@ public function __toString(): string
124115
}
125116

126117
/**
127-
* @return int
118+
* @return int<0, max>
128119
*/
129-
public function getSpecificity()
120+
public function getSpecificity(): int
130121
{
131-
if ($this->specificity === null) {
132-
$a = 0;
133-
/// @todo should exclude \# as well as "#"
134-
$aMatches = null;
135-
$b = \substr_count($this->selector, '#');
136-
$c = \preg_match_all(self::NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX, $this->selector, $aMatches);
137-
$d = \preg_match_all(self::ELEMENTS_AND_PSEUDO_ELEMENTS_RX, $this->selector, $aMatches);
138-
$this->specificity = ($a * 1000) + ($b * 100) + ($c * 10) + $d;
139-
}
140-
return $this->specificity;
122+
$a = 0;
123+
/// @todo should exclude \# as well as "#"
124+
$aMatches = null;
125+
$b = \substr_count($this->selector, '#');
126+
$c = \preg_match_all(self::NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX, $this->selector, $aMatches);
127+
$d = \preg_match_all(self::ELEMENTS_AND_PSEUDO_ELEMENTS_RX, $this->selector, $aMatches);
128+
return ($a * 1000) + ($b * 100) + ($c * 10) + $d;
141129
}
142130
}

0 commit comments

Comments
 (0)