Skip to content

Commit aa73a11

Browse files
VasekPurchartkukulich
authored andcommitted
Add option to sort uses case sensitively
1 parent d852379 commit aa73a11

File tree

5 files changed

+70
-13
lines changed

5 files changed

+70
-13
lines changed

README.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,25 @@ This sniff enforces trailing commas in multi-line arrays and requires short arra
143143
Checks whether uses at the top of a file are alphabetically sorted. Follows natural sorting and takes edge cases with special symbols into consideration. The following code snippet is an example of correctly sorted uses:
144144

145145
```php
146-
use Baz;
147-
use Foo;
148-
use Foo\Bar;
149-
use Foo_Baz;
150-
use Foo_bar;
151-
use Foo1;
152-
use Foo2;
153-
use Foo11;
154-
use Foo22;
155-
use FooBaz;
156-
use Foobar;
157-
use Foobarz;
146+
use LogableTrait;
147+
use LogAware;
148+
use LogFactory;
149+
use LoggerInterface;
150+
use LogLevel;
151+
use LogStandard;
152+
```
153+
154+
Sniff provides the following settings:
155+
156+
* `caseSensitive` - compare namespaces case sensitively, which makes this order correct:
157+
158+
```php
159+
use LogAware;
160+
use LogFactory;
161+
use LogLevel;
162+
use LogStandard;
163+
use LogableTrait;
164+
use LoggerInterface;
158165
```
159166

160167
#### SlevomatCodingStandard.TypeHints.LongTypeHints

SlevomatCodingStandard/Sniffs/Namespaces/AlphabeticallySortedUsesSniff.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ class AlphabeticallySortedUsesSniff implements \PHP_CodeSniffer_Sniff
1111

1212
const CODE_INCORRECT_ORDER = 'IncorrectlyOrderedUses';
1313

14+
/** @var bool */
15+
public $caseSensitive = false;
16+
1417
/** @var \SlevomatCodingStandard\Helpers\UseStatement|null */
1518
private $lastUse;
1619

@@ -101,7 +104,7 @@ private function compareUseStatements(UseStatement $a, UseStatement $b): int
101104
$bNameParts = explode(NamespaceHelper::NAMESPACE_SEPARATOR, $b->getFullyQualifiedTypeName());
102105

103106
for ($i = 0; $i < min(count($aNameParts), count($bNameParts)); $i++) {
104-
$comparison = strcasecmp($aNameParts[$i], $bNameParts[$i]);
107+
$comparison = $this->compare($aNameParts[$i], $bNameParts[$i]);
105108
if ($comparison !== 0) {
106109
return $comparison;
107110
}
@@ -110,4 +113,13 @@ private function compareUseStatements(UseStatement $a, UseStatement $b): int
110113
return count($aNameParts) <=> count($bNameParts);
111114
}
112115

116+
private function compare(string $a, string $b): int
117+
{
118+
if ($this->caseSensitive) {
119+
return strcmp($a, $b);
120+
}
121+
122+
return strcasecmp($a, $b);
123+
}
124+
113125
}

tests/Sniffs/Namespaces/AlphabeticallySortedUsesSniffTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ public function testCorrectOrderOfSimilarNamespaces()
3939
);
4040
}
4141

42+
public function testCorrectOrderOfSimilarNamespacesCaseSensitive()
43+
{
44+
$this->assertNoSniffErrorInFile(
45+
$this->checkFile(__DIR__ . '/data/correctOrderSimilarNamespacesCaseSensitive.php', [
46+
'caseSensitive' => true,
47+
])
48+
);
49+
}
50+
4251
public function testIncorrectOrderOfSimilarNamespaces()
4352
{
4453
$this->assertSniffError(

tests/Sniffs/Namespaces/data/correctOrderSimilarNamespaces.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@
1515
use Foobar;
1616
use Foobarz;
1717
use FooBaz;
18+
use LogableTrait;
19+
use LogAware;
20+
use LogFactory;
21+
use LoggerInterface;
22+
use LogLevel;
23+
use LogStandard;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Lorem;
4+
5+
use Baz;
6+
use Foo;
7+
use Foo\Bar;
8+
use Foo\Bar\Boo;
9+
use Foo1;
10+
use Foo11;
11+
use Foo2;
12+
use Foo22;
13+
use FooBaz;
14+
use Foo_Baz;
15+
use Foo_bar;
16+
use Foobar;
17+
use Foobarz;
18+
use LogAware;
19+
use LogFactory;
20+
use LogLevel;
21+
use LogStandard;
22+
use LogableTrait;
23+
use LoggerInterface;

0 commit comments

Comments
 (0)