View source
<?php
namespace Drupal\Sniffs\Classes;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
class UnusedUseStatementSniff implements Sniff {
public function register() {
return array(
T_USE,
);
}
public function process(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile
->getTokens();
if (empty($tokens[$stackPtr]['conditions']) === false) {
return;
}
$semiColon = $phpcsFile
->findEndOfStatement($stackPtr);
if ($tokens[$semiColon]['code'] !== T_SEMICOLON) {
return;
}
$classPtr = $phpcsFile
->findPrevious(Tokens::$emptyTokens, $semiColon - 1, null, true);
if ($tokens[$classPtr]['code'] !== T_STRING) {
return;
}
$classUsed = $phpcsFile
->findNext(T_STRING, $classPtr + 1);
$lowerClassName = strtolower($tokens[$classPtr]['content']);
$namespacePtr = $phpcsFile
->findPrevious([
T_NAMESPACE,
], $stackPtr);
$aliasUsed = $phpcsFile
->findPrevious(T_AS, $classPtr - 1, $stackPtr);
if ($namespacePtr !== false && $aliasUsed === false) {
$nsEnd = $phpcsFile
->findNext([
T_NS_SEPARATOR,
T_STRING,
T_WHITESPACE,
], $namespacePtr + 1, null, true);
$namespace = trim($phpcsFile
->getTokensAsString($namespacePtr + 1, $nsEnd - $namespacePtr - 1));
$useNamespacePtr = $phpcsFile
->findNext([
T_STRING,
], $stackPtr + 1);
$useNamespaceEnd = $phpcsFile
->findNext([
T_NS_SEPARATOR,
T_STRING,
], $useNamespacePtr + 1, null, true);
$use_namespace = rtrim($phpcsFile
->getTokensAsString($useNamespacePtr, $useNamespaceEnd - $useNamespacePtr - 1), '\\');
if (strcasecmp($namespace, $use_namespace) === 0) {
$classUsed = false;
}
}
while ($classUsed !== false) {
if (strtolower($tokens[$classUsed]['content']) === $lowerClassName) {
if ($tokens[$classUsed]['code'] === T_RETURN_TYPE) {
return;
}
$beforeUsage = $phpcsFile
->findPrevious(Tokens::$emptyTokens, $classUsed - 1, null, true);
if ($tokens[$beforeUsage]['code'] !== T_USE && $tokens[$beforeUsage]['code'] !== T_NS_SEPARATOR) {
return;
}
if ($tokens[$beforeUsage]['code'] === T_USE && empty($tokens[$beforeUsage]['conditions']) === false) {
return;
}
}
$classUsed = $phpcsFile
->findNext([
T_STRING,
T_RETURN_TYPE,
], $classUsed + 1);
}
$warning = 'Unused use statement';
$fix = $phpcsFile
->addFixableWarning($warning, $stackPtr, 'UnusedUse');
if ($fix === true) {
$phpcsFile->fixer
->beginChangeset();
for ($i = $stackPtr; $i <= $semiColon; $i++) {
$phpcsFile->fixer
->replaceToken($i, '');
}
while (isset($tokens[$i]) === true && $tokens[$i]['code'] === T_WHITESPACE) {
$phpcsFile->fixer
->replaceToken($i, '');
if (strpos($tokens[$i]['content'], $phpcsFile->eolChar) !== false) {
break;
}
$i++;
}
$useNamespacePtr = $phpcsFile
->findNext([
T_STRING,
], $stackPtr + 1);
$useNamespaceEnd = $phpcsFile
->findNext([
T_NS_SEPARATOR,
T_STRING,
], $useNamespacePtr + 1, null, true);
$fullNamespace = $phpcsFile
->getTokensAsString($useNamespacePtr, $useNamespaceEnd - $useNamespacePtr);
$tag = $phpcsFile
->findNext(T_DOC_COMMENT_TAG, $stackPtr + 1);
while ($tag !== false) {
if (($tokens[$tag]['content'] === '@var' || $tokens[$tag]['content'] === '@return') && isset($tokens[$tag + 1]) === true && $tokens[$tag + 1]['code'] === T_DOC_COMMENT_WHITESPACE && isset($tokens[$tag + 2]) === true && $tokens[$tag + 2]['code'] === T_DOC_COMMENT_STRING && strpos($tokens[$tag + 2]['content'], $tokens[$classPtr]['content']) === 0) {
$replacement = '\\' . $fullNamespace . substr($tokens[$tag + 2]['content'], strlen($tokens[$classPtr]['content']));
$phpcsFile->fixer
->replaceToken($tag + 2, $replacement);
}
$tag = $phpcsFile
->findNext(T_DOC_COMMENT_TAG, $tag + 1);
}
$phpcsFile->fixer
->endChangeset();
}
}
}