Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@17
|
3 * \Drupal\Sniffs\Classes\UnusedUseStatementSniff.
|
Chris@0
|
4 *
|
Chris@0
|
5 * @category PHP
|
Chris@0
|
6 * @package PHP_CodeSniffer
|
Chris@0
|
7 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
8 */
|
Chris@0
|
9
|
Chris@17
|
10 namespace Drupal\Sniffs\Classes;
|
Chris@17
|
11
|
Chris@17
|
12 use PHP_CodeSniffer\Files\File;
|
Chris@17
|
13 use PHP_CodeSniffer\Sniffs\Sniff;
|
Chris@17
|
14 use PHP_CodeSniffer\Util\Tokens;
|
Chris@17
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Checks for "use" statements that are not needed in a file.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @category PHP
|
Chris@0
|
20 * @package PHP_CodeSniffer
|
Chris@0
|
21 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
22 */
|
Chris@17
|
23 class UnusedUseStatementSniff implements Sniff
|
Chris@0
|
24 {
|
Chris@0
|
25
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * Returns an array of tokens this test wants to listen for.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @return array
|
Chris@0
|
31 */
|
Chris@0
|
32 public function register()
|
Chris@0
|
33 {
|
Chris@0
|
34 return array(T_USE);
|
Chris@0
|
35
|
Chris@0
|
36 }//end register()
|
Chris@0
|
37
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * Processes this test, when one of its tokens is encountered.
|
Chris@0
|
41 *
|
Chris@17
|
42 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
|
Chris@17
|
43 * @param int $stackPtr The position of the current token in
|
Chris@17
|
44 * the stack passed in $tokens.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @return void
|
Chris@0
|
47 */
|
Chris@17
|
48 public function process(File $phpcsFile, $stackPtr)
|
Chris@0
|
49 {
|
Chris@0
|
50 $tokens = $phpcsFile->getTokens();
|
Chris@0
|
51
|
Chris@0
|
52 // Only check use statements in the global scope.
|
Chris@0
|
53 if (empty($tokens[$stackPtr]['conditions']) === false) {
|
Chris@0
|
54 return;
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 // Seek to the end of the statement and get the string before the semi colon.
|
Chris@0
|
58 $semiColon = $phpcsFile->findEndOfStatement($stackPtr);
|
Chris@0
|
59 if ($tokens[$semiColon]['code'] !== T_SEMICOLON) {
|
Chris@0
|
60 return;
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 $classPtr = $phpcsFile->findPrevious(
|
Chris@17
|
64 Tokens::$emptyTokens,
|
Chris@0
|
65 ($semiColon - 1),
|
Chris@0
|
66 null,
|
Chris@0
|
67 true
|
Chris@0
|
68 );
|
Chris@0
|
69
|
Chris@0
|
70 if ($tokens[$classPtr]['code'] !== T_STRING) {
|
Chris@0
|
71 return;
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 // Search where the class name is used. PHP treats class names case
|
Chris@0
|
75 // insensitive, that's why we cannot search for the exact class name string
|
Chris@0
|
76 // and need to iterate over all T_STRING tokens in the file.
|
Chris@0
|
77 $classUsed = $phpcsFile->findNext(T_STRING, ($classPtr + 1));
|
Chris@0
|
78 $lowerClassName = strtolower($tokens[$classPtr]['content']);
|
Chris@0
|
79
|
Chris@0
|
80 // Check if the referenced class is in the same namespace as the current
|
Chris@0
|
81 // file. If it is then the use statement is not necessary.
|
Chris@0
|
82 $namespacePtr = $phpcsFile->findPrevious([T_NAMESPACE], $stackPtr);
|
Chris@0
|
83 // Check if the use statement does aliasing with the "as" keyword. Aliasing
|
Chris@0
|
84 // is allowed even in the same namespace.
|
Chris@0
|
85 $aliasUsed = $phpcsFile->findPrevious(T_AS, ($classPtr - 1), $stackPtr);
|
Chris@0
|
86
|
Chris@0
|
87 if ($namespacePtr !== false && $aliasUsed === false) {
|
Chris@0
|
88 $nsEnd = $phpcsFile->findNext(
|
Chris@0
|
89 [
|
Chris@0
|
90 T_NS_SEPARATOR,
|
Chris@0
|
91 T_STRING,
|
Chris@0
|
92 T_WHITESPACE,
|
Chris@0
|
93 ],
|
Chris@0
|
94 ($namespacePtr + 1),
|
Chris@0
|
95 null,
|
Chris@0
|
96 true
|
Chris@0
|
97 );
|
Chris@0
|
98 $namespace = trim($phpcsFile->getTokensAsString(($namespacePtr + 1), ($nsEnd - $namespacePtr - 1)));
|
Chris@0
|
99
|
Chris@0
|
100 $useNamespacePtr = $phpcsFile->findNext([T_STRING], ($stackPtr + 1));
|
Chris@0
|
101 $useNamespaceEnd = $phpcsFile->findNext(
|
Chris@0
|
102 [
|
Chris@0
|
103 T_NS_SEPARATOR,
|
Chris@0
|
104 T_STRING,
|
Chris@0
|
105 ],
|
Chris@0
|
106 ($useNamespacePtr + 1),
|
Chris@0
|
107 null,
|
Chris@0
|
108 true
|
Chris@0
|
109 );
|
Chris@0
|
110 $use_namespace = rtrim($phpcsFile->getTokensAsString($useNamespacePtr, ($useNamespaceEnd - $useNamespacePtr - 1)), '\\');
|
Chris@0
|
111
|
Chris@0
|
112 if (strcasecmp($namespace, $use_namespace) === 0) {
|
Chris@0
|
113 $classUsed = false;
|
Chris@0
|
114 }
|
Chris@0
|
115 }//end if
|
Chris@0
|
116
|
Chris@0
|
117 while ($classUsed !== false) {
|
Chris@0
|
118 if (strtolower($tokens[$classUsed]['content']) === $lowerClassName) {
|
Chris@0
|
119 // If the name is used in a PHP 7 function return type declaration
|
Chris@0
|
120 // stop.
|
Chris@0
|
121 if ($tokens[$classUsed]['code'] === T_RETURN_TYPE) {
|
Chris@0
|
122 return;
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 $beforeUsage = $phpcsFile->findPrevious(
|
Chris@17
|
126 Tokens::$emptyTokens,
|
Chris@0
|
127 ($classUsed - 1),
|
Chris@0
|
128 null,
|
Chris@0
|
129 true
|
Chris@0
|
130 );
|
Chris@0
|
131 // If a backslash is used before the class name then this is some other
|
Chris@0
|
132 // use statement.
|
Chris@0
|
133 if ($tokens[$beforeUsage]['code'] !== T_USE && $tokens[$beforeUsage]['code'] !== T_NS_SEPARATOR) {
|
Chris@0
|
134 return;
|
Chris@0
|
135 }
|
Chris@0
|
136
|
Chris@0
|
137 // Trait use statement within a class.
|
Chris@0
|
138 if ($tokens[$beforeUsage]['code'] === T_USE && empty($tokens[$beforeUsage]['conditions']) === false) {
|
Chris@0
|
139 return;
|
Chris@0
|
140 }
|
Chris@0
|
141 }//end if
|
Chris@0
|
142
|
Chris@0
|
143 $classUsed = $phpcsFile->findNext([T_STRING, T_RETURN_TYPE], ($classUsed + 1));
|
Chris@0
|
144 }//end while
|
Chris@0
|
145
|
Chris@0
|
146 $warning = 'Unused use statement';
|
Chris@0
|
147 $fix = $phpcsFile->addFixableWarning($warning, $stackPtr, 'UnusedUse');
|
Chris@0
|
148 if ($fix === true) {
|
Chris@0
|
149 // Remove the whole use statement line.
|
Chris@0
|
150 $phpcsFile->fixer->beginChangeset();
|
Chris@0
|
151 for ($i = $stackPtr; $i <= $semiColon; $i++) {
|
Chris@0
|
152 $phpcsFile->fixer->replaceToken($i, '');
|
Chris@0
|
153 }
|
Chris@0
|
154
|
Chris@0
|
155 // Also remove whitespace after the semicolon (new lines).
|
Chris@0
|
156 while (isset($tokens[$i]) === true && $tokens[$i]['code'] === T_WHITESPACE) {
|
Chris@0
|
157 $phpcsFile->fixer->replaceToken($i, '');
|
Chris@0
|
158 if (strpos($tokens[$i]['content'], $phpcsFile->eolChar) !== false) {
|
Chris@0
|
159 break;
|
Chris@0
|
160 }
|
Chris@0
|
161
|
Chris@0
|
162 $i++;
|
Chris@0
|
163 }
|
Chris@0
|
164
|
Chris@0
|
165 // Replace @var data types in doc comments with the fully qualified class
|
Chris@0
|
166 // name.
|
Chris@0
|
167 $useNamespacePtr = $phpcsFile->findNext([T_STRING], ($stackPtr + 1));
|
Chris@0
|
168 $useNamespaceEnd = $phpcsFile->findNext(
|
Chris@0
|
169 [
|
Chris@0
|
170 T_NS_SEPARATOR,
|
Chris@0
|
171 T_STRING,
|
Chris@0
|
172 ],
|
Chris@0
|
173 ($useNamespacePtr + 1),
|
Chris@0
|
174 null,
|
Chris@0
|
175 true
|
Chris@0
|
176 );
|
Chris@0
|
177 $fullNamespace = $phpcsFile->getTokensAsString($useNamespacePtr, ($useNamespaceEnd - $useNamespacePtr));
|
Chris@0
|
178
|
Chris@0
|
179 $tag = $phpcsFile->findNext(T_DOC_COMMENT_TAG, ($stackPtr + 1));
|
Chris@0
|
180
|
Chris@0
|
181 while ($tag !== false) {
|
Chris@0
|
182 if (($tokens[$tag]['content'] === '@var' || $tokens[$tag]['content'] === '@return')
|
Chris@0
|
183 && isset($tokens[($tag + 1)]) === true
|
Chris@0
|
184 && $tokens[($tag + 1)]['code'] === T_DOC_COMMENT_WHITESPACE
|
Chris@0
|
185 && isset($tokens[($tag + 2)]) === true
|
Chris@0
|
186 && $tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING
|
Chris@0
|
187 && strpos($tokens[($tag + 2)]['content'], $tokens[$classPtr]['content']) === 0
|
Chris@0
|
188 ) {
|
Chris@0
|
189 $replacement = '\\'.$fullNamespace.substr($tokens[($tag + 2)]['content'], strlen($tokens[$classPtr]['content']));
|
Chris@0
|
190 $phpcsFile->fixer->replaceToken(($tag + 2), $replacement);
|
Chris@0
|
191 }
|
Chris@0
|
192
|
Chris@0
|
193 $tag = $phpcsFile->findNext(T_DOC_COMMENT_TAG, ($tag + 1));
|
Chris@0
|
194 }
|
Chris@0
|
195
|
Chris@0
|
196 $phpcsFile->fixer->endChangeset();
|
Chris@0
|
197 }//end if
|
Chris@0
|
198
|
Chris@0
|
199 }//end process()
|
Chris@0
|
200
|
Chris@0
|
201
|
Chris@0
|
202 }//end class
|