You are here

protected function VariableAnalysisSniff::checkForStaticDeclaration in Coder 8.2

Check is this is a static variable declaration.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile:

int $stackPtr:

string $varName:

string $currScope:

Return value

bool

1 call to VariableAnalysisSniff::checkForStaticDeclaration()
VariableAnalysisSniff::processVariable in coder_sniffer/DrupalPractice/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
Called to process normal member vars.

File

coder_sniffer/DrupalPractice/Sniffs/CodeAnalysis/VariableAnalysisSniff.php, line 1682

Class

VariableAnalysisSniff
Checks the for undefined function variables.

Namespace

DrupalPractice\Sniffs\CodeAnalysis

Code

protected function checkForStaticDeclaration(File $phpcsFile, $stackPtr, $varName, $currScope) {
  $tokens = $phpcsFile
    ->getTokens();

  // Are we a static declaration?
  // Static declarations are a bit more complicated than globals, since they
  // can contain assignments. The assignment is compile-time however so can
  // only be constant values, which makes life manageable.
  //
  // Just to complicate matters further, late static binding constants
  // take the form static::CONSTANT and are invalid within static variable
  // assignments, but we don't want to accidentally match their use of the
  // static keyword.
  //
  // Valid values are:
  // number         T_MINUS T_LNUMBER T_DNUMBER
  // string         T_CONSTANT_ENCAPSED_STRING
  // heredoc        T_START_HEREDOC T_HEREDOC T_END_HEREDOC
  // nowdoc         T_START_NOWDOC T_NOWDOC T_END_NOWDOC
  // define         T_STRING
  // class constant T_STRING T_DOUBLE_COLON T_STRING
  // Search backwards for first token that isn't whitespace, comma, variable,
  // equals, or on the list of assignable constant values above.
  $staticPtr = $phpcsFile
    ->findPrevious(array(
    T_WHITESPACE,
    T_VARIABLE,
    T_COMMA,
    T_EQUAL,
    T_MINUS,
    T_LNUMBER,
    T_DNUMBER,
    T_CONSTANT_ENCAPSED_STRING,
    T_STRING,
    T_DOUBLE_COLON,
    T_START_HEREDOC,
    T_HEREDOC,
    T_END_HEREDOC,
    T_START_NOWDOC,
    T_NOWDOC,
    T_END_NOWDOC,
  ), $stackPtr - 1, null, true, null, true);
  if ($staticPtr === false || $tokens[$staticPtr]['code'] !== T_STATIC) {

    // Debug code.
    // if ($varName == 'static4') {
    // echo "Failing token:\n" . print_r($tokens[$staticPtr], true);
    // }
    // End: Debug code.
    return false;
  }

  // Is it a late static binding static::?
  // If so, this isn't the static keyword we're looking for, but since
  // static:: isn't allowed in a compile-time constant, we also know
  // we can't be part of a static declaration anyway, so there's no
  // need to look any further.
  $lateStaticBindingPtr = $phpcsFile
    ->findNext(T_WHITESPACE, $staticPtr + 1, null, true, null, true);
  if ($lateStaticBindingPtr !== false && $tokens[$lateStaticBindingPtr]['code'] === T_DOUBLE_COLON) {
    return false;
  }

  // It's a static declaration.
  $this
    ->markVariableDeclaration($varName, 'static', null, $stackPtr, $currScope);
  if ($this
    ->isNextThingAnAssign($phpcsFile, $stackPtr) !== false) {
    $this
      ->markVariableAssignment($varName, $stackPtr, $currScope);
  }
  return true;
}