25
25
abstract class RuleSet implements Renderable, Commentable
26
26
{
27
27
/**
28
- * @var array<string, Rule>
28
+ * the rules in this rule set, using the property or rule name as the key,
29
+ * with potentially multiple rules per property or rule name.
30
+ *
31
+ * @var array<string, array<int<0,max>, Rule>>
29
32
*/
30
33
private $ rules = [];
31
34
@@ -103,31 +106,31 @@ public function getLineNo(): int
103
106
104
107
public function addRule (Rule $ ruleToAdd , ?Rule $ sibling = null ): void
105
108
{
106
- $ sRule = $ ruleToAdd ->getRule ();
107
- if (!isset ($ this ->rules [$ sRule ])) {
108
- $ this ->rules [$ sRule ] = [];
109
+ $ propertyOrRuleName = $ ruleToAdd ->getRule ();
110
+ if (!isset ($ this ->rules [$ propertyOrRuleName ])) {
111
+ $ this ->rules [$ propertyOrRuleName ] = [];
109
112
}
110
113
111
- $ position = \count ($ this ->rules [$ sRule ]);
114
+ $ position = \count ($ this ->rules [$ propertyOrRuleName ]);
112
115
113
116
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 [$ propertyOrRuleName ], true );
118
+ if ($ siblingPosition !== false ) {
119
+ $ position = $ siblingPosition ;
117
120
$ ruleToAdd ->setPosition ($ sibling ->getLineNo (), $ sibling ->getColNo () - 1 );
118
121
}
119
122
}
120
123
if ($ ruleToAdd ->getLineNo () === 0 && $ ruleToAdd ->getColNo () === 0 ) {
121
124
//this node is added manually, give it the next best line
122
125
$ 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 ];
126
129
$ ruleToAdd ->setPosition ($ last ->getLineNo () + 1 , 0 );
127
130
}
128
131
}
129
132
130
- \array_splice ($ this ->rules [$ sRule ], $ position , 0 , [$ ruleToAdd ]);
133
+ \array_splice ($ this ->rules [$ propertyOrRuleName ], $ position , 0 , [$ ruleToAdd ]);
131
134
}
132
135
133
136
/**
@@ -153,14 +156,15 @@ public function getRules($searchPattern = null)
153
156
}
154
157
/** @var array<int, Rule> $result */
155
158
$ result = [];
156
- foreach ($ this ->rules as $ sName => $ rules ) {
159
+ foreach ($ this ->rules as $ propertyOrRuleName => $ rules ) {
157
160
// Either no search rule is given or the search rule matches the found rule exactly
158
161
// or the search rule ends in “-” and the found rule starts with the search rule.
159
162
if (
160
- !$ searchPattern || $ sName === $ searchPattern
163
+ !$ searchPattern || $ propertyOrRuleName === $ searchPattern
161
164
|| (
162
165
\strrpos ($ searchPattern , '- ' ) === \strlen ($ searchPattern ) - \strlen ('- ' )
163
- && (\strpos ($ sName , $ searchPattern ) === 0 || $ sName === \substr ($ searchPattern , 0 , -1 ))
166
+ && (\strpos ($ propertyOrRuleName , $ searchPattern ) === 0
167
+ || $ propertyOrRuleName === \substr ($ searchPattern , 0 , -1 ))
164
168
)
165
169
) {
166
170
$ result = \array_merge ($ result , $ rules );
@@ -172,6 +176,7 @@ public function getRules($searchPattern = null)
172
176
}
173
177
return $ first ->getLineNo () - $ second ->getLineNo ();
174
178
});
179
+
175
180
return $ result ;
176
181
}
177
182
@@ -230,26 +235,27 @@ public function getRulesAssoc($searchPattern = null)
230
235
public function removeRule ($ searchPattern ): void
231
236
{
232
237
if ($ searchPattern instanceof Rule) {
233
- $ sRule = $ searchPattern ->getRule ();
234
- if (!isset ($ this ->rules [$ sRule ])) {
238
+ $ propertyToRemove = $ searchPattern ->getRule ();
239
+ if (!isset ($ this ->rules [$ propertyToRemove ])) {
235
240
return ;
236
241
}
237
- foreach ($ this ->rules [$ sRule ] as $ key => $ rule ) {
242
+ foreach ($ this ->rules [$ propertyToRemove ] as $ key => $ rule ) {
238
243
if ($ rule === $ searchPattern ) {
239
- unset($ this ->rules [$ sRule ][$ key ]);
244
+ unset($ this ->rules [$ propertyToRemove ][$ key ]);
240
245
}
241
246
}
242
247
} else {
243
- foreach ($ this ->rules as $ sName => $ rules ) {
248
+ foreach ($ this ->rules as $ propertyOrRuleName => $ rules ) {
244
249
// Either no search rule is given or the search rule matches the found rule exactly
245
250
// or the search rule ends in “-” and the found rule starts with the search rule or equals it
246
251
// (without the trailing dash).
247
252
if (
248
- !$ searchPattern || $ sName === $ searchPattern
253
+ !$ searchPattern || $ propertyOrRuleName === $ searchPattern
249
254
|| (\strrpos ($ searchPattern , '- ' ) === \strlen ($ searchPattern ) - \strlen ('- ' )
250
- && (\strpos ($ sName , $ searchPattern ) === 0 || $ sName === \substr ($ searchPattern , 0 , -1 )))
255
+ && (\strpos ($ propertyOrRuleName , $ searchPattern ) === 0
256
+ || $ propertyOrRuleName === \substr ($ searchPattern , 0 , -1 )))
251
257
) {
252
- unset($ this ->rules [$ sName ]);
258
+ unset($ this ->rules [$ propertyOrRuleName ]);
253
259
}
254
260
}
255
261
}
0 commit comments