Skip to content

Commit

Permalink
chore: Utils::sortElements - better typing (PHP-CS-Fixer#7646)
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus authored and danog committed Feb 2, 2024
1 parent 5edf9fd commit f671c13
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
13 changes: 11 additions & 2 deletions src/Fixer/ClassNotation/OrderedClassElementsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ private function sortElements(array $elements): array
'doteardown' => 10,
];

$getPositionType = function (array $element) use ($phpunitPositions) {
$getPositionType = function (array $element) use ($phpunitPositions): int {
$type = $element['type'];

if (\in_array($type, ['method', 'magic', 'phpunit'], true) && isset($this->typePosition["method:{$element['name']}"])) {
Expand Down Expand Up @@ -539,8 +539,17 @@ private function sortElements(array $elements): array

return Utils::stableSort(
$elements,
/**
* @return array{element: _ClassElement, position: int}
*/
static fn (array $element): array => ['element' => $element, 'position' => $getPositionType($element)],
fn ($a, $b): int => ($a['position'] === $b['position']) ? $this->sortGroupElements($a['element'], $b['element']) : $a['position'] <=> $b['position'],
/**
* @param array{element: _ClassElement, position: int} $a
* @param array{element: _ClassElement, position: int} $b
*
* @return -1|0|1
*/
fn (array $a, array $b): int => ($a['position'] === $b['position']) ? $this->sortGroupElements($a['element'], $b['element']) : $a['position'] <=> $b['position'],
);
}

Expand Down
11 changes: 7 additions & 4 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,14 @@ public static function calculateTrailingWhitespaceIndent(Token $token): string
*
* Stability is ensured by using Schwartzian transform.
*
* @param list<mixed> $elements
* @param callable $getComparedValue a callable that takes a single element and returns the value to compare
* @param callable $compareValues a callable that compares two values
* @template T
* @template R
*
* @return mixed[]
* @param list<T> $elements
* @param callable(T): R $getComparedValue a callable that takes a single element and returns the value to compare
* @param callable(R, R): int $compareValues a callable that compares two values
*
* @return list<T>
*/
public static function stableSort(array $elements, callable $getComparedValue, callable $compareValues): array
{
Expand Down

0 comments on commit f671c13

Please sign in to comment.