@@ -27,7 +27,7 @@ abstract class RuleSet implements Renderable, Commentable
27
27
/**
28
28
* @var array<string, Rule>
29
29
*/
30
- private $ aRules = [];
30
+ private $ rules = [];
31
31
32
32
/**
33
33
* @var int<0, max>
@@ -69,9 +69,9 @@ public static function parseRuleSet(ParserState $parserState, RuleSet $ruleSet):
69
69
$ rule = Rule::parse ($ parserState );
70
70
} catch (UnexpectedTokenException $ e ) {
71
71
try {
72
- $ sConsume = $ parserState ->consumeUntil (["\n" , '; ' , '} ' ], true );
72
+ $ consumedText = $ parserState ->consumeUntil (["\n" , '; ' , '} ' ], true );
73
73
// 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 ), '} ' )) {
75
75
$ parserState ->backtrack (1 );
76
76
} else {
77
77
while ($ parserState ->comes ('; ' )) {
@@ -101,33 +101,33 @@ public function getLineNo(): int
101
101
return $ this ->lineNumber ;
102
102
}
103
103
104
- public function addRule (Rule $ rule , ?Rule $ oSibling = null ): void
104
+ public function addRule (Rule $ ruleToAdd , ?Rule $ sibling = null ): void
105
105
{
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 ] = [];
109
109
}
110
110
111
- $ position = \count ($ this ->aRules [ $ sRule ]);
111
+ $ position = \count ($ this ->rules [ $ rule ]);
112
112
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 );
118
118
}
119
119
}
120
- if ($ rule ->getLineNo () === 0 && $ rule ->getColNo () === 0 ) {
120
+ if ($ ruleToAdd ->getLineNo () === 0 && $ ruleToAdd ->getColNo () === 0 ) {
121
121
//this node is added manually, give it the next best line
122
122
$ 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 );
127
127
}
128
128
}
129
129
130
- \array_splice ($ this ->aRules [ $ sRule ], $ position , 0 , [$ rule ]);
130
+ \array_splice ($ this ->rules [ $ rule ], $ position , 0 , [$ ruleToAdd ]);
131
131
}
132
132
133
133
/**
@@ -138,32 +138,32 @@ public function addRule(Rule $rule, ?Rule $oSibling = null): void
138
138
* @example $ruleSet->getRules('font-')
139
139
* //returns an array of all rules either beginning with font- or matching font.
140
140
*
141
- * @param Rule|string|null $mRule
141
+ * @param Rule|string|null $searchPattern
142
142
* Pattern to search for. If null, returns all rules.
143
143
* If the pattern ends with a dash, all rules starting with the pattern are returned
144
144
* as well as one matching the pattern with the dash excluded.
145
145
* Passing a Rule behaves like calling `getRules($mRule->getRule())`.
146
146
*
147
147
* @return array<int, Rule>
148
148
*/
149
- public function getRules ($ mRule = null )
149
+ public function getRules ($ searchPattern = null )
150
150
{
151
- if ($ mRule instanceof Rule) {
152
- $ mRule = $ mRule ->getRule ();
151
+ if ($ searchPattern instanceof Rule) {
152
+ $ searchPattern = $ searchPattern ->getRule ();
153
153
}
154
154
/** @var array<int, Rule> $result */
155
155
$ result = [];
156
- foreach ($ this ->aRules as $ sName => $ aRules ) {
156
+ foreach ($ this ->rules as $ ruleName => $ rule ) {
157
157
// Either no search rule is given or the search rule matches the found rule exactly
158
158
// or the search rule ends in “-” and the found rule starts with the search rule.
159
159
if (
160
- !$ mRule || $ sName === $ mRule
160
+ !$ searchPattern || $ ruleName === $ searchPattern
161
161
|| (
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 ))
164
164
)
165
165
) {
166
- $ result = \array_merge ($ result , $ aRules );
166
+ $ result = \array_merge ($ result , $ rule );
167
167
}
168
168
}
169
169
\usort ($ result , static function (Rule $ first , Rule $ second ): int {
@@ -172,18 +172,19 @@ public function getRules($mRule = null)
172
172
}
173
173
return $ first ->getLineNo () - $ second ->getLineNo ();
174
174
});
175
+
175
176
return $ result ;
176
177
}
177
178
178
179
/**
179
180
* Overrides all the rules of this set.
180
181
*
181
- * @param array<array-key, Rule> $aRules The rules to override with.
182
+ * @param array<array-key, Rule> $rules The rules to override with.
182
183
*/
183
- public function setRules (array $ aRules ): void
184
+ public function setRules (array $ rules ): void
184
185
{
185
- $ this ->aRules = [];
186
- foreach ($ aRules as $ rule ) {
186
+ $ this ->rules = [];
187
+ foreach ($ rules as $ rule ) {
187
188
$ this ->addRule ($ rule );
188
189
}
189
190
}
@@ -196,18 +197,18 @@ public function setRules(array $aRules): void
196
197
* like `{ background-color: green; background-color; rgba(0, 127, 0, 0.7); }` will only yield an associative array
197
198
* containing the rgba-valued rule while `getRules()` would yield an indexed array containing both.
198
199
*
199
- * @param Rule|string|null $mRule $mRule
200
+ * @param Rule|string|null $searchPattern $mRule
200
201
* Pattern to search for. If null, returns all rules. If the pattern ends with a dash,
201
202
* all rules starting with the pattern are returned as well as one matching the pattern with the dash
202
203
* excluded. Passing a Rule behaves like calling `getRules($mRule->getRule())`.
203
204
*
204
205
* @return array<string, Rule>
205
206
*/
206
- public function getRulesAssoc ($ mRule = null )
207
+ public function getRulesAssoc ($ searchPattern = null )
207
208
{
208
209
/** @var array<string, Rule> $result */
209
210
$ result = [];
210
- foreach ($ this ->getRules ($ mRule ) as $ rule ) {
211
+ foreach ($ this ->getRules ($ searchPattern ) as $ rule ) {
211
212
$ result [$ rule ->getRule ()] = $ rule ;
212
213
}
213
214
return $ result ;
@@ -222,34 +223,34 @@ public function getRulesAssoc($mRule = null)
222
223
* Note: this is different from pre-v.2.0 behaviour of PHP-CSS-Parser, where passing a Rule instance would
223
224
* remove all rules with the same name. To get the old behaviour, use `removeRule($rule->getRule())`.
224
225
*
225
- * @param Rule|string|null $mRule
226
+ * @param Rule|string|null $removalPattern
226
227
* pattern to remove. If $mRule is null, all rules are removed. If the pattern ends in a dash,
227
228
* all rules starting with the pattern are removed as well as one matching the pattern with the dash
228
229
* excluded. Passing a Rule behaves matches by identity.
229
230
*/
230
- public function removeRule ($ mRule ): void
231
+ public function removeRule ($ removalPattern ): void
231
232
{
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 ])) {
235
236
return ;
236
237
}
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 ]);
240
241
}
241
242
}
242
243
} else {
243
- foreach ($ this ->aRules as $ sName => $ aRules ) {
244
+ foreach ($ this ->rules as $ ruleName => $ rules ) {
244
245
// Either no search rule is given or the search rule matches the found rule exactly
245
246
// or the search rule ends in “-” and the found rule starts with the search rule or equals it
246
247
// (without the trailing dash).
247
248
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 )))
251
252
) {
252
- unset($ this ->aRules [ $ sName ]);
253
+ unset($ this ->rules [ $ ruleName ]);
253
254
}
254
255
}
255
256
}
@@ -266,27 +267,27 @@ public function __toString(): string
266
267
protected function renderRules (OutputFormat $ outputFormat )
267
268
{
268
269
$ 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 );
275
276
});
276
- if ($ sRendered === null ) {
277
+ if ($ renderedRule === null ) {
277
278
continue ;
278
279
}
279
- if ($ bIsFirst ) {
280
- $ bIsFirst = false ;
281
- $ result .= $ oNextLevel ->spaceBeforeRules ();
280
+ if ($ isFirst ) {
281
+ $ isFirst = false ;
282
+ $ result .= $ nextLevelFormat ->spaceBeforeRules ();
282
283
} else {
283
- $ result .= $ oNextLevel ->spaceBetweenRules ();
284
+ $ result .= $ nextLevelFormat ->spaceBetweenRules ();
284
285
}
285
- $ result .= $ sRendered ;
286
+ $ result .= $ renderedRule ;
286
287
}
287
288
}
288
289
289
- if (!$ bIsFirst ) {
290
+ if (!$ isFirst ) {
290
291
// Had some output
291
292
$ result .= $ outputFormat ->spaceAfterRules ();
292
293
}
0 commit comments