Skip to content

Commit 65c893a

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 19ffd07 commit 65c893a

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

src/RuleSet/RuleSet.php

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
abstract 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 = [];
@@ -103,31 +105,31 @@ public function getLineNo(): int
103105

104106
public function addRule(Rule $ruleToAdd, ?Rule $sibling = null): void
105107
{
106-
$sRule = $ruleToAdd->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

113115
if ($sibling !== null) {
114-
$iSiblingPos = \array_search($sibling, $this->rules[$sRule], true);
115-
if ($iSiblingPos !== false) {
116-
$position = $iSiblingPos;
116+
$siblingPosition = \array_search($sibling, $this->rules[$propertyOrRuleName], true);
117+
if ($siblingPosition !== false) {
118+
$position = $siblingPosition;
117119
$ruleToAdd->setPosition($sibling->getLineNo(), $sibling->getColNo() - 1);
118120
}
119121
}
120122
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];
125+
$rulesCount = \count($rules);
126+
if ($rulesCount > 0) {
127+
$last = $rules[$rulesCount - 1];
126128
$ruleToAdd->setPosition($last->getLineNo() + 1, 0);
127129
}
128130
}
129131

130-
\array_splice($this->rules[$sRule], $position, 0, [$ruleToAdd]);
132+
\array_splice($this->rules[$propertyOrRuleName], $position, 0, [$ruleToAdd]);
131133
}
132134

133135
/**
@@ -153,17 +155,18 @@ public function getRules($searchPattern = null)
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-
!$searchPattern || $sName === $searchPattern
162+
!$searchPattern || $propertyOrRuleName === $searchPattern
161163
|| (
162164
\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))
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($searchPattern = null)
172175
}
173176
return $first->getLineNo() - $second->getLineNo();
174177
});
178+
175179
return $result;
176180
}
177181

@@ -230,26 +234,27 @@ public function getRulesAssoc($searchPattern = null)
230234
public function removeRule($searchPattern): void
231235
{
232236
if ($searchPattern instanceof Rule) {
233-
$sRule = $searchPattern->getRule();
234-
if (!isset($this->rules[$sRule])) {
237+
$propertyToRemove = $searchPattern->getRule();
238+
if (!isset($this->rules[$propertyToRemove])) {
235239
return;
236240
}
237-
foreach ($this->rules[$sRule] as $key => $rule) {
241+
foreach ($this->rules[$propertyToRemove] as $key => $rule) {
238242
if ($rule === $searchPattern) {
239-
unset($this->rules[$sRule][$key]);
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-
!$searchPattern || $sName === $searchPattern
252+
!$searchPattern || $propertyOrRuleName === $searchPattern
249253
|| (\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)))
251256
) {
252-
unset($this->rules[$sName]);
257+
unset($this->rules[$propertyOrRuleName]);
253258
}
254259
}
255260
}

0 commit comments

Comments
 (0)