Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnusedUsesSniff: improve performance #932

Merged
merged 1 commit into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions SlevomatCodingStandard/Helpers/NamespaceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ class NamespaceHelper

public const NAMESPACE_SEPARATOR = '\\';

/**
* @param File $phpcsFile
* @return int[]
*/
public static function getAllNamespacesPointers(File $phpcsFile): array
{
return TokenHelper::findNextAll($phpcsFile, T_NAMESPACE, 0);
}

public static function isFullyQualifiedName(string $typeName): bool
{
return StringHelper::startsWith($typeName, self::NAMESPACE_SEPARATOR);
Expand Down
63 changes: 34 additions & 29 deletions SlevomatCodingStandard/Sniffs/Namespaces/UnusedUsesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,39 +86,44 @@ public function process(File $phpcsFile, $openTagPointer): void
$fileUnusedNames = UseStatementHelper::getFileUseStatements($phpcsFile);
$referencedNames = ReferencedNameHelper::getAllReferencedNames($phpcsFile, $startPointer);

$namespaces = NamespaceHelper::getAllNamespacesPointers($phpcsFile);
if (count($namespaces) === 0) {
$namespaces = [$startPointer];
}

$allUsedNames = [];
foreach ($referencedNames as $referencedName) {
$name = $referencedName->getNameAsReferencedInFile();
$pointer = $referencedName->getStartPointer();
$nameParts = NamespaceHelper::getNameParts($name);
$nameAsReferencedInFile = $nameParts[0];
$nameReferencedWithoutSubNamespace = count($nameParts) === 1;

$pointerBeforeUseStatements = TokenHelper::findPrevious($phpcsFile, T_NAMESPACE, $pointer - 1);
if ($pointerBeforeUseStatements === null) {
$pointerBeforeUseStatements = $startPointer;
}
foreach ($namespaces as $pointerBeforeUseStatements) {
foreach ($referencedNames as $referencedName) {
$name = $referencedName->getNameAsReferencedInFile();
$pointer = $referencedName->getStartPointer();
$nameParts = NamespaceHelper::getNameParts($name);
$nameAsReferencedInFile = $nameParts[0];
$nameReferencedWithoutSubNamespace = count($nameParts) === 1;
$uniqueId = $nameReferencedWithoutSubNamespace
? UseStatement::getUniqueId($referencedName->getType(), $nameAsReferencedInFile)
: UseStatement::getUniqueId(ReferencedName::TYPE_DEFAULT, $nameAsReferencedInFile);
if (
NamespaceHelper::isFullyQualifiedName($name)
|| !array_key_exists($pointerBeforeUseStatements, $fileUnusedNames)
|| !array_key_exists($uniqueId, $fileUnusedNames[$pointerBeforeUseStatements])
) {
continue;
}

$uniqueId = $nameReferencedWithoutSubNamespace
? UseStatement::getUniqueId($referencedName->getType(), $nameAsReferencedInFile)
: UseStatement::getUniqueId(ReferencedName::TYPE_DEFAULT, $nameAsReferencedInFile);
if (
NamespaceHelper::isFullyQualifiedName($name)
|| !array_key_exists($pointerBeforeUseStatements, $fileUnusedNames)
|| !array_key_exists($uniqueId, $fileUnusedNames[$pointerBeforeUseStatements])
) {
continue;
}
if ($fileUnusedNames[$pointerBeforeUseStatements][$uniqueId]->getNameAsReferencedInFile() !== $nameAsReferencedInFile) {
$phpcsFile->addError(
sprintf(
'Case of reference name "%s" and use statement "%s" does not match.',
$nameAsReferencedInFile,
$fileUnusedNames[$pointerBeforeUseStatements][$uniqueId]->getNameAsReferencedInFile()
),
$pointer,
self::CODE_MISMATCHING_CASE
);
}

if ($fileUnusedNames[$pointerBeforeUseStatements][$uniqueId]->getNameAsReferencedInFile() !== $nameAsReferencedInFile) {
$phpcsFile->addError(sprintf(
'Case of reference name "%s" and use statement "%s" does not match.',
$nameAsReferencedInFile,
$fileUnusedNames[$pointerBeforeUseStatements][$uniqueId]->getNameAsReferencedInFile()
), $pointer, self::CODE_MISMATCHING_CASE);
$allUsedNames[$pointerBeforeUseStatements][$uniqueId] = true;
}

$allUsedNames[$pointerBeforeUseStatements][$uniqueId] = true;
}

if ($this->searchAnnotations) {
Expand Down
9 changes: 9 additions & 0 deletions tests/Helpers/NamespaceHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ public function testClosestNamespaceNameWithMultipleNamespacesInFile(): void
self::assertSame('Lorem\Ipsum', $namespace);
}

public function testGetAllNamespacesWithMultipleNamespacesInFile(): void
{
$phpcsFile = $this->getCodeSnifferFile(
__DIR__ . '/data/multipleNamespaces.php'
);
$namespaces = NamespaceHelper::getAllNamespacesPointers($phpcsFile);
self::assertEquals([2, 16], $namespaces);
}

public function testResolveClassNameWithMoreNamespaces(): void
{
$phpcsFile = $this->getCodeSnifferFile(
Expand Down
44 changes: 44 additions & 0 deletions tests/Sniffs/Namespaces/UnusedUsesSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,50 @@ public function testUnusedUse(): void
);
}

public function testUnusedUseWithMultipleNamespaces(): void
{
$report = self::checkFile(__DIR__ . '/data/unusedUsesMultipleNamespaces.php');

self::assertEquals(8, $report->getErrorCount());
self::assertSniffError(
$report,
5,
UnusedUsesSniff::CODE_UNUSED_USE,
'First\ObjectPrototype'
);
self::assertSniffError(
$report,
12,
UnusedUsesSniff::CODE_UNUSED_USE,
'FooBar\UnusedFunction'
);
self::assertSniffError(
$report,
14,
UnusedUsesSniff::CODE_UNUSED_USE,
'FooBar\UNUSED_CONSTANT'
);
self::assertSniffError(
$report,
53,
UnusedUsesSniff::CODE_UNUSED_USE,
'Human\Arm'
);
self::assertSniffError(
$report,
55,
UnusedUsesSniff::CODE_UNUSED_USE,
'Human\Leg'
);

self::assertSniffError(
$report,
56,
UnusedUsesSniff::CODE_UNUSED_USE,
'Human\HEAD_SIZE_UNUSED'
);
}

public function testUnusedUseNoNamespaceNoErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/unusedUsesNoNamespaceNoErrors.php');
Expand Down
66 changes: 66 additions & 0 deletions tests/Sniffs/Namespaces/data/unusedUsesMultipleNamespaces.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Foo;

use First\ObjectPrototype;
use Framework;
use Framework\Subnamespace;
use My\ObjectPrototype as MyObject;
use NewNamespace\ObjectPrototype as NewObject;
use R\S;
use T;
use function FooBar\UnusedFunction;
use function LoremIpsum\UsedFunction;
use const FooBar\UNUSED_CONSTANT;
use const LoremIpsum\USED_CONSTANT;
use X;
use Lorem\FirstInterface;
use Ipsum\SecondInterface;
use Zetta\Rasmus;
use My\PartialClass;
use My\PartialFunction;
use My\PartialConstant;
use PartialNamespace;

class TestClass implements FirstInterface, SecondInterface
{

public function test(S $s): Rasmus
{
new \Test\Foo\Bar();
$date = T::today();
new Framework\FooObject();
new Subnamespace\BarObject();

$functionNameAsClass = new UnusedFunction();
$unusedConstant = new UNUSED_CONSTANT();
UsedFunction();
doFoo(USED_CONSTANT);

new PartialClass\UsedClass();
PartialFunction\usedFunction();
PartialConstant\USED_CONSTANT;
new PartialNamespace\UsedClass();

return new NewObject();
}

}

namespace Boo;

use Human\Meat;
use Human\Arm;
use Human\Brain;
use Human\Leg;
use const Human\HEAD_SIZE_UNUSED;
use const Human\HEAD_SIZE_USED;

class Zombie {
public function eatBrain(Brain $brain):Meat {
$bite = new Bite();
$bite->applyOn($brain, HEAD_SIZE_USED);

return new Meat;
}
}