From a63fb4d23414ed3800d7819103b4049134b30d8c Mon Sep 17 00:00:00 2001 From: mscherer Date: Tue, 24 Dec 2024 00:58:23 +0100 Subject: [PATCH] Fix broken unused use. --- .../Namespaces/UnusedUseStatementSniff.php | 118 ------------------ docs/sniffs.md | 5 +- 2 files changed, 2 insertions(+), 121 deletions(-) delete mode 100644 PSR2R/Sniffs/Namespaces/UnusedUseStatementSniff.php diff --git a/PSR2R/Sniffs/Namespaces/UnusedUseStatementSniff.php b/PSR2R/Sniffs/Namespaces/UnusedUseStatementSniff.php deleted file mode 100644 index 749611c..0000000 --- a/PSR2R/Sniffs/Namespaces/UnusedUseStatementSniff.php +++ /dev/null @@ -1,118 +0,0 @@ -getTokens(); - - if ($this->shouldIgnoreUse($phpcsFile, $stackPtr)) { - return; - } - - $semicolonIndex = $phpcsFile->findEndOfStatement($stackPtr); - if ($tokens[$semicolonIndex]['code'] !== T_SEMICOLON) { - return; - } - - $classNameIndex = $phpcsFile->findPrevious( - Tokens::$emptyTokens, - $semicolonIndex - 1, - null, - true, - ); - - // Seek along the statement to get the last part, which is the class/interface name. - while (isset($tokens[$classNameIndex + 1]) === true - && in_array($tokens[$classNameIndex + 1]['code'], [T_STRING, T_NS_SEPARATOR], false) - ) { - $classNameIndex++; - } - - if ($tokens[$classNameIndex]['code'] !== T_STRING) { - return; - } - - $classUsed = $phpcsFile->findNext( - [T_STRING, T_RETURN_TYPE], - $classNameIndex + 1, - null, - false, - $tokens[$classNameIndex]['content'], - ); - - while ($classUsed !== false) { - $beforeUsage = $phpcsFile->findPrevious( - Tokens::$emptyTokens, - $classUsed - 1, - null, - true, - ); - // If a backslash is used before the class name then this is some other - // use statement. - if ($tokens[$beforeUsage]['code'] !== T_USE && $tokens[$beforeUsage]['code'] !== T_NS_SEPARATOR) { - return; - } - - // Trait use statement within a class. - if ($tokens[$beforeUsage]['code'] === T_USE && !empty($tokens[$beforeUsage]['conditions'])) { - return; - } - - $classUsed = $phpcsFile->findNext( - [T_STRING, T_RETURN_TYPE], - $classUsed + 1, - null, - false, - $tokens[$classNameIndex]['content'], - ); - } - - $warning = 'Unused use statement'; - $fix = $phpcsFile->addFixableWarning($warning, $stackPtr, 'UnusedUse'); - if (!$fix) { - return; - } - - $phpcsFile->fixer->beginChangeset(); - for ($i = $stackPtr; $i <= $semicolonIndex; $i++) { - $phpcsFile->fixer->replaceToken($i, ''); - } - - // Also remove whitespace after the semicolon (new lines). - while (!empty($tokens[$i]) && $tokens[$i]['code'] === T_WHITESPACE) { - $phpcsFile->fixer->replaceToken($i, ''); - if (strpos($tokens[$i]['content'], $phpcsFile->eolChar) !== false) { - break; - } - - $i++; - } - - $phpcsFile->fixer->endChangeset(); - } - -} diff --git a/docs/sniffs.md b/docs/sniffs.md index 0228031..aa387a6 100644 --- a/docs/sniffs.md +++ b/docs/sniffs.md @@ -1,6 +1,6 @@ # PSR2R Code Sniffer -The PSR2R standard contains 191 sniffs +The PSR2R standard contains 190 sniffs Generic (22 sniffs) ------------------- @@ -90,7 +90,7 @@ PSR2 (6 sniffs) - PSR2.Namespaces.NamespaceDeclaration - PSR2.Namespaces.UseDeclaration -PSR2R (44 sniffs) +PSR2R (43 sniffs) ----------------- - PSR2R.Classes.BraceOnSameLine - PSR2R.Classes.InterfaceName @@ -120,7 +120,6 @@ PSR2R (44 sniffs) - PSR2R.Methods.MethodDeclaration - PSR2R.Methods.MethodMultilineArguments - PSR2R.Namespaces.NoInlineFullyQualifiedClassName -- PSR2R.Namespaces.UnusedUseStatement - PSR2R.PHP.DuplicateSemicolon - PSR2R.PHP.ListComma - PSR2R.PHP.NoShortOpenTag