Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@0
|
3 * Drupal_Sniffs_Classes_FullyQualifiedNamespaceSniff.
|
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 that class references do not use FQN but use statements.
|
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_FullyQualifiedNamespaceSniff 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_NS_SEPARATOR);
|
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 PHP_CodeSniffer file where the
|
Chris@0
|
37 * token was found.
|
Chris@0
|
38 * @param int $stackPtr The position in the PHP_CodeSniffer
|
Chris@0
|
39 * file's token stack where the token
|
Chris@0
|
40 * was found.
|
Chris@0
|
41 *
|
Chris@0
|
42 * @return void|int Optionally returns a stack pointer. The sniff will not be
|
Chris@0
|
43 * called again on the current file until the returned stack
|
Chris@0
|
44 * pointer is reached. Return $phpcsFile->numTokens + 1 to skip
|
Chris@0
|
45 * the rest of the file.
|
Chris@0
|
46 */
|
Chris@0
|
47 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
Chris@0
|
48 {
|
Chris@0
|
49 $tokens = $phpcsFile->getTokens();
|
Chris@0
|
50
|
Chris@0
|
51 // Skip this sniff in *api.php files because they want to have fully
|
Chris@0
|
52 // qualified names for documentation purposes.
|
Chris@0
|
53 if (substr($phpcsFile->getFilename(), -8) === '.api.php') {
|
Chris@0
|
54 return ($phpcsFile->numTokens + 1);
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 // We are only interested in a backslash embedded between strings, which
|
Chris@0
|
58 // means this is a class reference with more than once namespace part.
|
Chris@0
|
59 if ($tokens[($stackPtr - 1)]['code'] !== T_STRING || $tokens[($stackPtr + 1)]['code'] !== T_STRING) {
|
Chris@0
|
60 return;
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 // Check if this is a use statement and ignore those.
|
Chris@0
|
64 $before = $phpcsFile->findPrevious([T_STRING, T_NS_SEPARATOR, T_WHITESPACE], $stackPtr, null, true);
|
Chris@0
|
65 if ($tokens[$before]['code'] === T_USE || $tokens[$before]['code'] === T_NAMESPACE) {
|
Chris@0
|
66 return $phpcsFile->findNext([T_STRING, T_NS_SEPARATOR], ($stackPtr + 1), null, true);
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 // If this is a namespaced function call then ignore this because use
|
Chris@0
|
70 // statements for functions are not possible in PHP 5.5 and lower.
|
Chris@0
|
71 $after = $phpcsFile->findNext([T_STRING, T_NS_SEPARATOR, T_WHITESPACE], $stackPtr, null, true);
|
Chris@0
|
72 if ($tokens[$after]['code'] === T_OPEN_PARENTHESIS && $tokens[$before]['code'] !== T_NEW) {
|
Chris@0
|
73 return ($after + 1);
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 $error = 'Namespaced classes/interfaces/traits should be referenced with use statements';
|
Chris@0
|
77 $fix = $phpcsFile->addFixableError($error, $stackPtr, 'UseStatementMissing');
|
Chris@0
|
78
|
Chris@0
|
79 if ($fix === true) {
|
Chris@0
|
80 $fullName = $phpcsFile->getTokensAsString(($before + 1), ($after - 1 - $before));
|
Chris@0
|
81 $fullName = trim($fullName, '\ ');
|
Chris@0
|
82
|
Chris@0
|
83 $phpcsFile->fixer->beginChangeset();
|
Chris@0
|
84
|
Chris@0
|
85 // Replace the fully qualified name with the local name.
|
Chris@0
|
86 for ($i = ($before + 1); $i < $after; $i++) {
|
Chris@0
|
87 if ($tokens[$i]['code'] !== T_WHITESPACE) {
|
Chris@0
|
88 $phpcsFile->fixer->replaceToken($i, '');
|
Chris@0
|
89 }
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 $parts = explode('\\', $fullName);
|
Chris@0
|
93 $className = end($parts);
|
Chris@0
|
94 $phpcsFile->fixer->addContentBefore(($after - 1), $className);
|
Chris@0
|
95
|
Chris@0
|
96 // Check if there is a use statement already for this class and
|
Chris@0
|
97 // namespace.
|
Chris@0
|
98 $alreadyUsed = false;
|
Chris@0
|
99 $useStatement = $phpcsFile->findNext(T_USE, 0);
|
Chris@0
|
100 while ($useStatement !== false && empty($tokens[$useStatement]['conditions']) === true) {
|
Chris@0
|
101 $useEnd = $phpcsFile->findEndOfStatement($useStatement);
|
Chris@0
|
102 $classRef = trim($phpcsFile->getTokensAsString(($useStatement + 1), ($useEnd - 1 - $useStatement)));
|
Chris@0
|
103 if (strcasecmp($classRef, $fullName) === 0) {
|
Chris@0
|
104 $alreadyUsed = true;
|
Chris@0
|
105 break;
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 $useStatement = $phpcsFile->findNext(T_USE, ($useEnd + 1));
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 // @todo Check if the name is already in use - then we need to alias it.
|
Chris@0
|
112 // Insert use statement at the beginning of the file if it is not there
|
Chris@0
|
113 // already. Also check if another sniff (for example
|
Chris@0
|
114 // UnusedUseStatementSniff) has already deleted the use statement, then
|
Chris@0
|
115 // we need to add it back.
|
Chris@0
|
116 if ($alreadyUsed === false
|
Chris@0
|
117 || $phpcsFile->fixer->getTokenContent($useStatement) !== $tokens[$useStatement]['content']
|
Chris@0
|
118 ) {
|
Chris@0
|
119 // Check if there is a group of use statements and add it there.
|
Chris@0
|
120 $useStatement = $phpcsFile->findNext(T_USE, 0);
|
Chris@0
|
121 if ($useStatement !== false && empty($tokens[$useStatement]['conditions']) === true) {
|
Chris@0
|
122 $phpcsFile->fixer->addContentBefore($useStatement, "use $fullName;\n");
|
Chris@0
|
123 } else {
|
Chris@0
|
124 // Check if there is an @file comment.
|
Chris@0
|
125 $beginning = 0;
|
Chris@0
|
126 $fileComment = $phpcsFile->findNext(T_WHITESPACE, ($beginning + 1), null, true);
|
Chris@0
|
127 if ($tokens[$fileComment]['code'] === T_DOC_COMMENT_OPEN_TAG) {
|
Chris@0
|
128 $beginning = $tokens[$fileComment]['comment_closer'];
|
Chris@0
|
129 }
|
Chris@0
|
130
|
Chris@0
|
131 $phpcsFile->fixer->addContent($beginning, "use $fullName;\n");
|
Chris@0
|
132 }
|
Chris@0
|
133 }
|
Chris@0
|
134
|
Chris@0
|
135 $phpcsFile->fixer->endChangeset();
|
Chris@0
|
136 }//end if
|
Chris@0
|
137
|
Chris@0
|
138 // Continue after this class reference so that errors for this are not
|
Chris@0
|
139 // flagged multiple times.
|
Chris@0
|
140 return $phpcsFile->findNext([T_STRING, T_NS_SEPARATOR], ($stackPtr + 1), null, true);
|
Chris@0
|
141
|
Chris@0
|
142 }//end process()
|
Chris@0
|
143
|
Chris@0
|
144
|
Chris@0
|
145 }//end class
|