Skip to content

Commit 66c86f6

Browse files
committed
ReferenceUsedNamesOnlySniff: Fixed missing reports
1 parent 8835dfa commit 66c86f6

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

SlevomatCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesOnlySniff.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,20 +197,21 @@ public function process(File $phpcsFile, $openTagPointer): void
197197

198198
$references = $this->getReferences($phpcsFile, $openTagPointer);
199199

200-
$definedClassesIndex = array_flip(array_map(static function (string $className): string {
201-
return strtolower($className);
202-
}, ClassHelper::getAllNames($phpcsFile)));
200+
$definedClassesIndex = [];
201+
foreach (ClassHelper::getAllNames($phpcsFile) as $definedClassPointer => $definedClassName) {
202+
$definedClassesIndex[strtolower($definedClassName)] = NamespaceHelper::resolveClassName($phpcsFile, $definedClassName, $definedClassPointer);
203+
}
203204
$definedFunctionsIndex = array_flip(array_map(static function (string $functionName): string {
204205
return strtolower($functionName);
205206
}, FunctionHelper::getAllFunctionNames($phpcsFile)));
206207
$definedConstantsIndex = array_flip(ConstantHelper::getAllNames($phpcsFile));
207208

209+
$classReferencesIndex = [];
208210
if ($this->allowFullyQualifiedNameForCollidingClasses) {
209211
$classReferences = array_filter($references, static function (stdClass $reference): bool {
210212
return $reference->source === self::SOURCE_CODE && $reference->isClass;
211213
});
212214

213-
$classReferencesIndex = [];
214215
foreach ($classReferences as $classReference) {
215216
$classReferencesIndex[strtolower($classReference->name)] = NamespaceHelper::resolveName($phpcsFile, $classReference->name, $classReference->type, $classReference->startPointer);
216217
}
@@ -242,7 +243,10 @@ public function process(File $phpcsFile, $openTagPointer): void
242243
if ($isFullyQualified) {
243244
if ($reference->isClass && $this->allowFullyQualifiedNameForCollidingClasses) {
244245
$lowerCasedUnqualifiedClassName = strtolower($unqualifiedName);
245-
if (array_key_exists($lowerCasedUnqualifiedClassName, $definedClassesIndex)) {
246+
if (
247+
array_key_exists($lowerCasedUnqualifiedClassName, $definedClassesIndex)
248+
&& $canonicalName !== NamespaceHelper::normalizeToCanonicalName($definedClassesIndex[$lowerCasedUnqualifiedClassName])
249+
) {
246250
continue;
247251
}
248252

@@ -357,7 +361,11 @@ public function process(File $phpcsFile, $openTagPointer): void
357361

358362
if (!(
359363
$useStatement->getCanonicalNameAsReferencedInFile() === $canonicalNameToReference
360-
|| ($reference->isClass && array_key_exists($canonicalNameToReference, $definedClassesIndex))
364+
|| (
365+
$reference->isClass
366+
&& array_key_exists($canonicalNameToReference, $definedClassesIndex)
367+
&& $canonicalName !== NamespaceHelper::normalizeToCanonicalName($definedClassesIndex[$canonicalNameToReference])
368+
)
361369
|| ($reference->isFunction && array_key_exists($canonicalNameToReference, $definedFunctionsIndex))
362370
|| ($reference->isConstant && array_key_exists($canonicalNameToReference, $definedConstantsIndex))
363371
)) {
@@ -383,14 +391,19 @@ public function process(File $phpcsFile, $openTagPointer): void
383391
}
384392

385393
if ($fix) {
386-
$alreadyUsed = false;
394+
$addUse = true;
395+
396+
if ($reference->isClass && array_key_exists($canonicalNameToReference, $definedClassesIndex)) {
397+
$addUse = false;
398+
}
399+
387400
foreach ($useStatements as $useStatement) {
388401
if ($useStatement->getType() !== $reference->type || $useStatement->getFullyQualifiedTypeName() !== $canonicalName) {
389402
continue;
390403
}
391404

392405
$nameToReference = $useStatement->getNameAsReferencedInFile();
393-
$alreadyUsed = true;
406+
$addUse = false;
394407
break;
395408
}
396409

@@ -420,7 +433,7 @@ public function process(File $phpcsFile, $openTagPointer): void
420433
$phpcsFile->fixer->replaceToken($i, '');
421434
}
422435

423-
if (!$alreadyUsed) {
436+
if ($addUse) {
424437
$useStatementPlacePointer = $this->getUseStatementPlacePointer($phpcsFile, $openTagPointer, $useStatements);
425438

426439
$useTypeName = UseStatement::getTypeName($reference->type);

tests/Sniffs/Namespaces/ReferenceUsedNamesOnlySniffTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ public function testSearchingInAnnotations(): void
882882
]
883883
);
884884

885-
self::assertSame(30, $report->getErrorCount());
885+
self::assertSame(31, $report->getErrorCount());
886886

887887
self::assertSniffError($report, 8, ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME, 'Class \Foo\DateTime should not be referenced via a fully qualified name, but via a use statement.');
888888
self::assertSniffError($report, 9, ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME, 'Class \Foo\DateTime should not be referenced via a fully qualified name, but via a use statement.');
@@ -919,6 +919,8 @@ public function testSearchingInAnnotations(): void
919919
self::assertSniffError($report, 121, ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME, 'Class \Foo\DateTime should not be referenced via a fully qualified name, but via a use statement.');
920920
self::assertSniffError($report, 121, ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME, 'Class \Foo\ArrayObject should not be referenced via a fully qualified name, but via a use statement.');
921921

922+
self::assertSniffError($report, 131, ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME, 'Class \Foo\Test\Bla\Whatever should not be referenced via a fully qualified name, but via a use statement.');
923+
922924
self::assertAllFixedInFile($report);
923925
}
924926

tests/Sniffs/Namespaces/data/shouldBeInUseStatementSearchingInAnnotations.fixed.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,11 @@ class ConstantExpression
128128
{
129129

130130
}
131+
132+
class Whatever
133+
{
134+
135+
/** @var Whatever */
136+
private $whatever;
137+
138+
}

tests/Sniffs/Namespaces/data/shouldBeInUseStatementSearchingInAnnotations.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,11 @@ class ConstantExpression
124124
{
125125

126126
}
127+
128+
class Whatever
129+
{
130+
131+
/** @var \Foo\Test\Bla\Whatever */
132+
private $whatever;
133+
134+
}

0 commit comments

Comments
 (0)