Skip to content

Commit b0697d6

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

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: '#^Parameters should have "string" types as the only types passed to this method$#'
227233
identifier: typePerfect.narrowPublicClassMethodParamType

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
*
@@ -91,9 +86,6 @@ public static function isValid($selector)
9186
public function __construct($selector, $calculateSpecificity = false)
9287
{
9388
$this->setSelector($selector);
94-
if ($calculateSpecificity) {
95-
$this->getSpecificity();
96-
}
9789
}
9890

9991
/**
@@ -110,7 +102,6 @@ public function getSelector()
110102
public function setSelector($selector): void
111103
{
112104
$this->selector = \trim($selector);
113-
$this->specificity = null;
114105
}
115106

116107
/**
@@ -122,19 +113,16 @@ public function __toString(): string
122113
}
123114

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

0 commit comments

Comments
 (0)