Skip to content

Commit 25b661b

Browse files
committed
[BUGFIX] Fix a type annotation in RuleSet
Also improve some variable names. Part of #756.
1 parent f912e71 commit 25b661b

File tree

2 files changed

+23
-30
lines changed

2 files changed

+23
-30
lines changed

config/phpstan-baseline.neon

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,6 @@ parameters:
330330
count: 1
331331
path: ../src/RuleSet/DeclarationBlock.php
332332

333-
-
334-
message: '#^Argument of an invalid type Sabberworm\\CSS\\Rule\\Rule supplied for foreach, only iterables are supported\.$#'
335-
identifier: foreach.nonIterable
336-
count: 2
337-
path: ../src/RuleSet/RuleSet.php
338-
339333
-
340334
message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:removeLastSemicolon\(\)\.$#'
341335
identifier: method.notFound
@@ -396,12 +390,6 @@ parameters:
396390
count: 1
397391
path: ../src/RuleSet/RuleSet.php
398392

399-
-
400-
message: '#^Use explicit methods over array access on object$#'
401-
identifier: typePerfect.noArrayAccessOnObject
402-
count: 1
403-
path: ../src/RuleSet/RuleSet.php
404-
405393
-
406394
message: '#^Parameters should have "string" types as the only types passed to this method$#'
407395
identifier: typePerfect.narrowPublicClassMethodParamType

src/RuleSet/RuleSet.php

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
abstract class RuleSet implements Renderable, Commentable
2626
{
2727
/**
28-
* @var array<string, Rule>
28+
* the rules in this rule set, using the property or rule name as the key,
29+
* with potentially multiple rules per property or rule name.
30+
*
31+
* @var array<string, array<int<0,max>, Rule>>
2932
*/
3033
private $rules = [];
3134

@@ -103,15 +106,15 @@ public function getLineNo(): int
103106

104107
public function addRule(Rule $ruleToAdd, ?Rule $sibling = null): void
105108
{
106-
$sRule = $ruleToAdd->getRule();
107-
if (!isset($this->rules[$sRule])) {
108-
$this->rules[$sRule] = [];
109+
$propertyOrRuleName = $ruleToAdd->getRule();
110+
if (!isset($this->rules[$propertyOrRuleName])) {
111+
$this->rules[$propertyOrRuleName] = [];
109112
}
110113

111-
$position = \count($this->rules[$sRule]);
114+
$position = \count($this->rules[$propertyOrRuleName]);
112115

113116
if ($sibling !== null) {
114-
$iSiblingPos = \array_search($sibling, $this->rules[$sRule], true);
117+
$iSiblingPos = \array_search($sibling, $this->rules[$propertyOrRuleName], true);
115118
if ($iSiblingPos !== false) {
116119
$position = $iSiblingPos;
117120
$ruleToAdd->setPosition($sibling->getLineNo(), $sibling->getColNo() - 1);
@@ -127,7 +130,7 @@ public function addRule(Rule $ruleToAdd, ?Rule $sibling = null): void
127130
}
128131
}
129132

130-
\array_splice($this->rules[$sRule], $position, 0, [$ruleToAdd]);
133+
\array_splice($this->rules[$propertyOrRuleName], $position, 0, [$ruleToAdd]);
131134
}
132135

133136
/**
@@ -153,14 +156,15 @@ public function getRules($searchPattern = null)
153156
}
154157
/** @var array<int, Rule> $result */
155158
$result = [];
156-
foreach ($this->rules as $sName => $rules) {
159+
foreach ($this->rules as $propertyOrRuleName => $rules) {
157160
// Either no search rule is given or the search rule matches the found rule exactly
158161
// or the search rule ends in “-” and the found rule starts with the search rule.
159162
if (
160-
!$searchPattern || $sName === $searchPattern
163+
!$searchPattern || $propertyOrRuleName === $searchPattern
161164
|| (
162165
\strrpos($searchPattern, '-') === \strlen($searchPattern) - \strlen('-')
163-
&& (\strpos($sName, $searchPattern) === 0 || $sName === \substr($searchPattern, 0, -1))
166+
&& (\strpos($propertyOrRuleName, $searchPattern) === 0
167+
|| $propertyOrRuleName === \substr($searchPattern, 0, -1))
164168
)
165169
) {
166170
$result = \array_merge($result, $rules);
@@ -230,26 +234,27 @@ public function getRulesAssoc($searchPattern = null)
230234
public function removeRule($searchPattern): void
231235
{
232236
if ($searchPattern instanceof Rule) {
233-
$sRule = $searchPattern->getRule();
234-
if (!isset($this->rules[$sRule])) {
237+
$propertyOrRuleName = $searchPattern->getRule();
238+
if (!isset($this->rules[$propertyOrRuleName])) {
235239
return;
236240
}
237-
foreach ($this->rules[$sRule] as $key => $rule) {
241+
foreach ($this->rules[$propertyOrRuleName] as $key => $rule) {
238242
if ($rule === $searchPattern) {
239-
unset($this->rules[$sRule][$key]);
243+
unset($this->rules[$propertyOrRuleName][$key]);
240244
}
241245
}
242246
} else {
243-
foreach ($this->rules as $sName => $rules) {
247+
foreach ($this->rules as $propertyOrRuleName => $rules) {
244248
// Either no search rule is given or the search rule matches the found rule exactly
245249
// or the search rule ends in “-” and the found rule starts with the search rule or equals it
246250
// (without the trailing dash).
247251
if (
248-
!$searchPattern || $sName === $searchPattern
252+
!$searchPattern || $propertyOrRuleName === $searchPattern
249253
|| (\strrpos($searchPattern, '-') === \strlen($searchPattern) - \strlen('-')
250-
&& (\strpos($sName, $searchPattern) === 0 || $sName === \substr($searchPattern, 0, -1)))
254+
&& (\strpos($propertyOrRuleName, $searchPattern) === 0
255+
|| $propertyOrRuleName === \substr($searchPattern, 0, -1)))
251256
) {
252-
unset($this->rules[$sName]);
257+
unset($this->rules[$propertyOrRuleName]);
253258
}
254259
}
255260
}

0 commit comments

Comments
 (0)