Skip to content

Commit aff06ae

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

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

SlevomatCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesOnlySniff.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,15 @@ public function process(File $phpcsFile, $openTagPointer): void
248248
if (array_key_exists($unqualifiedName, $definedConstantsIndex)) {
249249
continue;
250250
}
251+
252+
if (
253+
array_key_exists($collidingUseStatementUniqueId, $useStatements)
254+
&& $canonicalName !== NamespaceHelper::normalizeToCanonicalName(
255+
$useStatements[$collidingUseStatementUniqueId]->getFullyQualifiedTypeName()
256+
)
257+
) {
258+
continue;
259+
}
251260
}
252261
}
253262

tests/Sniffs/Namespaces/ReferenceUsedNamesOnlySniffTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -667,8 +667,20 @@ public function testCollidingFullyQualifiedConstantNameDisallowed(): void
667667
['allowFullyQualifiedNameForCollidingConstants' => false]
668668
);
669669

670-
self::assertSame(1, $report->getErrorCount());
671-
self::assertSniffError($report, 12, ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME);
670+
self::assertSame(2, $report->getErrorCount());
671+
672+
self::assertSniffError(
673+
$report,
674+
14,
675+
ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME,
676+
'Constant \PHP_VERSION should not be referenced via a fully qualified name, but via a use statement.'
677+
);
678+
self::assertSniffError(
679+
$report,
680+
14,
681+
ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME,
682+
'Constant \A\SOMETHING should not be referenced via a fully qualified name, but via a use statement.'
683+
);
672684
}
673685

674686
public function testReferencingGlobalFunctionViaFallbackErrorsWithMoreComplexSettings(): void

tests/Sniffs/Namespaces/data/collidingFullyQualifiedConstantNames.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
namespace FooNamespace;
44

5+
use const SOMETHING;
6+
57
const PHP_VERSION = '6.0.0';
68

79
class Foo
810
{
911

1012
public function boo()
1113
{
12-
echo \PHP_VERSION;
14+
echo \PHP_VERSION . SOMETHING . \A\SOMETHING;
1315
}
1416

1517
}

0 commit comments

Comments
 (0)