You are here

protected function UnusedPrivateMethodSniff::processTokenWithinScope in Coder 8.2

Same name and namespace in other branches
  1. 8.3 coder_sniffer/DrupalPractice/Sniffs/Objects/UnusedPrivateMethodSniff.php \DrupalPractice\Sniffs\Objects\UnusedPrivateMethodSniff::processTokenWithinScope()
  2. 8.3.x coder_sniffer/DrupalPractice/Sniffs/Objects/UnusedPrivateMethodSniff.php \DrupalPractice\Sniffs\Objects\UnusedPrivateMethodSniff::processTokenWithinScope()

Processes the tokens within the scope.

Parameters

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

int $stackPtr The position where this token was: found.

int $currScope The position of the current scope.:

Return value

void

File

coder_sniffer/DrupalPractice/Sniffs/Objects/UnusedPrivateMethodSniff.php, line 47

Class

UnusedPrivateMethodSniff
Checks that private methods are actually used in a class.

Namespace

DrupalPractice\Sniffs\Objects

Code

protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope) {

  // Only check private methods.
  $methodProperties = $phpcsFile
    ->getMethodProperties($stackPtr);
  if ($methodProperties['scope'] !== 'private' || $methodProperties['is_static'] === true) {
    return;
  }
  $tokens = $phpcsFile
    ->getTokens();
  $methodName = $phpcsFile
    ->getDeclarationName($stackPtr);
  $classPtr = key($tokens[$stackPtr]['conditions']);

  // Search for direct $this->methodCall() or indirect callbacks [$this,
  // 'methodCall'].
  $current = $tokens[$classPtr]['scope_opener'];
  $end = $tokens[$classPtr]['scope_closer'];
  while (($current = $phpcsFile
    ->findNext(T_VARIABLE, $current + 1, $end)) !== false) {
    if ($tokens[$current]['content'] !== '$this') {
      continue;
    }
    $next = $phpcsFile
      ->findNext(Tokens::$emptyTokens, $current + 1, null, true);
    if ($next === false) {
      continue;
    }
    if ($tokens[$next]['code'] === T_OBJECT_OPERATOR) {
      $call = $phpcsFile
        ->findNext(Tokens::$emptyTokens, $next + 1, null, true);
      if ($call === false || $tokens[$call]['content'] !== $methodName) {
        continue;
      }
      $parenthesis = $phpcsFile
        ->findNext(Tokens::$emptyTokens, $call + 1, null, true);
      if ($parenthesis === false || $tokens[$parenthesis]['code'] !== T_OPEN_PARENTHESIS) {
        continue;
      }

      // At this point this is a method call to the private method, so we
      // can stop.
      return;
    }
    else {
      if ($tokens[$next]['code'] === T_COMMA) {
        $call = $phpcsFile
          ->findNext(Tokens::$emptyTokens, $next + 1, null, true);
        if ($call === false || substr($tokens[$call]['content'], 1, -1) !== $methodName) {
          continue;
        }

        // At this point this is likely the private method as callback on a
        // function such as array_filter().
        return;
      }
    }

    //end if
  }

  //end while
  $warning = 'Unused private method %s()';
  $data = [
    $methodName,
  ];
  $phpcsFile
    ->addWarning($warning, $stackPtr, 'UnusedMethod', $data);
}