Skip to content

Commit e0e8d4a

Browse files
committed
[CLEANUP] Avoid Hungarian notation in RuleSet
Also improve some names. (This class still in in urgent need of refactoring to improve type safety and make clear naming easier.) Part of #756
1 parent 86aeaa7 commit e0e8d4a

File tree

2 files changed

+29
-35
lines changed

2 files changed

+29
-35
lines changed

config/phpstan-baseline.neon

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -318,12 +318,6 @@ parameters:
318318
count: 1
319319
path: ../src/RuleSet/DeclarationBlock.php
320320

321-
-
322-
message: '#^Argument of an invalid type Sabberworm\\CSS\\Rule\\Rule supplied for foreach, only iterables are supported\.$#'
323-
identifier: foreach.nonIterable
324-
count: 2
325-
path: ../src/RuleSet/RuleSet.php
326-
327321
-
328322
message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:removeLastSemicolon\(\)\.$#'
329323
identifier: method.notFound
@@ -384,12 +378,6 @@ parameters:
384378
count: 1
385379
path: ../src/RuleSet/RuleSet.php
386380

387-
-
388-
message: '#^Use explicit methods over array access on object$#'
389-
identifier: typePerfect.noArrayAccessOnObject
390-
count: 1
391-
path: ../src/RuleSet/RuleSet.php
392-
393381
-
394382
message: '#^Parameters should have "string" types as the only types passed to this method$#'
395383
identifier: typePerfect.narrowPublicClassMethodParamType

src/RuleSet/RuleSet.php

Lines changed: 29 additions & 23 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 name as the key,
29+
* with potentially multiple rules per property name.
30+
*
31+
* @var array<string, array<int<0,max>, Rule>>
2932
*/
3033
private $rules = [];
3134

@@ -103,31 +106,31 @@ 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+
$propertyName = $ruleToAdd->getRule();
110+
if (!isset($this->rules[$propertyName])) {
111+
$this->rules[$propertyName] = [];
109112
}
110113

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

113116
if ($sibling !== null) {
114-
$iSiblingPos = \array_search($sibling, $this->rules[$sRule], true);
115-
if ($iSiblingPos !== false) {
116-
$position = $iSiblingPos;
117+
$siblingPosition = \array_search($sibling, $this->rules[$propertyName], true);
118+
if ($siblingPosition !== false) {
119+
$position = $siblingPosition;
117120
$ruleToAdd->setPosition($sibling->getLineNo(), $sibling->getColNo() - 1);
118121
}
119122
}
120123
if ($ruleToAdd->getLineNo() === 0 && $ruleToAdd->getColNo() === 0) {
121124
//this node is added manually, give it the next best line
122125
$rules = $this->getRules();
123-
$pos = \count($rules);
124-
if ($pos > 0) {
125-
$last = $rules[$pos - 1];
126+
$rulesCount = \count($rules);
127+
if ($rulesCount > 0) {
128+
$last = $rules[$rulesCount - 1];
126129
$ruleToAdd->setPosition($last->getLineNo() + 1, 0);
127130
}
128131
}
129132

130-
\array_splice($this->rules[$sRule], $position, 0, [$ruleToAdd]);
133+
\array_splice($this->rules[$propertyName], $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 $propertyName => $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 || $propertyName === $searchPattern
161164
|| (
162165
\strrpos($searchPattern, '-') === \strlen($searchPattern) - \strlen('-')
163-
&& (\strpos($sName, $searchPattern) === 0 || $sName === \substr($searchPattern, 0, -1))
166+
&& (\strpos($propertyName, $searchPattern) === 0
167+
|| $propertyName === \substr($searchPattern, 0, -1))
164168
)
165169
) {
166170
$result = \array_merge($result, $rules);
@@ -172,6 +176,7 @@ public function getRules($searchPattern = null)
172176
}
173177
return $first->getLineNo() - $second->getLineNo();
174178
});
179+
175180
return $result;
176181
}
177182

@@ -230,26 +235,27 @@ public function getRulesAssoc($searchPattern = null)
230235
public function removeRule($searchPattern): void
231236
{
232237
if ($searchPattern instanceof Rule) {
233-
$sRule = $searchPattern->getRule();
234-
if (!isset($this->rules[$sRule])) {
238+
$propertyToRemove = $searchPattern->getRule();
239+
if (!isset($this->rules[$propertyToRemove])) {
235240
return;
236241
}
237-
foreach ($this->rules[$sRule] as $key => $rule) {
242+
foreach ($this->rules[$propertyToRemove] as $key => $rule) {
238243
if ($rule === $searchPattern) {
239-
unset($this->rules[$sRule][$key]);
244+
unset($this->rules[$propertyToRemove][$key]);
240245
}
241246
}
242247
} else {
243-
foreach ($this->rules as $sName => $rules) {
248+
foreach ($this->rules as $propertyName => $rules) {
244249
// Either no search rule is given or the search rule matches the found rule exactly
245250
// or the search rule ends in “-” and the found rule starts with the search rule or equals it
246251
// (without the trailing dash).
247252
if (
248-
!$searchPattern || $sName === $searchPattern
253+
!$searchPattern || $propertyName === $searchPattern
249254
|| (\strrpos($searchPattern, '-') === \strlen($searchPattern) - \strlen('-')
250-
&& (\strpos($sName, $searchPattern) === 0 || $sName === \substr($searchPattern, 0, -1)))
255+
&& (\strpos($propertyName, $searchPattern) === 0
256+
|| $propertyName === \substr($searchPattern, 0, -1)))
251257
) {
252-
unset($this->rules[$sName]);
258+
unset($this->rules[$propertyName]);
253259
}
254260
}
255261
}

0 commit comments

Comments
 (0)