Skip to content

Commit a788a1a

Browse files
committed
UseStatementHelper has to distinguish among classes, functions and constants
1 parent 126dc6e commit a788a1a

File tree

8 files changed

+67
-32
lines changed

8 files changed

+67
-32
lines changed

SlevomatCodingStandard/Helpers/NamespaceHelper.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,16 @@ public static function resolveName(
110110
return $nameAsReferencedInFile;
111111
}
112112

113-
$normalizedName = UseStatement::normalizedNameAsReferencedInFile($type, self::normalizeToCanonicalName($nameAsReferencedInFile));
113+
$uniqueId = UseStatement::getUniqueId($type, self::normalizeToCanonicalName($nameAsReferencedInFile));
114114

115-
if (isset($useStatements[$normalizedName])) {
116-
return sprintf('%s%s', self::NAMESPACE_SEPARATOR, $useStatements[$normalizedName]->getFullyQualifiedTypeName());
115+
if (isset($useStatements[$uniqueId])) {
116+
return sprintf('%s%s', self::NAMESPACE_SEPARATOR, $useStatements[$uniqueId]->getFullyQualifiedTypeName());
117117
}
118118

119119
$nameParts = self::getNameParts($nameAsReferencedInFile);
120-
$normalizedNameFirstPart = UseStatement::normalizedNameAsReferencedInFile($type, $nameParts[0]);
121-
if (count($nameParts) > 1 && isset($useStatements[$normalizedNameFirstPart])) {
122-
return sprintf('%s%s%s%s', self::NAMESPACE_SEPARATOR, $useStatements[$normalizedNameFirstPart]->getFullyQualifiedTypeName(), self::NAMESPACE_SEPARATOR, implode(self::NAMESPACE_SEPARATOR, array_slice($nameParts, 1)));
120+
$firstPartUniqueId = UseStatement::getUniqueId($type, $nameParts[0]);
121+
if (count($nameParts) > 1 && isset($useStatements[$firstPartUniqueId])) {
122+
return sprintf('%s%s%s%s', self::NAMESPACE_SEPARATOR, $useStatements[$firstPartUniqueId]->getFullyQualifiedTypeName(), self::NAMESPACE_SEPARATOR, implode(self::NAMESPACE_SEPARATOR, array_slice($nameParts, 1)));
123123
}
124124

125125
$name = sprintf('%s%s', self::NAMESPACE_SEPARATOR, $nameAsReferencedInFile);

SlevomatCodingStandard/Helpers/UseStatement.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ public function __construct(
3838
$this->type = $type;
3939
}
4040

41+
public static function getUniqueId(string $type, string $name): string
42+
{
43+
$normalizedName = self::normalizedNameAsReferencedInFile($type, $name);
44+
45+
if ($type === self::TYPE_DEFAULT) {
46+
return $normalizedName;
47+
}
48+
49+
return sprintf('%s %s', $type, $normalizedName);
50+
}
51+
4152
public static function normalizedNameAsReferencedInFile(string $type, string $name): string
4253
{
4354
if ($type !== self::TYPE_CONSTANT) {
@@ -89,6 +100,19 @@ public function hasSameType(self $that): bool
89100
return $this->type === $that->type;
90101
}
91102

103+
public function getTypeName(): ?string
104+
{
105+
if ($this->isConstant()) {
106+
return 'const';
107+
}
108+
109+
if ($this->isFunction()) {
110+
return 'function';
111+
}
112+
113+
return null;
114+
}
115+
92116
public function compareByType(self $that): int
93117
{
94118
$order = [

SlevomatCodingStandard/Helpers/UseStatementHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public static function getUseStatements(\PHP_CodeSniffer\Files\File $phpcsFile,
9797
$usePointer,
9898
$type
9999
);
100-
$useStatements[$useStatement->getCanonicalNameAsReferencedInFile()] = $useStatement;
100+
$useStatements[UseStatement::getUniqueId($type, $name)] = $useStatement;
101101
}
102102

103103
self::$allUseStatements[$cacheKey] = $useStatements;

SlevomatCodingStandard/Sniffs/Namespaces/AbstractFullyQualifiedGlobalReference.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use SlevomatCodingStandard\Helpers\ReferencedName;
77
use SlevomatCodingStandard\Helpers\ReferencedNameHelper;
88
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
9+
use SlevomatCodingStandard\Helpers\UseStatement;
910
use SlevomatCodingStandard\Helpers\UseStatementHelper;
1011

1112
abstract class AbstractFullyQualifiedGlobalReference
@@ -60,7 +61,7 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $openTagPointer)
6061

6162
$canonicalName = $this->isCaseSensitive() ? $name : strtolower($name);
6263

63-
if (array_key_exists($canonicalName, $useStatements)) {
64+
if (array_key_exists(UseStatement::getUniqueId($referencedName->getType(), $canonicalName), $useStatements)) {
6465
$fullyQualifiedName = NamespaceHelper::resolveName($phpcsFile, $name, $referencedName->getType(), $useStatements, $namePointer);
6566
if (NamespaceHelper::hasNamespace($fullyQualifiedName)) {
6667
continue;

SlevomatCodingStandard/Sniffs/Namespaces/FullyQualifiedExceptionsSniff.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $openTagPointer)
7272
foreach ($referencedNames as $referencedName) {
7373
$pointer = $referencedName->getStartPointer();
7474
$name = $referencedName->getNameAsReferencedInFile();
75-
$normalizedName = UseStatement::normalizedNameAsReferencedInFile($referencedName->getType(), $name);
76-
if (isset($useStatements[$normalizedName]) && $referencedName->hasSameUseStatementType($useStatements[$normalizedName])) {
77-
$useStatement = $useStatements[$normalizedName];
75+
$uniqueId = UseStatement::getUniqueId($referencedName->getType(), $name);
76+
if (isset($useStatements[$uniqueId]) && $referencedName->hasSameUseStatementType($useStatements[$uniqueId])) {
77+
$useStatement = $useStatements[$uniqueId];
7878
if (
7979
in_array($useStatement->getFullyQualifiedTypeName(), $this->getIgnoredNames(), true)
8080
|| (

SlevomatCodingStandard/Sniffs/Namespaces/UnusedUsesSniff.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,24 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $openTagPointer)
4444
$nameParts = NamespaceHelper::getNameParts($name);
4545
$nameAsReferencedInFile = $nameParts[0];
4646
$nameReferencedWithoutSubNamespace = count($nameParts) === 1;
47-
$normalizedNameAsReferencedInFile = $nameReferencedWithoutSubNamespace
48-
? UseStatement::normalizedNameAsReferencedInFile($referencedName->getType(), $nameAsReferencedInFile)
49-
: UseStatement::normalizedNameAsReferencedInFile(ReferencedName::TYPE_DEFAULT, $nameAsReferencedInFile);
47+
$uniqueId = $nameReferencedWithoutSubNamespace
48+
? UseStatement::getUniqueId($referencedName->getType(), $nameAsReferencedInFile)
49+
: UseStatement::getUniqueId(ReferencedName::TYPE_DEFAULT, $nameAsReferencedInFile);
5050
if (
5151
!NamespaceHelper::isFullyQualifiedName($name)
52-
&& isset($unusedNames[$normalizedNameAsReferencedInFile])
52+
&& isset($unusedNames[$uniqueId])
5353
) {
54-
if ($nameReferencedWithoutSubNamespace && !$referencedName->hasSameUseStatementType($unusedNames[$normalizedNameAsReferencedInFile])) {
54+
if ($nameReferencedWithoutSubNamespace && !$referencedName->hasSameUseStatementType($unusedNames[$uniqueId])) {
5555
continue;
5656
}
57-
if ($unusedNames[$normalizedNameAsReferencedInFile]->getNameAsReferencedInFile() !== $nameAsReferencedInFile) {
57+
if ($unusedNames[$uniqueId]->getNameAsReferencedInFile() !== $nameAsReferencedInFile) {
5858
$phpcsFile->addError(sprintf(
5959
'Case of reference name %s and use statement %s do not match.',
6060
$nameAsReferencedInFile,
61-
$unusedNames[$normalizedNameAsReferencedInFile]->getNameAsReferencedInFile()
61+
$unusedNames[$uniqueId]->getNameAsReferencedInFile()
6262
), $pointer, self::CODE_MISMATCHING_CASE);
6363
}
64-
unset($unusedNames[$normalizedNameAsReferencedInFile]);
64+
unset($unusedNames[$uniqueId]);
6565
}
6666
}
6767

tests/Helpers/UseStatementHelperTest.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,17 @@ public function testGetFullyQualifiedTypeNameFromUse(): void
7979
$loremIpsumUsePointer = TokenHelper::findNext($codeSnifferFile, T_USE, $fooUsePointer + 1);
8080
$this->assertSame('Lorem\Ipsum', UseStatementHelper::getFullyQualifiedTypeNameFromUse($codeSnifferFile, $loremIpsumUsePointer));
8181

82-
$rasmusFooConstantUsePointer = TokenHelper::findNext($codeSnifferFile, T_USE, $loremIpsumUsePointer + 1);
83-
$this->assertSame('Rasmus\FOO_CONSTANT', UseStatementHelper::getFullyQualifiedTypeNameFromUse($codeSnifferFile, $rasmusFooConstantUsePointer));
82+
$lerdorfIsBarConstantUsePointer = TokenHelper::findNext($codeSnifferFile, T_USE, $loremIpsumUsePointer + 1);
83+
$this->assertSame('Lerdorf\IS_BAR', UseStatementHelper::getFullyQualifiedTypeNameFromUse($codeSnifferFile, $lerdorfIsBarConstantUsePointer));
8484

85-
$lerdorfIsBarPointer = TokenHelper::findNext($codeSnifferFile, T_USE, $rasmusFooConstantUsePointer + 1);
86-
$this->assertSame('Lerdorf\isBar', UseStatementHelper::getFullyQualifiedTypeNameFromUse($codeSnifferFile, $lerdorfIsBarPointer));
85+
$rasmusFooConstantUsePointer = TokenHelper::findNext($codeSnifferFile, T_USE, $lerdorfIsBarConstantUsePointer + 1);
86+
$this->assertSame('Rasmus\FOO', UseStatementHelper::getFullyQualifiedTypeNameFromUse($codeSnifferFile, $rasmusFooConstantUsePointer));
87+
88+
$lerdorfIsBarFunctionUsePointer = TokenHelper::findNext($codeSnifferFile, T_USE, $rasmusFooConstantUsePointer + 1);
89+
$this->assertSame('Lerdorf\isBar', UseStatementHelper::getFullyQualifiedTypeNameFromUse($codeSnifferFile, $lerdorfIsBarFunctionUsePointer));
90+
91+
$rasmusFooFunctionUsePointer = TokenHelper::findNext($codeSnifferFile, T_USE, $lerdorfIsBarFunctionUsePointer + 1);
92+
$this->assertSame('Rasmus\foo', UseStatementHelper::getFullyQualifiedTypeNameFromUse($codeSnifferFile, $rasmusFooFunctionUsePointer));
8793
}
8894

8995
public function testGetUseStatements(): void
@@ -92,14 +98,16 @@ public function testGetUseStatements(): void
9298
__DIR__ . '/data/useStatements.php'
9399
);
94100
$useStatements = UseStatementHelper::getUseStatements($codeSnifferFile, 0);
95-
$this->assertCount(6, $useStatements);
96-
$this->assertSame(2, $useStatements['baz']->getPointer());
97-
$this->assertUseStatement('Bar\Baz', 'Baz', $useStatements['baz'], false, false);
98-
$this->assertUseStatement('Foo', 'Foo', $useStatements['foo'], false, false);
99-
$this->assertUseStatement('Lorem\Ipsum', 'LoremIpsum', $useStatements['loremipsum'], false, false);
100-
$this->assertUseStatement('Zero', 'Zero', $useStatements['zero'], false, false);
101-
$this->assertUseStatement('Rasmus\FOO_CONSTANT', 'FOO_CONSTANT', $useStatements['FOO_CONSTANT'], false, true);
102-
$this->assertUseStatement('Lerdorf\isBar', 'isBar', $useStatements['isbar'], true, false);
101+
$this->assertCount(8, $useStatements);
102+
$this->assertSame(2, $useStatements[UseStatement::getUniqueId(UseStatement::TYPE_DEFAULT, 'Baz')]->getPointer());
103+
$this->assertUseStatement('Bar\Baz', 'Baz', $useStatements[UseStatement::getUniqueId(UseStatement::TYPE_DEFAULT, 'Baz')], false, false);
104+
$this->assertUseStatement('Foo', 'Foo', $useStatements[UseStatement::getUniqueId(UseStatement::TYPE_DEFAULT, 'Foo')], false, false);
105+
$this->assertUseStatement('Lorem\Ipsum', 'LoremIpsum', $useStatements[UseStatement::getUniqueId(UseStatement::TYPE_DEFAULT, 'LoremIpsum')], false, false);
106+
$this->assertUseStatement('Zero', 'Zero', $useStatements[UseStatement::getUniqueId(UseStatement::TYPE_DEFAULT, 'Zero')], false, false);
107+
$this->assertUseStatement('Rasmus\FOO', 'FOO', $useStatements[UseStatement::getUniqueId(UseStatement::TYPE_CONSTANT, 'FOO')], false, true);
108+
$this->assertUseStatement('Rasmus\foo', 'foo', $useStatements[UseStatement::getUniqueId(UseStatement::TYPE_FUNCTION, 'foo')], true, false);
109+
$this->assertUseStatement('Lerdorf\IS_BAR', 'IS_BAR', $useStatements[UseStatement::getUniqueId(UseStatement::TYPE_CONSTANT, 'IS_BAR')], false, true);
110+
$this->assertUseStatement('Lerdorf\isBar', 'isBar', $useStatements[UseStatement::getUniqueId(UseStatement::TYPE_FUNCTION, 'isBar')], true, false);
103111
}
104112

105113
private function assertUseStatement(string $fullyQualifiedTypeName, string $referencedName, UseStatement $useStatement, bool $isFunction, bool $isConstant): void

tests/Helpers/data/useStatements.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
use Bar\Baz;
44
use Foo;
55
use Lorem\Ipsum as LoremIpsum;
6-
use const Rasmus\FOO_CONSTANT;
6+
use const Lerdorf\IS_BAR;
7+
use const Rasmus\FOO;
78
use function Lerdorf\isBar;
9+
use function Rasmus\foo;
810

911
class FooBar
1012
{

0 commit comments

Comments
 (0)