You are here

protected function ValidFunctionNameSniff::processTokenWithinScope in Coder 8.2

Same name and namespace in other branches
  1. 8.3 coder_sniffer/Drupal/Sniffs/NamingConventions/ValidFunctionNameSniff.php \Drupal\Sniffs\NamingConventions\ValidFunctionNameSniff::processTokenWithinScope()
  2. 8.3.x coder_sniffer/Drupal/Sniffs/NamingConventions/ValidFunctionNameSniff.php \Drupal\Sniffs\NamingConventions\ValidFunctionNameSniff::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/Drupal/Sniffs/NamingConventions/ValidFunctionNameSniff.php, line 42

Class

ValidFunctionNameSniff
\Drupal\Sniffs\NamingConventions\ValidFunctionNameSniff.

Namespace

Drupal\Sniffs\NamingConventions

Code

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

    // Ignore closures.
    return;
  }
  $className = $phpcsFile
    ->getDeclarationName($currScope);
  $errorData = array(
    $className . '::' . $methodName,
  );

  // Is this a magic method. i.e., is prefixed with "__" ?
  if (preg_match('|^__|', $methodName) !== 0) {
    $magicPart = strtolower(substr($methodName, 2));
    if (isset($this->magicMethods[$magicPart]) === false && isset($this->methodsDoubleUnderscore[$magicPart]) === false) {
      $error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
      $phpcsFile
        ->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData);
    }
    return;
  }
  $methodProps = $phpcsFile
    ->getMethodProperties($stackPtr);
  if (Common::isCamelCaps($methodName, false, true, $this->strict) === false) {
    if ($methodProps['scope_specified'] === true) {
      $error = '%s method name "%s" is not in lowerCamel format';
      $data = array(
        ucfirst($methodProps['scope']),
        $errorData[0],
      );
      $phpcsFile
        ->addError($error, $stackPtr, 'ScopeNotCamelCaps', $data);
    }
    else {
      $error = 'Method name "%s" is not in lowerCamel format';
      $phpcsFile
        ->addError($error, $stackPtr, 'NotCamelCaps', $errorData);
    }
    $phpcsFile
      ->recordMetric($stackPtr, 'CamelCase method name', 'no');
    return;
  }
  else {
    $phpcsFile
      ->recordMetric($stackPtr, 'CamelCase method name', 'yes');
  }
}