Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@0
|
3 * DrupalPractice_Sniffs_General_VariableNameSniff
|
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 the usage of variable_get() in forms and the variable name.
|
Chris@0
|
12 *
|
Chris@0
|
13 * @category PHP
|
Chris@0
|
14 * @package PHP_CodeSniffer
|
Chris@0
|
15 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
16 */
|
Chris@0
|
17 class DrupalPractice_Sniffs_General_VariableNameSniff extends Drupal_Sniffs_Semantics_FunctionCall
|
Chris@0
|
18 {
|
Chris@0
|
19
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * Returns an array of function names this test wants to listen for.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @return array
|
Chris@0
|
25 */
|
Chris@0
|
26 public function registerFunctionNames()
|
Chris@0
|
27 {
|
Chris@0
|
28 return array('variable_get');
|
Chris@0
|
29
|
Chris@0
|
30 }//end registerFunctionNames()
|
Chris@0
|
31
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * Processes this function call.
|
Chris@0
|
35 *
|
Chris@0
|
36 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
Chris@0
|
37 * @param int $stackPtr The position of the function call in
|
Chris@0
|
38 * the stack.
|
Chris@0
|
39 * @param int $openBracket The position of the opening
|
Chris@0
|
40 * parenthesis in the stack.
|
Chris@0
|
41 * @param int $closeBracket The position of the closing
|
Chris@0
|
42 * parenthesis in the stack.
|
Chris@0
|
43 *
|
Chris@0
|
44 * @return void
|
Chris@0
|
45 */
|
Chris@0
|
46 public function processFunctionCall(
|
Chris@0
|
47 PHP_CodeSniffer_File $phpcsFile,
|
Chris@0
|
48 $stackPtr,
|
Chris@0
|
49 $openBracket,
|
Chris@0
|
50 $closeBracket
|
Chris@0
|
51 ) {
|
Chris@0
|
52 $tokens = $phpcsFile->getTokens();
|
Chris@0
|
53
|
Chris@0
|
54 // We assume that the sequence '#default_value' => variable_get(...)
|
Chris@0
|
55 // indicates a variable that the module owns.
|
Chris@0
|
56 $arrow = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true);
|
Chris@0
|
57 if ($arrow === false || $tokens[$arrow]['code'] !== T_DOUBLE_ARROW) {
|
Chris@0
|
58 return;
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 $arrayKey = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($arrow - 1), null, true);
|
Chris@0
|
62 if ($arrayKey === false
|
Chris@0
|
63 || $tokens[$arrayKey]['code'] !== T_CONSTANT_ENCAPSED_STRING
|
Chris@0
|
64 || substr($tokens[$arrayKey]['content'], 1, -1) !== '#default_value'
|
Chris@0
|
65 ) {
|
Chris@0
|
66 return;
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 $argument = $this->getArgument(1);
|
Chris@0
|
70
|
Chris@0
|
71 // Variable name is not a literal string, so we return early.
|
Chris@0
|
72 if ($argument === false || $tokens[$argument['start']]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
|
Chris@0
|
73 return;
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 $moduleName = DrupalPractice_Project::getName($phpcsFile);
|
Chris@0
|
77 if ($moduleName === false) {
|
Chris@0
|
78 return;
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 $variableName = substr($tokens[$argument['start']]['content'], 1, -1);
|
Chris@0
|
82 if (strpos($variableName, $moduleName) !== 0) {
|
Chris@0
|
83 $warning = 'All variables defined by your module must be prefixed with your module\'s name to avoid name collisions with others. Expected start with "%s" but found "%s"';
|
Chris@0
|
84 $data = array(
|
Chris@0
|
85 $moduleName,
|
Chris@0
|
86 $variableName,
|
Chris@0
|
87 );
|
Chris@0
|
88 $phpcsFile->addWarning($warning, $argument['start'], 'VariableName', $data);
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 }//end processFunctionCall()
|
Chris@0
|
92
|
Chris@0
|
93
|
Chris@0
|
94 }//end class
|