Skip to content

Commit 6c2887e

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 215c199 commit 6c2887e

File tree

1 file changed

+62
-61
lines changed

1 file changed

+62
-61
lines changed

src/RuleSet/RuleSet.php

Lines changed: 62 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ abstract class RuleSet implements Renderable, Commentable
2727
/**
2828
* @var array<string, Rule>
2929
*/
30-
private $aRules = [];
30+
private $rules = [];
3131

3232
/**
3333
* @var int<0, max>
@@ -69,9 +69,9 @@ public static function parseRuleSet(ParserState $parserState, RuleSet $ruleSet):
6969
$rule = Rule::parse($parserState);
7070
} catch (UnexpectedTokenException $e) {
7171
try {
72-
$sConsume = $parserState->consumeUntil(["\n", ';', '}'], true);
72+
$consumedText = $parserState->consumeUntil(["\n", ';', '}'], true);
7373
// We need to “unfind” the matches to the end of the ruleSet as this will be matched later
74-
if ($parserState->streql(\substr($sConsume, -1), '}')) {
74+
if ($parserState->streql(\substr($consumedText, -1), '}')) {
7575
$parserState->backtrack(1);
7676
} else {
7777
while ($parserState->comes(';')) {
@@ -101,33 +101,33 @@ public function getLineNo(): int
101101
return $this->lineNumber;
102102
}
103103

104-
public function addRule(Rule $rule, ?Rule $oSibling = null): void
104+
public function addRule(Rule $ruleToAdd, ?Rule $sibling = null): void
105105
{
106-
$sRule = $rule->getRule();
107-
if (!isset($this->aRules[$sRule])) {
108-
$this->aRules[$sRule] = [];
106+
$rule = $ruleToAdd->getRule();
107+
if (!isset($this->rules[$rule])) {
108+
$this->rules[$rule] = [];
109109
}
110110

111-
$position = \count($this->aRules[$sRule]);
111+
$position = \count($this->rules[$rule]);
112112

113-
if ($oSibling !== null) {
114-
$iSiblingPos = \array_search($oSibling, $this->aRules[$sRule], true);
115-
if ($iSiblingPos !== false) {
116-
$position = $iSiblingPos;
117-
$rule->setPosition($oSibling->getLineNo(), $oSibling->getColNo() - 1);
113+
if ($sibling !== null) {
114+
$siblingPosition = \array_search($sibling, $this->rules[$rule], true);
115+
if ($siblingPosition !== false) {
116+
$position = $siblingPosition;
117+
$ruleToAdd->setPosition($sibling->getLineNo(), $sibling->getColNo() - 1);
118118
}
119119
}
120-
if ($rule->getLineNo() === 0 && $rule->getColNo() === 0) {
120+
if ($ruleToAdd->getLineNo() === 0 && $ruleToAdd->getColNo() === 0) {
121121
//this node is added manually, give it the next best line
122122
$rules = $this->getRules();
123-
$pos = \count($rules);
124-
if ($pos > 0) {
125-
$last = $rules[$pos - 1];
126-
$rule->setPosition($last->getLineNo() + 1, 0);
123+
$rulesCount = \count($rules);
124+
if ($rulesCount > 0) {
125+
$last = $rules[$rulesCount - 1];
126+
$ruleToAdd->setPosition($last->getLineNo() + 1, 0);
127127
}
128128
}
129129

130-
\array_splice($this->aRules[$sRule], $position, 0, [$rule]);
130+
\array_splice($this->rules[$rule], $position, 0, [$ruleToAdd]);
131131
}
132132

133133
/**
@@ -138,32 +138,32 @@ public function addRule(Rule $rule, ?Rule $oSibling = null): void
138138
* @example $ruleSet->getRules('font-')
139139
* //returns an array of all rules either beginning with font- or matching font.
140140
*
141-
* @param Rule|string|null $mRule
141+
* @param Rule|string|null $searchPattern
142142
* Pattern to search for. If null, returns all rules.
143143
* If the pattern ends with a dash, all rules starting with the pattern are returned
144144
* as well as one matching the pattern with the dash excluded.
145145
* Passing a Rule behaves like calling `getRules($mRule->getRule())`.
146146
*
147147
* @return array<int, Rule>
148148
*/
149-
public function getRules($mRule = null)
149+
public function getRules($searchPattern = null)
150150
{
151-
if ($mRule instanceof Rule) {
152-
$mRule = $mRule->getRule();
151+
if ($searchPattern instanceof Rule) {
152+
$searchPattern = $searchPattern->getRule();
153153
}
154154
/** @var array<int, Rule> $result */
155155
$result = [];
156-
foreach ($this->aRules as $sName => $aRules) {
156+
foreach ($this->rules as $ruleName => $rule) {
157157
// Either no search rule is given or the search rule matches the found rule exactly
158158
// or the search rule ends in “-” and the found rule starts with the search rule.
159159
if (
160-
!$mRule || $sName === $mRule
160+
!$searchPattern || $ruleName === $searchPattern
161161
|| (
162-
\strrpos($mRule, '-') === \strlen($mRule) - \strlen('-')
163-
&& (\strpos($sName, $mRule) === 0 || $sName === \substr($mRule, 0, -1))
162+
\strrpos($searchPattern, '-') === \strlen($searchPattern) - \strlen('-')
163+
&& (\strpos($ruleName, $searchPattern) === 0 || $ruleName === \substr($searchPattern, 0, -1))
164164
)
165165
) {
166-
$result = \array_merge($result, $aRules);
166+
$result = \array_merge($result, $rule);
167167
}
168168
}
169169
\usort($result, static function (Rule $first, Rule $second): int {
@@ -172,18 +172,19 @@ public function getRules($mRule = null)
172172
}
173173
return $first->getLineNo() - $second->getLineNo();
174174
});
175+
175176
return $result;
176177
}
177178

178179
/**
179180
* Overrides all the rules of this set.
180181
*
181-
* @param array<array-key, Rule> $aRules The rules to override with.
182+
* @param array<array-key, Rule> $rules The rules to override with.
182183
*/
183-
public function setRules(array $aRules): void
184+
public function setRules(array $rules): void
184185
{
185-
$this->aRules = [];
186-
foreach ($aRules as $rule) {
186+
$this->rules = [];
187+
foreach ($rules as $rule) {
187188
$this->addRule($rule);
188189
}
189190
}
@@ -196,18 +197,18 @@ public function setRules(array $aRules): void
196197
* like `{ background-color: green; background-color; rgba(0, 127, 0, 0.7); }` will only yield an associative array
197198
* containing the rgba-valued rule while `getRules()` would yield an indexed array containing both.
198199
*
199-
* @param Rule|string|null $mRule $mRule
200+
* @param Rule|string|null $searchPattern $mRule
200201
* Pattern to search for. If null, returns all rules. If the pattern ends with a dash,
201202
* all rules starting with the pattern are returned as well as one matching the pattern with the dash
202203
* excluded. Passing a Rule behaves like calling `getRules($mRule->getRule())`.
203204
*
204205
* @return array<string, Rule>
205206
*/
206-
public function getRulesAssoc($mRule = null)
207+
public function getRulesAssoc($searchPattern = null)
207208
{
208209
/** @var array<string, Rule> $result */
209210
$result = [];
210-
foreach ($this->getRules($mRule) as $rule) {
211+
foreach ($this->getRules($searchPattern) as $rule) {
211212
$result[$rule->getRule()] = $rule;
212213
}
213214
return $result;
@@ -222,34 +223,34 @@ public function getRulesAssoc($mRule = null)
222223
* Note: this is different from pre-v.2.0 behaviour of PHP-CSS-Parser, where passing a Rule instance would
223224
* remove all rules with the same name. To get the old behaviour, use `removeRule($rule->getRule())`.
224225
*
225-
* @param Rule|string|null $mRule
226+
* @param Rule|string|null $removalPattern
226227
* pattern to remove. If $mRule is null, all rules are removed. If the pattern ends in a dash,
227228
* all rules starting with the pattern are removed as well as one matching the pattern with the dash
228229
* excluded. Passing a Rule behaves matches by identity.
229230
*/
230-
public function removeRule($mRule): void
231+
public function removeRule($removalPattern): void
231232
{
232-
if ($mRule instanceof Rule) {
233-
$sRule = $mRule->getRule();
234-
if (!isset($this->aRules[$sRule])) {
233+
if ($removalPattern instanceof Rule) {
234+
$removalRule = $removalPattern->getRule();
235+
if (!isset($this->rules[$removalRule])) {
235236
return;
236237
}
237-
foreach ($this->aRules[$sRule] as $key => $rule) {
238-
if ($rule === $mRule) {
239-
unset($this->aRules[$sRule][$key]);
238+
foreach ($this->rules[$removalRule] as $key => $rule) {
239+
if ($rule === $removalPattern) {
240+
unset($this->rules[$removalRule][$key]);
240241
}
241242
}
242243
} else {
243-
foreach ($this->aRules as $sName => $aRules) {
244+
foreach ($this->rules as $ruleName => $rules) {
244245
// Either no search rule is given or the search rule matches the found rule exactly
245246
// or the search rule ends in “-” and the found rule starts with the search rule or equals it
246247
// (without the trailing dash).
247248
if (
248-
!$mRule || $sName === $mRule
249-
|| (\strrpos($mRule, '-') === \strlen($mRule) - \strlen('-')
250-
&& (\strpos($sName, $mRule) === 0 || $sName === \substr($mRule, 0, -1)))
249+
!$removalPattern || $ruleName === $removalPattern
250+
|| (\strrpos($removalPattern, '-') === \strlen($removalPattern) - \strlen('-')
251+
&& (\strpos($ruleName, $removalPattern) === 0 || $ruleName === \substr($removalPattern, 0, -1)))
251252
) {
252-
unset($this->aRules[$sName]);
253+
unset($this->rules[$ruleName]);
253254
}
254255
}
255256
}
@@ -266,27 +267,27 @@ public function __toString(): string
266267
protected function renderRules(OutputFormat $outputFormat)
267268
{
268269
$result = '';
269-
$bIsFirst = true;
270-
$oNextLevel = $outputFormat->nextLevel();
271-
foreach ($this->aRules as $aRules) {
272-
foreach ($aRules as $rule) {
273-
$sRendered = $oNextLevel->safely(static function () use ($rule, $oNextLevel): string {
274-
return $rule->render($oNextLevel);
270+
$isFirst = true;
271+
$nextLevelFormat = $outputFormat->nextLevel();
272+
foreach ($this->rules as $rules) {
273+
foreach ($rules as $rule) {
274+
$renderedRule = $nextLevelFormat->safely(static function () use ($rule, $nextLevelFormat): string {
275+
return $rule->render($nextLevelFormat);
275276
});
276-
if ($sRendered === null) {
277+
if ($renderedRule === null) {
277278
continue;
278279
}
279-
if ($bIsFirst) {
280-
$bIsFirst = false;
281-
$result .= $oNextLevel->spaceBeforeRules();
280+
if ($isFirst) {
281+
$isFirst = false;
282+
$result .= $nextLevelFormat->spaceBeforeRules();
282283
} else {
283-
$result .= $oNextLevel->spaceBetweenRules();
284+
$result .= $nextLevelFormat->spaceBetweenRules();
284285
}
285-
$result .= $sRendered;
286+
$result .= $renderedRule;
286287
}
287288
}
288289

289-
if (!$bIsFirst) {
290+
if (!$isFirst) {
290291
// Had some output
291292
$result .= $outputFormat->spaceAfterRules();
292293
}

0 commit comments

Comments
 (0)