Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@0
|
3 * Drupal_Sniffs_Semantics_ConstantNameSniff
|
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 constants introduced with define() in module or install files start
|
Chris@0
|
12 * with the module's name.
|
Chris@0
|
13 *
|
Chris@0
|
14 * @category PHP
|
Chris@0
|
15 * @package PHP_CodeSniffer
|
Chris@0
|
16 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
17 */
|
Chris@0
|
18 class Drupal_Sniffs_Semantics_ConstantNameSniff extends Drupal_Sniffs_Semantics_FunctionCall
|
Chris@0
|
19 {
|
Chris@0
|
20
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Returns an array of function names this test wants to listen for.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @return array
|
Chris@0
|
26 */
|
Chris@0
|
27 public function registerFunctionNames()
|
Chris@0
|
28 {
|
Chris@0
|
29 return array('define');
|
Chris@0
|
30
|
Chris@0
|
31 }//end registerFunctionNames()
|
Chris@0
|
32
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * Processes this function call.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
Chris@0
|
38 * @param int $stackPtr The position of the function call in
|
Chris@0
|
39 * the stack.
|
Chris@0
|
40 * @param int $openBracket The position of the opening
|
Chris@0
|
41 * parenthesis in the stack.
|
Chris@0
|
42 * @param int $closeBracket The position of the closing
|
Chris@0
|
43 * parenthesis in the stack.
|
Chris@0
|
44 *
|
Chris@0
|
45 * @return void
|
Chris@0
|
46 */
|
Chris@0
|
47 public function processFunctionCall(
|
Chris@0
|
48 PHP_CodeSniffer_File $phpcsFile,
|
Chris@0
|
49 $stackPtr,
|
Chris@0
|
50 $openBracket,
|
Chris@0
|
51 $closeBracket
|
Chris@0
|
52 ) {
|
Chris@0
|
53 $nameParts = explode('.', basename($phpcsFile->getFilename()));
|
Chris@0
|
54 $fileExtension = end($nameParts);
|
Chris@0
|
55 // Only check in *.module files.
|
Chris@0
|
56 if ($fileExtension !== 'module' && $fileExtension !== 'install') {
|
Chris@0
|
57 return;
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 $tokens = $phpcsFile->getTokens();
|
Chris@0
|
61 $argument = $this->getArgument(1);
|
Chris@0
|
62 if ($tokens[$argument['start']]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
|
Chris@0
|
63 // Not a string literal, so this is some obscure constant that we ignore.
|
Chris@0
|
64 return;
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 $moduleName = reset($nameParts);
|
Chris@0
|
68 $expectedStart = strtoupper($moduleName);
|
Chris@0
|
69 // Remove the quotes around the string litral.
|
Chris@0
|
70 $constant = substr($tokens[$argument['start']]['content'], 1, -1);
|
Chris@0
|
71 if (strpos($constant, $expectedStart) !== 0) {
|
Chris@0
|
72 $warning = 'All constants defined by a module must be prefixed with the module\'s name, expected "%s" but found "%s"';
|
Chris@0
|
73 $data = array(
|
Chris@0
|
74 $expectedStart."_$constant",
|
Chris@0
|
75 $constant,
|
Chris@0
|
76 );
|
Chris@0
|
77 $phpcsFile->addWarning($warning, $stackPtr, 'ConstantStart', $data);
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 }//end processFunctionCall()
|
Chris@0
|
81
|
Chris@0
|
82
|
Chris@0
|
83 }//end class
|