2525abstract class RuleSet implements Renderable, Commentable
2626{
2727 /**
28+ * the rules in this rule set, using the property or rule name as the key
29+ *
2830 * @var array<string, Rule>
2931 */
3032 private $ rules = [];
@@ -69,9 +71,9 @@ public static function parseRuleSet(ParserState $parserState, RuleSet $ruleSet):
6971 $ rule = Rule::parse ($ parserState );
7072 } catch (UnexpectedTokenException $ e ) {
7173 try {
72- $ sConsume = $ parserState ->consumeUntil (["\n" , '; ' , '} ' ], true );
74+ $ consumedText = $ parserState ->consumeUntil (["\n" , '; ' , '} ' ], true );
7375 // 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 ), '} ' )) {
76+ if ($ parserState ->streql (\substr ($ consumedText , -1 ), '} ' )) {
7577 $ parserState ->backtrack (1 );
7678 } else {
7779 while ($ parserState ->comes ('; ' )) {
@@ -101,33 +103,33 @@ public function getLineNo(): int
101103 return $ this ->lineNumber ;
102104 }
103105
104- public function addRule (Rule $ rule , ?Rule $ oSibling = null ): void
106+ public function addRule (Rule $ ruleToAdd , ?Rule $ sibling = null ): void
105107 {
106- $ sRule = $ rule ->getRule ();
107- if (!isset ($ this ->rules [$ sRule ])) {
108- $ this ->rules [$ sRule ] = [];
108+ $ propertyOrRuleName = $ ruleToAdd ->getRule ();
109+ if (!isset ($ this ->rules [$ propertyOrRuleName ])) {
110+ $ this ->rules [$ propertyOrRuleName ] = [];
109111 }
110112
111- $ position = \count ($ this ->rules [$ sRule ]);
113+ $ position = \count ($ this ->rules [$ propertyOrRuleName ]);
112114
113- if ($ oSibling !== null ) {
114- $ iSiblingPos = \array_search ($ oSibling , $ this ->rules [$ sRule ], true );
115- if ($ iSiblingPos !== false ) {
116- $ position = $ iSiblingPos ;
117- $ rule ->setPosition ($ oSibling ->getLineNo (), $ oSibling ->getColNo () - 1 );
115+ if ($ sibling !== null ) {
116+ $ siblingPosition = \array_search ($ sibling , $ this ->rules [$ propertyOrRuleName ], true );
117+ if ($ siblingPosition !== false ) {
118+ $ position = $ siblingPosition ;
119+ $ ruleToAdd ->setPosition ($ sibling ->getLineNo (), $ sibling ->getColNo () - 1 );
118120 }
119121 }
120- if ($ rule ->getLineNo () === 0 && $ rule ->getColNo () === 0 ) {
122+ if ($ ruleToAdd ->getLineNo () === 0 && $ ruleToAdd ->getColNo () === 0 ) {
121123 //this node is added manually, give it the next best line
122124 $ rules = $ this ->getRules ();
123- $ pos = \count ($ rules );
124- if ($ pos > 0 ) {
125- $ last = $ rules [$ pos - 1 ];
126- $ rule ->setPosition ($ last ->getLineNo () + 1 , 0 );
125+ $ rulesCount = \count ($ rules );
126+ if ($ rulesCount > 0 ) {
127+ $ last = $ rules [$ rulesCount - 1 ];
128+ $ ruleToAdd ->setPosition ($ last ->getLineNo () + 1 , 0 );
127129 }
128130 }
129131
130- \array_splice ($ this ->rules [$ sRule ], $ position , 0 , [$ rule ]);
132+ \array_splice ($ this ->rules [$ propertyOrRuleName ], $ position , 0 , [$ ruleToAdd ]);
131133 }
132134
133135 /**
@@ -138,32 +140,33 @@ public function addRule(Rule $rule, ?Rule $oSibling = null): void
138140 * @example $ruleSet->getRules('font-')
139141 * //returns an array of all rules either beginning with font- or matching font.
140142 *
141- * @param Rule|string|null $mRule
143+ * @param Rule|string|null $searchPattern
142144 * Pattern to search for. If null, returns all rules.
143145 * If the pattern ends with a dash, all rules starting with the pattern are returned
144146 * as well as one matching the pattern with the dash excluded.
145147 * Passing a Rule behaves like calling `getRules($mRule->getRule())`.
146148 *
147149 * @return array<int, Rule>
148150 */
149- public function getRules ($ mRule = null )
151+ public function getRules ($ searchPattern = null )
150152 {
151- if ($ mRule instanceof Rule) {
152- $ mRule = $ mRule ->getRule ();
153+ if ($ searchPattern instanceof Rule) {
154+ $ searchPattern = $ searchPattern ->getRule ();
153155 }
154156 /** @var array<int, Rule> $result */
155157 $ result = [];
156- foreach ($ this ->rules as $ sName => $ rules ) {
158+ foreach ($ this ->rules as $ propertyOrRuleName => $ rule ) {
157159 // Either no search rule is given or the search rule matches the found rule exactly
158160 // or the search rule ends in “-” and the found rule starts with the search rule.
159161 if (
160- !$ mRule || $ sName === $ mRule
162+ !$ searchPattern || $ propertyOrRuleName === $ searchPattern
161163 || (
162- \strrpos ($ mRule , '- ' ) === \strlen ($ mRule ) - \strlen ('- ' )
163- && (\strpos ($ sName , $ mRule ) === 0 || $ sName === \substr ($ mRule , 0 , -1 ))
164+ \strrpos ($ searchPattern , '- ' ) === \strlen ($ searchPattern ) - \strlen ('- ' )
165+ && (\strpos ($ propertyOrRuleName , $ searchPattern ) === 0
166+ || $ propertyOrRuleName === \substr ($ searchPattern , 0 , -1 ))
164167 )
165168 ) {
166- $ result = \array_merge ($ result , $ rules );
169+ $ result = \array_merge ($ result , $ rule );
167170 }
168171 }
169172 \usort ($ result , static function (Rule $ first , Rule $ second ): int {
@@ -172,6 +175,7 @@ public function getRules($mRule = null)
172175 }
173176 return $ first ->getLineNo () - $ second ->getLineNo ();
174177 });
178+
175179 return $ result ;
176180 }
177181
@@ -196,18 +200,18 @@ public function setRules(array $rules): void
196200 * like `{ background-color: green; background-color; rgba(0, 127, 0, 0.7); }` will only yield an associative array
197201 * containing the rgba-valued rule while `getRules()` would yield an indexed array containing both.
198202 *
199- * @param Rule|string|null $mRule $mRule
203+ * @param Rule|string|null $searchPattern
200204 * Pattern to search for. If null, returns all rules. If the pattern ends with a dash,
201205 * all rules starting with the pattern are returned as well as one matching the pattern with the dash
202206 * excluded. Passing a Rule behaves like calling `getRules($mRule->getRule())`.
203207 *
204208 * @return array<string, Rule>
205209 */
206- public function getRulesAssoc ($ mRule = null )
210+ public function getRulesAssoc ($ searchPattern = null )
207211 {
208212 /** @var array<string, Rule> $result */
209213 $ result = [];
210- foreach ($ this ->getRules ($ mRule ) as $ rule ) {
214+ foreach ($ this ->getRules ($ searchPattern ) as $ rule ) {
211215 $ result [$ rule ->getRule ()] = $ rule ;
212216 }
213217 return $ result ;
@@ -222,34 +226,35 @@ public function getRulesAssoc($mRule = null)
222226 * Note: this is different from pre-v.2.0 behaviour of PHP-CSS-Parser, where passing a Rule instance would
223227 * remove all rules with the same name. To get the old behaviour, use `removeRule($rule->getRule())`.
224228 *
225- * @param Rule|string|null $mRule
226- * pattern to remove. If $mRule is null, all rules are removed. If the pattern ends in a dash,
229+ * @param Rule|string|null $searchPattern
230+ * pattern to remove. If null, all rules are removed. If the pattern ends in a dash,
227231 * all rules starting with the pattern are removed as well as one matching the pattern with the dash
228232 * excluded. Passing a Rule behaves matches by identity.
229233 */
230- public function removeRule ($ mRule ): void
234+ public function removeRule ($ searchPattern ): void
231235 {
232- if ($ mRule instanceof Rule) {
233- $ sRule = $ mRule ->getRule ();
234- if (!isset ($ this ->rules [$ sRule ])) {
236+ if ($ searchPattern instanceof Rule) {
237+ $ propertyToRemove = $ searchPattern ->getRule ();
238+ if (!isset ($ this ->rules [$ propertyToRemove ])) {
235239 return ;
236240 }
237- foreach ($ this ->rules [$ sRule ] as $ key => $ rule ) {
238- if ($ rule === $ mRule ) {
239- unset($ this ->rules [$ sRule ][$ key ]);
241+ foreach ($ this ->rules [$ propertyToRemove ] as $ key => $ rule ) {
242+ if ($ rule === $ searchPattern ) {
243+ unset($ this ->rules [$ propertyToRemove ][$ 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- !$ mRule || $ sName === $ mRule
249- || (\strrpos ($ mRule , '- ' ) === \strlen ($ mRule ) - \strlen ('- ' )
250- && (\strpos ($ sName , $ mRule ) === 0 || $ sName === \substr ($ mRule , 0 , -1 )))
252+ !$ searchPattern || $ propertyOrRuleName === $ searchPattern
253+ || (\strrpos ($ searchPattern , '- ' ) === \strlen ($ searchPattern ) - \strlen ('- ' )
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 }
@@ -270,22 +275,22 @@ protected function renderRules(OutputFormat $outputFormat)
270275 {
271276 $ result = '' ;
272277 $ isFirst = true ;
273- $ oNextLevel = $ outputFormat ->nextLevel ();
278+ $ nextLevelFormat = $ outputFormat ->nextLevel ();
274279 foreach ($ this ->rules as $ rules ) {
275280 foreach ($ rules as $ rule ) {
276- $ sRendered = $ oNextLevel ->safely (static function () use ($ rule , $ oNextLevel ): string {
277- return $ rule ->render ($ oNextLevel );
281+ $ renderedRule = $ nextLevelFormat ->safely (static function () use ($ rule , $ nextLevelFormat ): string {
282+ return $ rule ->render ($ nextLevelFormat );
278283 });
279- if ($ sRendered === null ) {
284+ if ($ renderedRule === null ) {
280285 continue ;
281286 }
282287 if ($ isFirst ) {
283288 $ isFirst = false ;
284- $ result .= $ oNextLevel ->spaceBeforeRules ();
289+ $ result .= $ nextLevelFormat ->spaceBeforeRules ();
285290 } else {
286- $ result .= $ oNextLevel ->spaceBetweenRules ();
291+ $ result .= $ nextLevelFormat ->spaceBetweenRules ();
287292 }
288- $ result .= $ sRendered ;
293+ $ result .= $ renderedRule ;
289294 }
290295 }
291296
0 commit comments