comparison vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Semantics/EmptyInstallSniff.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_EmptyInstallSniff.
4 *
5 * @category PHP
6 * @package PHP_CodeSniffer
7 * @link http://pear.php.net/package/PHP_CodeSniffer
8 */
9
10 /**
11 * Throws an error if hook_install() or hook_uninstall() definitions are empty.
12 *
13 * @category PHP
14 * @package PHP_CodeSniffer
15 * @link http://pear.php.net/package/PHP_CodeSniffer
16 */
17 class Drupal_Sniffs_Semantics_EmptyInstallSniff extends Drupal_Sniffs_Semantics_FunctionDefinition
18 {
19
20
21 /**
22 * Process this function definition.
23 *
24 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
25 * @param int $stackPtr The position of the function name in the stack.
26 * name in the stack.
27 * @param int $functionPtr The position of the function keyword in the stack.
28 * keyword in the stack.
29 *
30 * @return void
31 */
32 public function processFunction(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $functionPtr)
33 {
34 $fileExtension = strtolower(substr($phpcsFile->getFilename(), -7));
35 // Only check in *.install files.
36 if ($fileExtension !== 'install') {
37 return;
38 }
39
40 $fileName = substr(basename($phpcsFile->getFilename()), 0, -8);
41 $tokens = $phpcsFile->getTokens();
42 if ($tokens[$stackPtr]['content'] === ($fileName.'_install')
43 || $tokens[$stackPtr]['content'] === ($fileName.'_uninstall')
44 ) {
45 // Check if there is a function body.
46 $bodyPtr = $phpcsFile->findNext(
47 PHP_CodeSniffer_Tokens::$emptyTokens,
48 ($tokens[$functionPtr]['scope_opener'] + 1),
49 $tokens[$functionPtr]['scope_closer'],
50 true
51 );
52 if ($bodyPtr === false) {
53 $error = 'Empty installation hooks are not necessary';
54 $phpcsFile->addError($error, $stackPtr, 'EmptyInstall');
55 }
56 }
57
58 }//end processFunction()
59
60
61 }//end class