/** * Processes this function call. * * @param PHP_CodeSniffer_File $phpcsFile * The file being scanned. * @param int $stackPtr * The position of the function call in the stack. * @param int $openBracket * The position of the opening parenthesis in the stack. * @param int $closeBracket * The position of the closing parenthesis in the stack. * @param Drupal_Sniffs_Semantics_FunctionCallSniff $sniff * Can be used to retreive the function's arguments with the getArgument() * method. * * @return void */ public function processFunctionCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $closeBracket, Drupal_Sniffs_Semantics_FunctionCallSniff $sniff) { $tokens = $phpcsFile->getTokens(); // We assume that the sequence '#default_value' => variable_get(...) // indicates a variable that the module owns. $arrow = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr - 1, null, true); if ($arrow === false || $tokens[$arrow]['code'] !== T_DOUBLE_ARROW) { return; } $arrayKey = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $arrow - 1, null, true); if ($arrayKey === false || $tokens[$arrayKey]['code'] !== T_CONSTANT_ENCAPSED_STRING || substr($tokens[$arrayKey]['content'], 1, -1) !== '#default_value') { return; } $argument = $sniff->getArgument(1); // Variable name is not a literal string, so we return early. if ($argument === false || $tokens[$argument['start']]['code'] !== T_CONSTANT_ENCAPSED_STRING) { return; } $moduleName = DrupalPractice_Project::getName($phpcsFile); if ($moduleName === false) { return; } $variableName = substr($tokens[$argument['start']]['content'], 1, -1); if (strpos($variableName, $moduleName) !== 0) { $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"'; $data = array($moduleName, $variableName); $phpcsFile->addWarning($warning, $argument['start'], 'VariableName', $data); } }
/** * Processes this test, when one of its tokens is encountered. * * @param PHP_CodeSniffer_File $phpcsFile The current file being processed. * @param int $stackPtr The position of the current token * in the stack passed in $tokens. * * @return void */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { // If there is a PHP 5.3 namespace declaration in the file we return // immediately as classes can be named arbitrary within a namespace. $namespace = $phpcsFile->findPrevious(T_NAMESPACE, $stackPtr - 1); if ($namespace !== false) { return; } $moduleName = DrupalPractice_Project::getName($phpcsFile); if ($moduleName === false) { return; } $tokens = $phpcsFile->getTokens(); $className = $phpcsFile->findNext(T_STRING, $stackPtr); $name = trim($tokens[$className]['content']); // Underscores are omitted in class names. Also convert all characters // to lower case to compare them later. $classPrefix = strtolower(str_replace('_', '', $moduleName)); // Views classes might have underscores in the name, which is also fine. $viewsPrefix = strtolower($moduleName); $name = strtolower($name); if (strpos($name, $classPrefix) !== 0 && strpos($name, $viewsPrefix) !== 0) { $warning = '%s name must be prefixed with the project name "%s"'; $nameParts = explode('_', $moduleName); $camelName = ''; foreach ($nameParts as &$part) { $camelName .= ucfirst($part); } $errorData = array(ucfirst($tokens[$stackPtr]['content']), $camelName); $phpcsFile->addWarning($warning, $className, 'ClassPrefix', $errorData); } }