Skip to content

Commit 9db5df5

Browse files
committed
SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly: Fixed false positives for colliding functions
1 parent 1e78e21 commit 9db5df5

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

SlevomatCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesOnlySniff.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ public function process(File $phpcsFile, $openTagPointer): void
184184
$canonicalName = NamespaceHelper::normalizeToCanonicalName($name);
185185
$unqualifiedName = NamespaceHelper::getUnqualifiedNameFromFullyQualifiedName($name);
186186

187+
$collidingUseStatementUniqueId = UseStatement::getUniqueId($reference->type, $unqualifiedName);
188+
187189
$isFullyQualified = NamespaceHelper::isFullyQualifiedName($name);
188190
$isGlobalFallback = !$isFullyQualified
189191
&& !NamespaceHelper::hasNamespace($name)
@@ -221,9 +223,9 @@ public function process(File $phpcsFile, $openTagPointer): void
221223
}
222224

223225
if (
224-
array_key_exists($lowerCasedUnqualifiedClassName, $useStatements)
226+
array_key_exists($collidingUseStatementUniqueId, $useStatements)
225227
&& $canonicalName !== NamespaceHelper::normalizeToCanonicalName(
226-
$useStatements[$lowerCasedUnqualifiedClassName]->getFullyQualifiedTypeName()
228+
$useStatements[$collidingUseStatementUniqueId]->getFullyQualifiedTypeName()
227229
)
228230
) {
229231
continue;
@@ -233,6 +235,15 @@ public function process(File $phpcsFile, $openTagPointer): void
233235
if (array_key_exists($lowerCasedUnqualifiedFunctionName, $definedFunctionsIndex)) {
234236
continue;
235237
}
238+
239+
if (
240+
array_key_exists($collidingUseStatementUniqueId, $useStatements)
241+
&& $canonicalName !== NamespaceHelper::normalizeToCanonicalName(
242+
$useStatements[$collidingUseStatementUniqueId]->getFullyQualifiedTypeName()
243+
)
244+
) {
245+
continue;
246+
}
236247
} elseif ($reference->isConstant && $this->allowFullyQualifiedNameForCollidingConstants) {
237248
if (array_key_exists($unqualifiedName, $definedConstantsIndex)) {
238249
continue;

tests/Sniffs/Namespaces/ReferenceUsedNamesOnlySniffTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,8 +645,10 @@ public function testCollidingFullyQualifiedFunctionNameDisallowed(): void
645645
['allowFullyQualifiedNameForCollidingFunctions' => false]
646646
);
647647

648-
self::assertSame(1, $report->getErrorCount());
649-
self::assertSniffError($report, 15, ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME);
648+
self::assertSame(2, $report->getErrorCount());
649+
650+
self::assertSniffError($report, 17, ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME);
651+
self::assertSniffError($report, 22, ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME);
650652
}
651653

652654
public function testCollidingFullyQualifiedConstantNameAllowed(): void

tests/Sniffs/Namespaces/data/collidingFullyQualifiedFunctionNames.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace FooNamespace;
44

5+
use function B\decode;
6+
57
function phpversion()
68
{
79
return '6.0.0';
@@ -16,3 +18,5 @@ public function boo()
1618
}
1719

1820
}
21+
22+
decode(\A\decode(''));

0 commit comments

Comments
 (0)