You are here

protected function MethodScopeSniff::processTokenWithinScope in Coder 8.2

Same name and namespace in other branches
  1. 8.3 coder_sniffer/Drupal/Sniffs/Scope/MethodScopeSniff.php \Drupal\Sniffs\Scope\MethodScopeSniff::processTokenWithinScope()
  2. 8.3.x coder_sniffer/Drupal/Sniffs/Scope/MethodScopeSniff.php \Drupal\Sniffs\Scope\MethodScopeSniff::processTokenWithinScope()

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.:

int $currScope The current scope opener token.:

Return value

void

File

coder_sniffer/Drupal/Sniffs/Scope/MethodScopeSniff.php, line 51

Class

MethodScopeSniff
Verifies that class/interface/trait methods have scope modifiers.

Namespace

Drupal\Sniffs\Scope

Code

protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope) {
  $tokens = $phpcsFile
    ->getTokens();
  $methodName = $phpcsFile
    ->getDeclarationName($stackPtr);
  if ($methodName === null) {

    // Ignore closures.
    return;
  }
  if ($phpcsFile
    ->hasCondition($stackPtr, T_FUNCTION) === true) {

    // Ignore nested functions.
    return;
  }
  $modifier = null;
  for ($i = $stackPtr - 1; $i > 0; $i--) {
    if ($tokens[$i]['line'] < $tokens[$stackPtr]['line']) {
      break;
    }
    else {
      if (isset(Tokens::$scopeModifiers[$tokens[$i]['code']]) === true) {
        $modifier = $i;
        break;
      }
    }
  }
  if ($modifier === null) {
    $error = 'Visibility must be declared on method "%s"';
    $data = array(
      $methodName,
    );
    $fix = $phpcsFile
      ->addFixableError($error, $stackPtr, 'Missing', $data);
    if ($fix === true) {

      // No scope modifier means the method is public in PHP, fix that
      // to be explicitly public.
      $phpcsFile->fixer
        ->addContentBefore($stackPtr, 'public ');
    }
  }
}