From f11e98d562134a357d4d03392f78713ac4a37d73 Mon Sep 17 00:00:00 2001 From: Jake Hotson Date: Wed, 4 Jun 2025 18:18:19 +0100 Subject: [PATCH] [CLEANUP] Extract method `RuleSet::comparePositionable` As well as being used with `usort()`, it may have other uses. The deprecated `getLineNo()` and `getColNo()` are still used for now. Replacing these will be done separately. Relates to #974. --- src/RuleSet/RuleSet.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/RuleSet/RuleSet.php b/src/RuleSet/RuleSet.php index a1f26c660..b24eefab2 100644 --- a/src/RuleSet/RuleSet.php +++ b/src/RuleSet/RuleSet.php @@ -173,12 +173,7 @@ public function getRules(?string $searchPattern = null): array $result = \array_merge($result, $rules); } } - \usort($result, static function (Rule $first, Rule $second): int { - if ($first->getLineNo() === $second->getLineNo()) { - return $first->getColNo() - $second->getColNo(); - } - return $first->getLineNo() - $second->getLineNo(); - }); + \usort($result, [self::class, 'comparePositionable']); return $result; } @@ -299,4 +294,15 @@ protected function renderRules(OutputFormat $outputFormat): string return $formatter->removeLastSemicolon($result); } + + /** + * @return int negative if `$first` is before `$second`; zero if they have the same position; positive otherwise + */ + private static function comparePositionable(Positionable $first, Positionable $second): int + { + if ($first->getLineNo() === $second->getLineNo()) { + return $first->getColNo() - $second->getColNo(); + } + return $first->getLineNo() - $second->getLineNo(); + } }