annotate vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Classes/FullyQualifiedNamespaceSniff.php @ 19:fa3358dc1485 tip

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