Skip to content

Commit

Permalink
UnusedUsesSniff: Fixed false positive
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Jul 21, 2021
1 parent 5716052 commit add9082
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
13 changes: 12 additions & 1 deletion SlevomatCodingStandard/Helpers/ReferencedNameHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use function array_values;
use function count;
use function in_array;
use function preg_match;
use const T_ANON_CLASS;
use const T_ARRAY;
use const T_AS;
Expand All @@ -23,6 +24,7 @@
use const T_CONST;
use const T_DECLARE;
use const T_DOUBLE_COLON;
use const T_DOUBLE_QUOTED_STRING;
use const T_ELLIPSIS;
use const T_EXTENDS;
use const T_FUNCTION;
Expand Down Expand Up @@ -127,7 +129,7 @@ private static function createAllReferencedNames(File $phpcsFile, int $openTagPo
$referencedNames = [];

$beginSearchAtPointer = $openTagPointer + 1;
$nameTokenCodes = TokenHelper::getNameTokenCodes();
$nameTokenCodes = array_merge([T_DOUBLE_QUOTED_STRING], TokenHelper::getNameTokenCodes());
$tokens = $phpcsFile->getTokens();

while (true) {
Expand All @@ -136,6 +138,15 @@ private static function createAllReferencedNames(File $phpcsFile, int $openTagPo
break;
}

if ($tokens[$nameStartPointer]['code'] === T_DOUBLE_QUOTED_STRING) {
if (preg_match('~(' . TypeHelper::REGEXP . ')::~', $tokens[$nameStartPointer]['content'], $matches) === 1) {
$referencedNames[] = new ReferencedName($matches[1], $nameStartPointer, $nameStartPointer, ReferencedName::TYPE_CLASS);
}

$beginSearchAtPointer = $nameStartPointer + 1;
continue;
}

// Attributes are parsed in specific method
$attributeStartPointerBefore = TokenHelper::findPrevious($phpcsFile, T_ATTRIBUTE, $nameStartPointer - 1, $beginSearchAtPointer);
if ($attributeStartPointerBefore !== null) {
Expand Down
8 changes: 3 additions & 5 deletions SlevomatCodingStandard/Helpers/TypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
class TypeHelper
{

public const REGEXP = '\\\\?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*';

/**
* Validates type name according to the allowed characters in type names + namespaces
*
Expand All @@ -22,11 +24,7 @@ class TypeHelper
public static function isTypeName(string $typeName): bool
{
$matches = [];
$result = preg_match(
'~^\\\\?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*$~',
$typeName,
$matches
);
$result = preg_match('~^' . self::REGEXP . '$~', $typeName, $matches);
if ($result === false) {
// @codeCoverageIgnoreStart
throw new Exception('PREG error ' . preg_last_error());
Expand Down
1 change: 1 addition & 0 deletions tests/Helpers/ReferencedNameHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function testGetAllReferencedNames(): void
['STR_PAD_RIGHT', false, true],
['EnumType', false, false],
['UrlGeneratorInterface', false, false],
['ClassInString', false, false],
];

$names = ReferencedNameHelper::getAllReferencedNames($phpcsFile, 0);
Expand Down
10 changes: 10 additions & 0 deletions tests/Helpers/data/lotsOfReferencedNames.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,13 @@ public function generateRoute($router): string
], referenceType: UrlGeneratorInterface::RELATIVE_PATH);
}
}

class InString
{

public function doSomething()
{
return "String interpolation:{$this->getParameter(ClassInString::PARAM_NAME)}";
}

}
3 changes: 3 additions & 0 deletions tests/Sniffs/Namespaces/UnusedUsesSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public function testUnusedUse(): void

// Used class with static variable
self::assertNoSniffError($report, 29);

// Used in string
self::assertNoSniffError($report, 30);
}

public function testUnusedUseWithMultipleNamespaces(): void
Expand Down
3 changes: 3 additions & 0 deletions tests/Sniffs/Namespaces/data/unusedUses.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use EnumClass;
use ReferenceClass;
use ClassWithStaticVariable;
use ClassInString;

class TestClass implements FirstInterface, SecondInterface
{
Expand Down Expand Up @@ -56,6 +57,8 @@ enum_type: EnumClass::VALUE(),
reference_type: ReferenceClass::SOME_CONSTANT
);

$string = "String interpolation:{$this->getParameter(ClassInString::PARAM_NAME)}";

return new NewObject();
}

Expand Down

0 comments on commit add9082

Please sign in to comment.