|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * \Drupal\Sniffs\Attributes\ValidHookNameSniff. |
| 4 | + * |
| 5 | + * @category PHP |
| 6 | + * @package PHP_CodeSniffer |
| 7 | + * @link http://pear.php.net/package/PHP_CodeSniffer |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Drupal\Sniffs\Attributes; |
| 11 | + |
| 12 | +use PHP_CodeSniffer\Files\File; |
| 13 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 14 | + |
| 15 | +/** |
| 16 | + * Checks that Hook attribute argument name not starts with "hook_" prefix. |
| 17 | + * |
| 18 | + * @category PHP |
| 19 | + * @package PHP_CodeSniffer |
| 20 | + * @link http://pear.php.net/package/PHP_CodeSniffer |
| 21 | + */ |
| 22 | +class ValidHookNameSniff implements Sniff |
| 23 | +{ |
| 24 | + |
| 25 | + |
| 26 | + /** |
| 27 | + * Returns an array of tokens this test wants to listen for. |
| 28 | + * |
| 29 | + * @return array<int|string> |
| 30 | + */ |
| 31 | + public function register() |
| 32 | + { |
| 33 | + return [T_ATTRIBUTE]; |
| 34 | + |
| 35 | + }//end register() |
| 36 | + |
| 37 | + |
| 38 | + /** |
| 39 | + * Processes this test, when one of its tokens is encountered. |
| 40 | + * |
| 41 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where the |
| 42 | + * token was found. |
| 43 | + * @param int $stackPtr The position in the PHP_CodeSniffer |
| 44 | + * file's token stack where the token |
| 45 | + * was found. |
| 46 | + * |
| 47 | + * @return void|int Optionally returns a stack pointer. The sniff will not be |
| 48 | + * called again on the current file until the returned stack |
| 49 | + * pointer is reached. Return $phpcsFile->numTokens + 1 to skip |
| 50 | + * the rest of the file. |
| 51 | + */ |
| 52 | + public function process(File $phpcsFile, $stackPtr) |
| 53 | + { |
| 54 | + $tokens = $phpcsFile->getTokens(); |
| 55 | + $attributeName = $phpcsFile->findNext(T_STRING, ($stackPtr + 1)); |
| 56 | + if ($attributeName !== false |
| 57 | + && $tokens[$attributeName]['content'] === 'Hook' |
| 58 | + ) { |
| 59 | + $hookName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($attributeName + 2)); |
| 60 | + if ($hookName !== false) { |
| 61 | + // Remove outer quotes. |
| 62 | + $hookNameValue = trim($tokens[$hookName]['content'], '"\''); |
| 63 | + |
| 64 | + if (strpos($hookNameValue, 'hook_') === 0 && $hookNameValue !== 'hook_') { |
| 65 | + // Remove "hook_" prefix. |
| 66 | + $hookNameValueFixed = substr($hookNameValue, 5); |
| 67 | + $message = sprintf("The hook name should not start with 'hook_', expected '%s' but found '%s'", $hookNameValueFixed, $hookNameValue); |
| 68 | + |
| 69 | + $fix = $phpcsFile->addFixableWarning($message, $hookName, 'HookPrefix'); |
| 70 | + if ($fix === true) { |
| 71 | + // Return outer quotes. |
| 72 | + $hookNameValueFixed = str_replace($hookNameValue, $hookNameValueFixed, $tokens[$hookName]['content']); |
| 73 | + $phpcsFile->fixer->replaceToken($hookName, $hookNameValueFixed); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + }//end if |
| 78 | + |
| 79 | + }//end process() |
| 80 | + |
| 81 | + |
| 82 | +}//end class |
0 commit comments