Skip to content

[BUGFIX] AddRule before sibling with different property name #1270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
28 changes: 28 additions & 0 deletions src/RuleSet/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,25 @@ public function addRule(Rule $ruleToAdd, ?Rule $sibling = null): void
$position = \count($this->rules[$propertyName]);

if ($sibling !== null) {
$siblingIsInSet = false;
$siblingPosition = \array_search($sibling, $this->rules[$propertyName], true);
if ($siblingPosition !== false) {
$siblingIsInSet = true;
$position = $siblingPosition;
} else {
$siblingIsInSet = $this->hasRule($sibling);
if ($siblingIsInSet) {
// 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 (self::comparePositionable($rule, $sibling) >= 0) {
$position = $index;
break;
}
}
}
}
if ($siblingIsInSet) {
// Increment column number of all existing rules on same line, starting at sibling
$siblingLineNumber = $sibling->getLineNumber();
$siblingColumnNumber = $sibling->getColumnNumber();
Expand All @@ -123,6 +139,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 +322,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