You are here

protected function ValidVariableNameSniff::processMemberVar 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::processMemberVar()
  2. 8.3.x coder_sniffer/Drupal/Sniffs/NamingConventions/ValidVariableNameSniff.php \Drupal\Sniffs\NamingConventions\ValidVariableNameSniff::processMemberVar()

Processes class member variables.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.:

int $stackPtr The position of the current token: in the stack passed in $tokens.

Return value

void

File

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

Class

ValidVariableNameSniff
\Drupal\Sniffs\NamingConventions\ValidVariableNameSniff.

Namespace

Drupal\Sniffs\NamingConventions

Code

protected function processMemberVar(File $phpcsFile, $stackPtr) {
  $tokens = $phpcsFile
    ->getTokens();
  $memberProps = $phpcsFile
    ->getMemberProperties($stackPtr);
  if (empty($memberProps) === true) {
    return;
  }
  $memberName = ltrim($tokens[$stackPtr]['content'], '$');
  if (strpos($memberName, '_') === false) {
    return;
  }

  // Check if the class extends another class and get the name of the class
  // that is extended.
  if (empty($tokens[$stackPtr]['conditions']) === false) {
    $classPtr = key($tokens[$stackPtr]['conditions']);
    $extendsName = $phpcsFile
      ->findExtendedClassName($classPtr);

    // Special case config entities: those are allowed to have underscores in
    // their class property names. If a class extends something like
    // ConfigEntityBase then we consider it a config entity class and allow
    // underscores.
    if ($extendsName !== false && strpos($extendsName, 'ConfigEntity') !== false) {
      return;
    }
  }
  $error = 'Class property %s should use lowerCamel naming without underscores';
  $data = array(
    $tokens[$stackPtr]['content'],
  );
  $phpcsFile
    ->addError($error, $stackPtr, 'LowerCamelName', $data);
}