Skip to content

Commit 7642f68

Browse files
committed
[BUGFIX] AddRule before sibling with different property name
Part of #974.
1 parent afdca11 commit 7642f68

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
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: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,29 @@ public function addRule(Rule $ruleToAdd, ?Rule $sibling = null): void
104104
$position = \count($this->rules[$propertyName]);
105105

106106
if ($sibling !== null) {
107+
$siblingLineNumber = $sibling->getLineNumber();
108+
$siblingColumnNumber = $sibling->getColumnNumber();
109+
$siblingIsInSet = false;
107110
$siblingPosition = \array_search($sibling, $this->rules[$propertyName], true);
108111
if ($siblingPosition !== false) {
112+
$siblingIsInSet = true;
109113
$position = $siblingPosition;
114+
} elseif ($siblingIsInSet = $this->hasRule($sibling)) {
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 (
119+
$rule->getLineNumber() > $siblingLineNumber ||
120+
$rule->getLineNumber() === $siblingLineNumber &&
121+
$rule->getColumnNumber() >= $siblingColumnNumber
122+
) {
123+
$position = $index;
124+
break;
125+
}
126+
}
127+
}
128+
if ($siblingIsInSet) {
110129
// Increment column number of all existing rules on same line, starting at sibling
111-
$siblingLineNumber = $sibling->getLineNumber();
112-
$siblingColumnNumber = $sibling->getColumnNumber();
113130
foreach ($this->rules as $rulesForAProperty) {
114131
foreach ($rulesForAProperty as $rule) {
115132
if (
@@ -123,6 +140,7 @@ public function addRule(Rule $ruleToAdd, ?Rule $sibling = null): void
123140
$ruleToAdd->setPosition($siblingLineNumber, $siblingColumnNumber);
124141
}
125142
}
143+
126144
if ($ruleToAdd->getLineNumber() === null) {
127145
//this node is added manually, give it the next best line
128146
$columnNumber = $ruleToAdd->getColumnNumber() ?? 0;
@@ -305,4 +323,15 @@ private static function comparePositionable(Positionable $first, Positionable $s
305323
}
306324
return $first->getLineNo() - $second->getLineNo();
307325
}
326+
327+
private function hasRule(Rule $rule): bool
328+
{
329+
foreach ($this->rules as $rulesForAProperty) {
330+
if (\in_array($rule, $rulesForAProperty, true)) {
331+
return true;
332+
}
333+
}
334+
335+
return false;
336+
}
308337
}

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)