Skip to content

Commit fdbb925

Browse files
authored
[BUGFIX] AddRule before sibling with different property name (#1270)
Part of #974.
1 parent afdca11 commit fdbb925

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ Please also have a look at our
105105

106106
### Fixed
107107

108+
- Insert `Rule` before sibling even with different property name
109+
(in `RuleSet::addRule()`) (#1270)
108110
- Ensure `RuleSet::addRule()` sets non-negative column number when sibling
109111
provided (#1268)
110112
- Set line number when `RuleSet::addRule()` called with only column number set

src/RuleSet/RuleSet.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,25 @@ public function addRule(Rule $ruleToAdd, ?Rule $sibling = null): void
104104
$position = \count($this->rules[$propertyName]);
105105

106106
if ($sibling !== null) {
107+
$siblingIsInSet = false;
107108
$siblingPosition = \array_search($sibling, $this->rules[$propertyName], true);
108109
if ($siblingPosition !== false) {
110+
$siblingIsInSet = true;
109111
$position = $siblingPosition;
112+
} else {
113+
$siblingIsInSet = $this->hasRule($sibling);
114+
if ($siblingIsInSet) {
115+
// Maintain ordering within `$this->rules[$propertyName]`
116+
// by inserting before first `Rule` with a same-or-later position than the sibling.
117+
foreach ($this->rules[$propertyName] as $index => $rule) {
118+
if (self::comparePositionable($rule, $sibling) >= 0) {
119+
$position = $index;
120+
break;
121+
}
122+
}
123+
}
124+
}
125+
if ($siblingIsInSet) {
110126
// Increment column number of all existing rules on same line, starting at sibling
111127
$siblingLineNumber = $sibling->getLineNumber();
112128
$siblingColumnNumber = $sibling->getColumnNumber();
@@ -123,6 +139,7 @@ public function addRule(Rule $ruleToAdd, ?Rule $sibling = null): void
123139
$ruleToAdd->setPosition($siblingLineNumber, $siblingColumnNumber);
124140
}
125141
}
142+
126143
if ($ruleToAdd->getLineNumber() === null) {
127144
//this node is added manually, give it the next best line
128145
$columnNumber = $ruleToAdd->getColumnNumber() ?? 0;
@@ -305,4 +322,15 @@ private static function comparePositionable(Positionable $first, Positionable $s
305322
}
306323
return $first->getLineNo() - $second->getLineNo();
307324
}
325+
326+
private function hasRule(Rule $rule): bool
327+
{
328+
foreach ($this->rules as $rulesForAProperty) {
329+
if (\in_array($rule, $rulesForAProperty, true)) {
330+
return true;
331+
}
332+
}
333+
334+
return false;
335+
}
308336
}

tests/Unit/RuleSet/RuleSetTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,6 @@ public function addRuleWithSiblingInsertsRuleBeforeSibling(
221221
int $siblingIndex,
222222
string $propertyNameToAdd
223223
): void {
224-
self::markTestSkipped('tofix: if property names don\'t match, sibling is ignored');
225-
226224
$ruleToAdd = new Rule($propertyNameToAdd);
227225
$this->setRulesFromPropertyNames($initialPropertyNames);
228226
$sibling = $this->subject->getRules()[$siblingIndex];

0 commit comments

Comments
 (0)