You are here

protected function PropertyDeclarationSniff::processMemberVar in Coder 8.2

Same name and namespace in other branches
  1. 8.3 coder_sniffer/Drupal/Sniffs/Classes/PropertyDeclarationSniff.php \Drupal\Sniffs\Classes\PropertyDeclarationSniff::processMemberVar()
  2. 8.3.x coder_sniffer/Drupal/Sniffs/Classes/PropertyDeclarationSniff.php \Drupal\Sniffs\Classes\PropertyDeclarationSniff::processMemberVar()

Processes the function tokens within the class.

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/Classes/PropertyDeclarationSniff.php, line 37

Class

PropertyDeclarationSniff
Laregely copied from \PHP_CodeSniffer\Standards\PSR2\Sniffs\Classes\PropertyDeclarationSniff to have a fixer for the var keyword.

Namespace

Drupal\Sniffs\Classes

Code

protected function processMemberVar(File $phpcsFile, $stackPtr) {
  $tokens = $phpcsFile
    ->getTokens();
  if ($tokens[$stackPtr]['content'][1] === '_') {
    $error = 'Property name "%s" should not be prefixed with an underscore to indicate visibility';
    $data = array(
      $tokens[$stackPtr]['content'],
    );
    $phpcsFile
      ->addWarning($error, $stackPtr, 'Underscore', $data);
  }

  // Detect multiple properties defined at the same time. Throw an error
  // for this, but also only process the first property in the list so we don't
  // repeat errors.
  $find = Tokens::$scopeModifiers;
  $find = array_merge($find, array(
    T_VARIABLE,
    T_VAR,
    T_SEMICOLON,
  ));
  $prev = $phpcsFile
    ->findPrevious($find, $stackPtr - 1);
  if ($tokens[$prev]['code'] === T_VARIABLE) {
    return;
  }
  if ($tokens[$prev]['code'] === T_VAR) {
    $error = 'The var keyword must not be used to declare a property';
    $fix = $phpcsFile
      ->addFixableError($error, $stackPtr, 'VarUsed');
    if ($fix === true) {
      $phpcsFile->fixer
        ->replaceToken($prev, 'public');
    }
  }
  $next = $phpcsFile
    ->findNext(array(
    T_VARIABLE,
    T_SEMICOLON,
  ), $stackPtr + 1);
  if ($tokens[$next]['code'] === T_VARIABLE) {
    $error = 'There must not be more than one property declared per statement';
    $phpcsFile
      ->addError($error, $stackPtr, 'Multiple');
  }
  $modifier = $phpcsFile
    ->findPrevious(Tokens::$scopeModifiers, $stackPtr);
  if ($modifier === false || $tokens[$modifier]['line'] !== $tokens[$stackPtr]['line']) {
    $error = 'Visibility must be declared on property "%s"';
    $data = array(
      $tokens[$stackPtr]['content'],
    );
    $phpcsFile
      ->addError($error, $stackPtr, 'ScopeMissing', $data);
  }
}