comparison vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Semantics/InstallHooksSniff.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2 /**
3 * Drupal_Sniffs_Semantics_InstallHooksSniff
4 *
5 * @category PHP
6 * @package PHP_CodeSniffer
7 * @link http://pear.php.net/package/PHP_CodeSniffer
8 */
9
10 /**
11 * Checks that hook_disable(), hook_enable(), hook_install(), hook_uninstall(),
12 * hook_requirements() and hook_schema() are not defined in the module file.
13 *
14 * @category PHP
15 * @package PHP_CodeSniffer
16 * @link http://pear.php.net/package/PHP_CodeSniffer
17 */
18 class Drupal_Sniffs_Semantics_InstallHooksSniff extends Drupal_Sniffs_Semantics_FunctionDefinition
19 {
20
21
22 /**
23 * Process this function definition.
24 *
25 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
26 * @param int $stackPtr The position of the function name in the stack.
27 * name in the stack.
28 * @param int $functionPtr The position of the function keyword in the stack.
29 * keyword in the stack.
30 *
31 * @return void
32 */
33 public function processFunction(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $functionPtr)
34 {
35 $fileExtension = strtolower(substr($phpcsFile->getFilename(), -6));
36 // Only check in *.module files.
37 if ($fileExtension !== 'module') {
38 return;
39 }
40
41 $tokens = $phpcsFile->getTokens();
42
43 $fileName = substr(basename($phpcsFile->getFilename()), 0, -7);
44 if ($tokens[$stackPtr]['content'] === ($fileName.'_install')
45 || $tokens[$stackPtr]['content'] === ($fileName.'_uninstall')
46 || $tokens[$stackPtr]['content'] === ($fileName.'_requirements')
47 || $tokens[$stackPtr]['content'] === ($fileName.'_schema')
48 || $tokens[$stackPtr]['content'] === ($fileName.'_enable')
49 || $tokens[$stackPtr]['content'] === ($fileName.'_disable')
50 ) {
51 $error = '%s() is an installation hook and must be declared in an install file';
52 $data = array($tokens[$stackPtr]['content']);
53 $phpcsFile->addError($error, $stackPtr, 'InstallHook', $data);
54 }
55
56 }//end processFunction()
57
58
59 }//end class