Skip to content

Commit 639afb0

Browse files
jost125kukulich
authored andcommitted
Fix detection of colliding classes
There were bug, that lookup was not made in keys, but in values.
1 parent be27df6 commit 639afb0

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

SlevomatCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesOnlySniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ function (ReferencedName $referencedName): string {
150150

151151
if ($this->allowFullyQualifiedNameForCollidingClasses) {
152152
$unqualifiedClassName = NamespaceHelper::getUnqualifiedNameFromFullyQualifiedName($name);
153-
if (isset($referencesIndex[$unqualifiedClassName]) || in_array($unqualifiedClassName, $definedClassesIndex ?? [], true)) {
153+
if (isset($referencesIndex[$unqualifiedClassName]) || array_key_exists($unqualifiedClassName, $definedClassesIndex ?? [])) {
154154
continue;
155155
}
156156
}

tests/Sniffs/Namespaces/ReferenceUsedNamesOnlySniffTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,4 +605,23 @@ public function testCollidingClassNameDifferentNamespacesMoreClassesDisallowed()
605605
$this->assertSniffError($report, 14, ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME);
606606
}
607607

608+
public function testCollidingClassNameExtendsAllowed()
609+
{
610+
$report = $this->checkFile(
611+
__DIR__ . '/data/collidingClassNameExtends.php',
612+
['allowFullyQualifiedNameForCollidingClasses' => true]
613+
);
614+
$this->assertNoSniffErrorInFile($report);
615+
}
616+
617+
public function testCollidingClassNameExtendsDisabled()
618+
{
619+
$report = $this->checkFile(
620+
__DIR__ . '/data/collidingClassNameExtends.php',
621+
['allowFullyQualifiedNameForCollidingClasses' => false]
622+
);
623+
$this->assertSame(1, $report->getErrorCount());
624+
$this->assertSniffError($report, 5, ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME);
625+
}
626+
608627
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Alpha;
4+
5+
class Foo extends \Beta\Foo {
6+
7+
}

0 commit comments

Comments
 (0)