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