25
25
abstract class RuleSet implements Renderable, Commentable
26
26
{
27
27
/**
28
+ * the rules in this rule set, using the property or rule name as the key
29
+ *
28
30
* @var array<string, Rule>
29
31
*/
30
32
private $ rules = [];
@@ -69,9 +71,9 @@ public static function parseRuleSet(ParserState $parserState, RuleSet $ruleSet):
69
71
$ rule = Rule::parse ($ parserState );
70
72
} catch (UnexpectedTokenException $ e ) {
71
73
try {
72
- $ sConsume = $ parserState ->consumeUntil (["\n" , '; ' , '} ' ], true );
74
+ $ consumedText = $ parserState ->consumeUntil (["\n" , '; ' , '} ' ], true );
73
75
// 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 ), '} ' )) {
75
77
$ parserState ->backtrack (1 );
76
78
} else {
77
79
while ($ parserState ->comes ('; ' )) {
@@ -101,33 +103,33 @@ public function getLineNo(): int
101
103
return $ this ->lineNumber ;
102
104
}
103
105
104
- public function addRule (Rule $ rule , ?Rule $ sibling = null ): void
106
+ public function addRule (Rule $ ruleToAdd , ?Rule $ sibling = null ): void
105
107
{
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 ] = [];
109
111
}
110
112
111
- $ position = \count ($ this ->rules [$ sRule ]);
113
+ $ position = \count ($ this ->rules [$ propertyOrRuleName ]);
112
114
113
115
if ($ sibling !== null ) {
114
- $ iSiblingPos = \array_search ($ sibling , $ this ->rules [$ sRule ], true );
115
- if ($ iSiblingPos !== false ) {
116
- $ position = $ iSiblingPos ;
117
- $ rule ->setPosition ($ sibling ->getLineNo (), $ sibling ->getColNo () - 1 );
116
+ $ siblingPosition = \array_search ($ sibling , $ this ->rules [$ propertyOrRuleName ], true );
117
+ if ($ siblingPosition !== false ) {
118
+ $ position = $ siblingPosition ;
119
+ $ ruleToAdd ->setPosition ($ sibling ->getLineNo (), $ sibling ->getColNo () - 1 );
118
120
}
119
121
}
120
- if ($ rule ->getLineNo () === 0 && $ rule ->getColNo () === 0 ) {
122
+ if ($ ruleToAdd ->getLineNo () === 0 && $ ruleToAdd ->getColNo () === 0 ) {
121
123
//this node is added manually, give it the next best line
122
124
$ 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 );
127
129
}
128
130
}
129
131
130
- \array_splice ($ this ->rules [$ sRule ], $ position , 0 , [$ rule ]);
132
+ \array_splice ($ this ->rules [$ propertyOrRuleName ], $ position , 0 , [$ ruleToAdd ]);
131
133
}
132
134
133
135
/**
@@ -153,17 +155,18 @@ public function getRules($searchPattern = null)
153
155
}
154
156
/** @var array<int, Rule> $result */
155
157
$ result = [];
156
- foreach ($ this ->rules as $ sName => $ rules ) {
158
+ foreach ($ this ->rules as $ propertyOrRuleName => $ rule ) {
157
159
// Either no search rule is given or the search rule matches the found rule exactly
158
160
// or the search rule ends in “-” and the found rule starts with the search rule.
159
161
if (
160
- !$ searchPattern || $ sName === $ searchPattern
162
+ !$ searchPattern || $ propertyOrRuleName === $ searchPattern
161
163
|| (
162
164
\strrpos ($ searchPattern , '- ' ) === \strlen ($ searchPattern ) - \strlen ('- ' )
163
- && (\strpos ($ sName , $ searchPattern ) === 0 || $ sName === \substr ($ searchPattern , 0 , -1 ))
165
+ && (\strpos ($ propertyOrRuleName , $ searchPattern ) === 0
166
+ || $ propertyOrRuleName === \substr ($ searchPattern , 0 , -1 ))
164
167
)
165
168
) {
166
- $ result = \array_merge ($ result , $ rules );
169
+ $ result = \array_merge ($ result , $ rule );
167
170
}
168
171
}
169
172
\usort ($ result , static function (Rule $ first , Rule $ second ): int {
@@ -172,6 +175,7 @@ public function getRules($searchPattern = null)
172
175
}
173
176
return $ first ->getLineNo () - $ second ->getLineNo ();
174
177
});
178
+
175
179
return $ result ;
176
180
}
177
181
@@ -230,26 +234,27 @@ public function getRulesAssoc($searchPattern = null)
230
234
public function removeRule ($ searchPattern ): void
231
235
{
232
236
if ($ searchPattern instanceof Rule) {
233
- $ sRule = $ searchPattern ->getRule ();
234
- if (!isset ($ this ->rules [$ sRule ])) {
237
+ $ propertyToRemove = $ searchPattern ->getRule ();
238
+ if (!isset ($ this ->rules [$ propertyToRemove ])) {
235
239
return ;
236
240
}
237
- foreach ($ this ->rules [$ sRule ] as $ key => $ rule ) {
241
+ foreach ($ this ->rules [$ propertyToRemove ] as $ key => $ rule ) {
238
242
if ($ rule === $ searchPattern ) {
239
- unset($ this ->rules [$ sRule ][$ key ]);
243
+ unset($ this ->rules [$ propertyToRemove ][$ key ]);
240
244
}
241
245
}
242
246
} else {
243
- foreach ($ this ->rules as $ sName => $ rules ) {
247
+ foreach ($ this ->rules as $ propertyOrRuleName => $ rules ) {
244
248
// Either no search rule is given or the search rule matches the found rule exactly
245
249
// or the search rule ends in “-” and the found rule starts with the search rule or equals it
246
250
// (without the trailing dash).
247
251
if (
248
- !$ searchPattern || $ sName === $ searchPattern
252
+ !$ searchPattern || $ propertyOrRuleName === $ searchPattern
249
253
|| (\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 )))
251
256
) {
252
- unset($ this ->rules [$ sName ]);
257
+ unset($ this ->rules [$ propertyOrRuleName ]);
253
258
}
254
259
}
255
260
}
@@ -273,10 +278,10 @@ protected function renderRules(OutputFormat $outputFormat)
273
278
$ nextLevelFormat = $ outputFormat ->nextLevel ();
274
279
foreach ($ this ->rules as $ rules ) {
275
280
foreach ($ rules as $ rule ) {
276
- $ sRendered = $ nextLevelFormat ->safely (static function () use ($ rule , $ nextLevelFormat ): string {
281
+ $ renderedRule = $ nextLevelFormat ->safely (static function () use ($ rule , $ nextLevelFormat ): string {
277
282
return $ rule ->render ($ nextLevelFormat );
278
283
});
279
- if ($ sRendered === null ) {
284
+ if ($ renderedRule === null ) {
280
285
continue ;
281
286
}
282
287
if ($ isFirst ) {
@@ -285,7 +290,7 @@ protected function renderRules(OutputFormat $outputFormat)
285
290
} else {
286
291
$ result .= $ nextLevelFormat ->spaceBetweenRules ();
287
292
}
288
- $ result .= $ sRendered ;
293
+ $ result .= $ renderedRule ;
289
294
}
290
295
}
291
296
0 commit comments