Skip to content

Commit

Permalink
fix: order by lowercase'd chars in AlphabeticallyOrderedConstantsSniff
Browse files Browse the repository at this point in the history
  • Loading branch information
David Kurka committed Dec 1, 2023
1 parent ef69411 commit 2ba92c8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use function in_array;
use function sort;
use function sprintf;
use function strtolower;
use function ucfirst;
use function usort;

Expand All @@ -36,6 +37,7 @@
* }
* @phpstan-type NameShape array{
* content: string,
* lowercaseContent: string,
* ptr: int
* }
* @phpstan-type ValueShape array{
Expand Down Expand Up @@ -63,7 +65,7 @@ public function process(File $phpcsFile, mixed $stackPtr): void

foreach ($namesWithValuesByVisibility as $visibility => $namesWithValues) {
$constantNames = array_map(
static fn (array $nameWithValue): string => $nameWithValue['name']['content'],
static fn (array $nameWithValue): string => $nameWithValue['name']['lowercaseContent'],
$namesWithValues,
);
$sortedConstantNames = $constantNames;
Expand Down Expand Up @@ -95,7 +97,7 @@ private function fix(File $file, array $namesWithValues): void
$sortedNameAndValueTokens = $namesWithValues;
usort(
$sortedNameAndValueTokens,
static fn (array $a, array $b): int => $a['name']['content'] <=> $b['name']['content'],
static fn (array $a, array $b): int => $a['name']['lowercaseContent'] <=> $b['name']['lowercaseContent'],
);

$fixer->beginChangeset();
Expand Down Expand Up @@ -184,7 +186,11 @@ private function findConstantName(File $phpcsFile, int $constStackPtr): array|nu
return null;
}

return ['content' => $tokens[$constantNameTokenPointer]['content'], 'ptr' => $constantNameTokenPointer];
return [
'content' => $tokens[$constantNameTokenPointer]['content'],
'lowercaseContent' => strtolower($tokens[$constantNameTokenPointer]['content']),
'ptr' => $constantNameTokenPointer,
];
}

private function findEqualsPointer(File $phpcsFile, int $constNameStackPtr): int|null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testErrors(): void
$expectedErrors = [
9 => AlphabeticallyOrderedConstantsSniff::CodeIncorrectConstantOrder,
19 => AlphabeticallyOrderedConstantsSniff::CodeIncorrectConstantOrder,
24 => AlphabeticallyOrderedConstantsSniff::CodeIncorrectConstantOrder,
25 => AlphabeticallyOrderedConstantsSniff::CodeIncorrectConstantOrder,
];
$possibleLines = array_keys($expectedErrors);
$errors = $file->getErrors();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ final class TestClass
protected const E = 'e';
protected const F = 'f';
protected const G = 'g';
protected const H = 'h';
protected const Ha = 'h';
protected const HB = 'h';

private const I = 'i';
private const J = 'j';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ final class TestClass
public const D = [123, 'test'];
public const B = 'b';

protected const H = 'h';
protected const E = 'e';
protected const G = 'g';
protected const F = 'f';
protected const HB = 'h';
protected const Ha = 'h';

private const K = 'k';
private const J = 'j';
Expand Down

0 comments on commit 2ba92c8

Please sign in to comment.