Skip to content

Commit 42af857

Browse files
authored
Improve priorities calculation (#851)
1 parent e469937 commit 42af857

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

.dev-tools/src/Priority/PriorityCollection.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PhpCsFixer\Fixer\FixerInterface;
1515
use PhpCsFixer\FixerFactory;
16+
use PhpCsFixerCustomFixers\Fixer\CommentSurroundedBySpacesFixer;
1617
use PhpCsFixerCustomFixers\Fixers;
1718
use Tests\PriorityTest;
1819

@@ -84,12 +85,35 @@ private function isFixerWithoutPriorityInCollection(): bool
8485

8586
private function getFirstPriorityFixerWithoutPriority(): ?PriorityFixer
8687
{
87-
foreach ($this->priorityFixers as $priorityFixer) {
88-
if (!$priorityFixer->hasPriority()) {
89-
return $priorityFixer;
90-
}
88+
static $firstFixers = [
89+
CommentSurroundedBySpacesFixer::class,
90+
];
91+
92+
$priorityFixersWithoutPriorities = \array_filter(
93+
$this->priorityFixers,
94+
static fn (PriorityFixer $priorityFixer) => !$priorityFixer->hasPriority()
95+
);
96+
97+
if ($priorityFixersWithoutPriorities === []) {
98+
return null;
9199
}
92100

93-
return null;
101+
\usort(
102+
$priorityFixersWithoutPriorities,
103+
static function (PriorityFixer $p1, PriorityFixer $p2) use ($firstFixers): int {
104+
foreach ($firstFixers as $firstFixer) {
105+
if ($p1->name() === $firstFixer) {
106+
return -1;
107+
}
108+
if ($p2->name() === $firstFixer) {
109+
return 1;
110+
}
111+
}
112+
113+
return $p1->name() <=> $p2->name();
114+
}
115+
);
116+
117+
return \reset($priorityFixersWithoutPriorities);
94118
}
95119
}

.dev-tools/src/Priority/PriorityFixer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public function __construct(FixerInterface $fixer, ?int $priority)
3333
$this->priority = $priority;
3434
}
3535

36+
public function name(): string
37+
{
38+
return \get_class($this->fixer);
39+
}
40+
3641
public function addFixerToRunAfter(self $priorityFixer): void
3742
{
3843
$this->fixersToRunAfter[] = $priorityFixer;

0 commit comments

Comments
 (0)