You are here

protected function ValidVariableNameSniff::processVariable in Coder 8.2

Same name and namespace in other branches
  1. 8.3 coder_sniffer/Drupal/Sniffs/NamingConventions/ValidVariableNameSniff.php \Drupal\Sniffs\NamingConventions\ValidVariableNameSniff::processVariable()
  2. 8.3.x coder_sniffer/Drupal/Sniffs/NamingConventions/ValidVariableNameSniff.php \Drupal\Sniffs\NamingConventions\ValidVariableNameSniff::processVariable()

Processes normal variables.

Parameters

\PHP_CodeSniffer\Files\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 82

Class

ValidVariableNameSniff
\Drupal\Sniffs\NamingConventions\ValidVariableNameSniff.

Namespace

Drupal\Sniffs\NamingConventions

Code

protected function processVariable(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) === 1) {
    $error = "Variable \"{$varName}\" starts with a capital letter, but only \$lowerCamelCase or \$snake_case is allowed";
    $phpcsFile
      ->addError($error, $stackPtr, 'LowerStart');
  }
}