Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[BUGFIX] AddRule before sibling with different property name
Part of #974.
  • Loading branch information
JakeQZ committed Jun 4, 2025
commit 7642f68b3425fa08fc46b14683b2ad94f839010d
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ Please also have a look at our

### Fixed

- Insert `Rule` before sibling even with different property name
(in `RuleSet::addRule()`) (#1270)
- Ensure `RuleSet::addRule()` sets non-negative column number when sibling
provided (#1268)
- Set line number when `RuleSet::addRule()` called with only column number set
Expand Down
33 changes: 31 additions & 2 deletions src/RuleSet/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,29 @@ public function addRule(Rule $ruleToAdd, ?Rule $sibling = null): void
$position = \count($this->rules[$propertyName]);

if ($sibling !== null) {
$siblingLineNumber = $sibling->getLineNumber();
$siblingColumnNumber = $sibling->getColumnNumber();
$siblingIsInSet = false;
$siblingPosition = \array_search($sibling, $this->rules[$propertyName], true);
if ($siblingPosition !== false) {
$siblingIsInSet = true;
$position = $siblingPosition;
} elseif ($siblingIsInSet = $this->hasRule($sibling)) {
// Maintain ordering within `$this->rules[$propertyName]`
// by inserting before first `Rule` with a same-or-later position than the sibling.
foreach ($this->rules[$propertyName] as $index => $rule) {
if (
$rule->getLineNumber() > $siblingLineNumber ||
$rule->getLineNumber() === $siblingLineNumber &&
$rule->getColumnNumber() >= $siblingColumnNumber
) {
$position = $index;
break;
}
}
}
if ($siblingIsInSet) {
// Increment column number of all existing rules on same line, starting at sibling
$siblingLineNumber = $sibling->getLineNumber();
$siblingColumnNumber = $sibling->getColumnNumber();
foreach ($this->rules as $rulesForAProperty) {
foreach ($rulesForAProperty as $rule) {
if (
Expand All @@ -123,6 +140,7 @@ public function addRule(Rule $ruleToAdd, ?Rule $sibling = null): void
$ruleToAdd->setPosition($siblingLineNumber, $siblingColumnNumber);
}
}

if ($ruleToAdd->getLineNumber() === null) {
//this node is added manually, give it the next best line
$columnNumber = $ruleToAdd->getColumnNumber() ?? 0;
Expand Down Expand Up @@ -305,4 +323,15 @@ private static function comparePositionable(Positionable $first, Positionable $s
}
return $first->getLineNo() - $second->getLineNo();
}

private function hasRule(Rule $rule): bool
{
foreach ($this->rules as $rulesForAProperty) {
if (\in_array($rule, $rulesForAProperty, true)) {
return true;
}
}

return false;
}
}
2 changes: 0 additions & 2 deletions tests/Unit/RuleSet/RuleSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,6 @@ public function addRuleWithSiblingInsertsRuleBeforeSibling(
int $siblingIndex,
string $propertyNameToAdd
): void {
self::markTestSkipped('tofix: if property names don\'t match, sibling is ignored');

$ruleToAdd = new Rule($propertyNameToAdd);
$this->setRulesFromPropertyNames($initialPropertyNames);
$sibling = $this->subject->getRules()[$siblingIndex];
Expand Down