Skip to content

sort #1

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

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Common;
use SlevomatCodingStandard\Helpers\CommentHelper;
use SlevomatCodingStandard\Helpers\NamespaceHelper;
use SlevomatCodingStandard\Helpers\StringHelper;
Expand All @@ -22,6 +23,9 @@
use function sprintf;
use function strcasecmp;
use function strcmp;
use function strcoll;
use function strlen;
use function substr;
use function uasort;
use const T_OPEN_TAG;
use const T_SEMICOLON;
Expand All @@ -32,11 +36,20 @@ class AlphabeticallySortedUsesSniff implements Sniff

public const CODE_INCORRECT_ORDER = 'IncorrectlyOrderedUses';

public const INVALID_ORDER = 'InvalidOrder';

private const SUPPORTED_ORDERING_METHODS = [
'dictionary',
'caseInsensitive',
'caseSensitive',
'locale',
];

/** @var bool */
public $psr12Compatible = true;

/** @var bool */
public $caseSensitive = false;
/** @var string */
public $order = 'caseInsensitive';

/**
* @return array<int, (int|string)>
Expand All @@ -54,6 +67,19 @@ public function register(): array
*/
public function process(File $phpcsFile, $openTagPointer): void
{
if (!in_array($this->order, self::SUPPORTED_ORDERING_METHODS, true)) {
$error = sprintf(
"'%s' is not a valid order for %s. Pick one of: %s",
$this->order,
Common::getSniffCode(self::class),
implode(', ', self::SUPPORTED_ORDERING_METHODS)
);

$phpcsFile->addError($error, $openTagPointer, self::INVALID_ORDER);

return;
}

if (TokenHelper::findPrevious($phpcsFile, T_OPEN_TAG, $openTagPointer - 1) !== null) {
return;
}
Expand Down Expand Up @@ -201,11 +227,50 @@ private function compareUseStatements(UseStatement $a, UseStatement $b): int

private function compare(string $a, string $b): int
{
if ($this->caseSensitive) {
return strcmp($a, $b);
switch ($this->order) {
case 'caseSensitive':
return strcmp($a, $b);
case 'locale':
return strcoll($a, $b);
case 'dictionary':
return $this->dictionaryCompare($a, $b);
}

// default is 'caseInsensitive'
return strcasecmp($a, $b);
}

/**
* Lexicographical namespace string compare.
*
* Example:
*
* use Doctrine\ORM\Query;
* use Doctrine\ORM\Query\Expr;
* use Doctrine\ORM\QueryBuilder;
*
* @param string $a first namespace string
* @param string $b second namespace string
*/
private function dictionaryCompare(string $a, string $b): int
{
$min = min(strlen($a), strlen($b));

for ($i = 0; $i < $min; $i++) {
if ($a[$i] === $b[$i]) {
continue;
}

if ($a[$i] < $b[$i]) {
return -1;
}

if ($a[$i] > $b[$i]) {
return 1;
}
}

return strcmp(substr($a, $min), substr($b, $min));
}

}
28 changes: 27 additions & 1 deletion tests/Sniffs/Namespaces/AlphabeticallySortedUsesSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testCorrectOrderOfSimilarNamespacesCaseSensitive(): void
{
self::assertNoSniffErrorInFile(
self::checkFile(__DIR__ . '/data/correctOrderSimilarNamespacesCaseSensitive.php', [
'caseSensitive' => true,
'order' => 'caseSensitive',
])
);
}
Expand All @@ -66,6 +66,32 @@ public function testPatrikOrder(): void
self::assertNoSniffErrorInFile($report);
}

public function testUnknownOrder(): void
{
self::assertSniffError(
self::checkFile(
__DIR__ . '/data/alphabeticalPatrik.php',
['order' => 'mySpecialOrder']
),
1,
AlphabeticallySortedUsesSniff::INVALID_ORDER,
"'mySpecialOrder' is not a valid order for SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses. Pick one of: dictionary, caseInsensitive, caseSensitive, locale"
);
}

public function testDictionaryOrder(): void
{
self::assertSniffError(
self::checkFile(
__DIR__ . '/data/dictionary.php',
['order' => 'dictionary']
),
17,
AlphabeticallySortedUsesSniff::CODE_INCORRECT_ORDER,
'Expr'
);
}

public function testFixable(): void
{
$report = self::checkFile(
Expand Down
22 changes: 22 additions & 0 deletions tests/Sniffs/Namespaces/data/dictionary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\PersistentCollection;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use MyApp\Bar\Dull;
use MyApp\Bar\DullAdvancedHelper;
use MyApp\Bar\DullHelper;
use MyApp\Bar\Foo;
use MyApp\Bar\FooRepository;
use MyApp\Entity\DullRepository;
use MyApp\Tests\AdvancedTestCase;
use MyApp\Tests\TestCase;
use MyApp\Validator\GetParameterContainer;
use MyApp\Validator\ParameterContainer;
use Doctrine\ORM\Query\Expr;

class Fooo
{

}