annotate vendor/drupal/coder/coder_sniffer/DrupalPractice/Sniffs/General/VariableNameSniff.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2 /**
Chris@17 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@17 10 namespace DrupalPractice\Sniffs\General;
Chris@17 11
Chris@17 12 use PHP_CodeSniffer\Files\File;
Chris@17 13 use Drupal\Sniffs\Semantics\FunctionCall;
Chris@17 14 use DrupalPractice\Project;
Chris@17 15 use PHP_CodeSniffer\Util\Tokens;
Chris@17 16
Chris@0 17 /**
Chris@0 18 * Checks the usage of variable_get() in forms and the variable name.
Chris@0 19 *
Chris@0 20 * @category PHP
Chris@0 21 * @package PHP_CodeSniffer
Chris@0 22 * @link http://pear.php.net/package/PHP_CodeSniffer
Chris@0 23 */
Chris@17 24 class VariableNameSniff extends FunctionCall
Chris@0 25 {
Chris@0 26
Chris@0 27
Chris@0 28 /**
Chris@0 29 * Returns an array of function names this test wants to listen for.
Chris@0 30 *
Chris@0 31 * @return array
Chris@0 32 */
Chris@0 33 public function registerFunctionNames()
Chris@0 34 {
Chris@0 35 return array('variable_get');
Chris@0 36
Chris@0 37 }//end registerFunctionNames()
Chris@0 38
Chris@0 39
Chris@0 40 /**
Chris@0 41 * Processes this function call.
Chris@0 42 *
Chris@17 43 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
Chris@17 44 * @param int $stackPtr The position of the function call in
Chris@17 45 * the stack.
Chris@17 46 * @param int $openBracket The position of the opening
Chris@17 47 * parenthesis in the stack.
Chris@17 48 * @param int $closeBracket The position of the closing
Chris@17 49 * parenthesis in the stack.
Chris@0 50 *
Chris@0 51 * @return void
Chris@0 52 */
Chris@0 53 public function processFunctionCall(
Chris@17 54 File $phpcsFile,
Chris@0 55 $stackPtr,
Chris@0 56 $openBracket,
Chris@0 57 $closeBracket
Chris@0 58 ) {
Chris@0 59 $tokens = $phpcsFile->getTokens();
Chris@0 60
Chris@0 61 // We assume that the sequence '#default_value' => variable_get(...)
Chris@0 62 // indicates a variable that the module owns.
Chris@17 63 $arrow = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
Chris@0 64 if ($arrow === false || $tokens[$arrow]['code'] !== T_DOUBLE_ARROW) {
Chris@0 65 return;
Chris@0 66 }
Chris@0 67
Chris@17 68 $arrayKey = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($arrow - 1), null, true);
Chris@0 69 if ($arrayKey === false
Chris@0 70 || $tokens[$arrayKey]['code'] !== T_CONSTANT_ENCAPSED_STRING
Chris@0 71 || substr($tokens[$arrayKey]['content'], 1, -1) !== '#default_value'
Chris@0 72 ) {
Chris@0 73 return;
Chris@0 74 }
Chris@0 75
Chris@0 76 $argument = $this->getArgument(1);
Chris@0 77
Chris@0 78 // Variable name is not a literal string, so we return early.
Chris@0 79 if ($argument === false || $tokens[$argument['start']]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
Chris@0 80 return;
Chris@0 81 }
Chris@0 82
Chris@17 83 $moduleName = Project::getName($phpcsFile);
Chris@0 84 if ($moduleName === false) {
Chris@0 85 return;
Chris@0 86 }
Chris@0 87
Chris@0 88 $variableName = substr($tokens[$argument['start']]['content'], 1, -1);
Chris@0 89 if (strpos($variableName, $moduleName) !== 0) {
Chris@0 90 $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 91 $data = array(
Chris@0 92 $moduleName,
Chris@0 93 $variableName,
Chris@0 94 );
Chris@0 95 $phpcsFile->addWarning($warning, $argument['start'], 'VariableName', $data);
Chris@0 96 }
Chris@0 97
Chris@0 98 }//end processFunctionCall()
Chris@0 99
Chris@0 100
Chris@0 101 }//end class