You are here

protected function Drupal_Sniffs_NamingConventions_ValidVariableNameSniff::processVariable in Coder 7.2

Processes normal variables.

Parameters

PHP_CodeSniffer_File $phpcsFile The file where this token was found.:

int $stackPtr The position where the token was found.:

Return value

void

File

coder_sniffer/Drupal/Sniffs/NamingConventions/ValidVariableNameSniff.php, line 64

Class

Drupal_Sniffs_NamingConventions_ValidVariableNameSniff
Drupal_Sniffs_NamingConventions_ValidVariableNameSniff.

Code

protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
  $tokens = $phpcsFile
    ->getTokens();
  $varName = ltrim($tokens[$stackPtr]['content'], '$');
  $phpReservedVars = array(
    '_SERVER',
    '_GET',
    '_POST',
    '_REQUEST',
    '_SESSION',
    '_ENV',
    '_COOKIE',
    '_FILES',
    'GLOBALS',
  );

  // If it's a php reserved var, then its ok.
  if (in_array($varName, $phpReservedVars) === true) {
    return;
  }

  // If it is a static public variable of a class, then its ok.
  if ($tokens[$stackPtr - 1]['code'] === T_DOUBLE_COLON) {
    return;
  }
  if (preg_match('/[A-Z]/', $varName)) {
    $error = "Variable \"{$varName}\" is camel caps format. do not use mixed case (camelCase), use lower case and _";
    $phpcsFile
      ->addError($error, $stackPtr);
  }
}