Skip to content

Commit a51fc82

Browse files
authored
[BUGFIX] Ensure non-negative column number in RuleSet (#1268)
When inserting a `Rule` before a sibling, increment the column number of other `Rule`s, instead of assigning a lower column number. Part of #974.
1 parent a2239e0 commit a51fc82

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
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+
- Ensure `RuleSet::addRule()` sets non-negative column number when sibling
109+
provided (#1268)
108110
- Set line number when `RuleSet::addRule()` called with only column number set
109111
(#1265)
110112
- Ensure first rule added with `RuleSet::addRule()` has valid position (#1262)

src/RuleSet/RuleSet.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,20 @@ public function addRule(Rule $ruleToAdd, ?Rule $sibling = null): void
107107
$siblingPosition = \array_search($sibling, $this->rules[$propertyName], true);
108108
if ($siblingPosition !== false) {
109109
$position = $siblingPosition;
110-
$ruleToAdd->setPosition($sibling->getLineNo(), $sibling->getColNo() - 1);
110+
// Increment column number of all existing rules on same line, starting at sibling
111+
$siblingLineNumber = $sibling->getLineNumber();
112+
$siblingColumnNumber = $sibling->getColumnNumber();
113+
foreach ($this->rules as $rulesForAProperty) {
114+
foreach ($rulesForAProperty as $rule) {
115+
if (
116+
$rule->getLineNumber() === $siblingLineNumber &&
117+
$rule->getColumnNumber() >= $siblingColumnNumber
118+
) {
119+
$rule->setPosition($siblingLineNumber, $rule->getColumnNumber() + 1);
120+
}
121+
}
122+
}
123+
$ruleToAdd->setPosition($siblingLineNumber, $siblingColumnNumber);
111124
}
112125
}
113126
if ($ruleToAdd->getLineNumber() === null) {

tests/Unit/RuleSet/RuleSetTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,6 @@ public function addRuleWithSiblingSetsValidColumnNumber(
271271
int $siblingIndex,
272272
string $propertyNameToAdd
273273
): void {
274-
self::markTestSkipped('tofix: sometimes a negative column number results');
275-
276274
$ruleToAdd = new Rule($propertyNameToAdd);
277275
$this->setRulesFromPropertyNames($initialPropertyNames);
278276
$sibling = $this->subject->getRules()[$siblingIndex];

0 commit comments

Comments
 (0)