Skip to content

Commit ae7d10b

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

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-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: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,35 @@ 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+
} else {
115+
foreach ($this->rules as $rulesForAProperty) {
116+
if (\in_array($sibling, $rulesForAProperty, true)) {
117+
$siblingIsInSet = true;
118+
// Maintain ordering within `$this->rules[$propertyName]`
119+
// by inserting before first `Rule` with a same-or-later position than the sibling.
120+
foreach ($this->rules[$propertyName] as $index => $rule) {
121+
if (
122+
$rule->getLineNumber() > $siblingLineNumber ||
123+
$rule->getLineNumber() === $siblingLineNumber &&
124+
$rule->getColumnNumber() >= $siblingColumnNumber
125+
) {
126+
$position = $index;
127+
break;
128+
}
129+
}
130+
break;
131+
}
132+
}
133+
}
134+
if ($siblingIsInSet) {
110135
// Increment column number of all existing rules on same line, starting at sibling
111-
$siblingLineNumber = $sibling->getLineNumber();
112-
$siblingColumnNumber = $sibling->getColumnNumber();
113136
foreach ($this->rules as $rulesForAProperty) {
114137
foreach ($rulesForAProperty as $rule) {
115138
if (
@@ -123,6 +146,7 @@ public function addRule(Rule $ruleToAdd, ?Rule $sibling = null): void
123146
$ruleToAdd->setPosition($siblingLineNumber, $siblingColumnNumber);
124147
}
125148
}
149+
126150
if ($ruleToAdd->getLineNumber() === null) {
127151
//this node is added manually, give it the next best line
128152
$columnNumber = $ruleToAdd->getColumnNumber() ?? 0;

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)