Skip to content

Commit c656687

Browse files
committed
AlphabeticallySortedUsesSniff more compatible with PHPStorm
1 parent cbf7e54 commit c656687

File tree

2 files changed

+12
-29
lines changed

2 files changed

+12
-29
lines changed

SlevomatCodingStandard/Sniffs/Namespaces/AlphabeticallySortedUsesSniff.php

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -96,36 +96,18 @@ private function compareUseStatements(UseStatement $a, UseStatement $b): int
9696
if (!$a->hasSameType($b)) {
9797
return $a->compareByType($b);
9898
}
99-
$aName = $a->getFullyQualifiedTypeName();
100-
$bName = $b->getFullyQualifiedTypeName();
101-
102-
$i = 0;
103-
for (; $i < min(strlen($aName), strlen($bName)); $i++) {
104-
if ($this->isSpecialCharacter($aName[$i]) && !$this->isSpecialCharacter($bName[$i])) {
105-
return -1;
106-
} elseif (!$this->isSpecialCharacter($aName[$i]) && $this->isSpecialCharacter($bName[$i])) {
107-
return 1;
108-
}
10999

110-
if (is_numeric($aName[$i]) && is_numeric($bName[$i])) {
111-
break;
112-
}
100+
$aNameParts = explode(NamespaceHelper::NAMESPACE_SEPARATOR, $a->getFullyQualifiedTypeName());
101+
$bNameParts = explode(NamespaceHelper::NAMESPACE_SEPARATOR, $b->getFullyQualifiedTypeName());
113102

114-
$cmp = strcasecmp($aName[$i], $bName[$i]);
115-
if (
116-
$cmp !== 0
117-
|| ($aName[$i] !== $bName[$i] && strtolower($aName[$i]) === strtolower($bName[$i]))
118-
) {
119-
return $cmp;
103+
for ($i = 0; $i < min(count($aNameParts), count($bNameParts)); $i++) {
104+
$comparison = strcasecmp($aNameParts[$i], $bNameParts[$i]);
105+
if ($comparison !== 0) {
106+
return $comparison;
120107
}
121108
}
122109

123-
return strnatcasecmp(substr($aName, $i), substr($bName, $i));
124-
}
125-
126-
private function isSpecialCharacter(string $character): bool
127-
{
128-
return in_array($character, ['\\', '_'], true);
110+
return count($aNameParts) <=> count($bNameParts);
129111
}
130112

131113
}

tests/Sniffs/Namespaces/data/correctOrderSimilarNamespaces.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
use Baz;
66
use Foo;
77
use Foo\Bar;
8-
use Foo_Baz;
9-
use Foo_bar;
8+
use Foo\Bar\Boo;
109
use Foo1;
11-
use Foo2;
1210
use Foo11;
11+
use Foo2;
1312
use Foo22;
14-
use FooBaz;
13+
use Foo_bar;
14+
use Foo_Baz;
1515
use Foobar;
1616
use Foobarz;
17+
use FooBaz;

0 commit comments

Comments
 (0)