Skip to content

[BUGFIX] Ensure non-negative column number in RuleSet #1268

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 1 commit into from
Jun 3, 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

- Ensure `RuleSet::addRule()` sets non-negative column number when sibling
provided (#1268)
- Set line number when `RuleSet::addRule()` called with only column number set
(#1265)
- Ensure first rule added with `RuleSet::addRule()` has valid position (#1262)
Expand Down
15 changes: 14 additions & 1 deletion src/RuleSet/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,20 @@ public function addRule(Rule $ruleToAdd, ?Rule $sibling = null): void
$siblingPosition = \array_search($sibling, $this->rules[$propertyName], true);
if ($siblingPosition !== false) {
$position = $siblingPosition;
$ruleToAdd->setPosition($sibling->getLineNo(), $sibling->getColNo() - 1);
// 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 (
$rule->getLineNumber() === $siblingLineNumber &&
$rule->getColumnNumber() >= $siblingColumnNumber
) {
$rule->setPosition($siblingLineNumber, $rule->getColumnNumber() + 1);
}
}
}
$ruleToAdd->setPosition($siblingLineNumber, $siblingColumnNumber);
}
}
if ($ruleToAdd->getLineNumber() === null) {
Expand Down
2 changes: 0 additions & 2 deletions tests/Unit/RuleSet/RuleSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,6 @@ public function addRuleWithSiblingSetsValidColumnNumber(
int $siblingIndex,
string $propertyNameToAdd
): void {
self::markTestSkipped('tofix: sometimes a negative column number results');

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