You are here

protected function ValidFunctionNameSniff::processTokenOutsideScope 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::processTokenOutsideScope()
  2. 8.3.x coder_sniffer/Drupal/Sniffs/NamingConventions/ValidFunctionNameSniff.php \Drupal\Sniffs\NamingConventions\ValidFunctionNameSniff::processTokenOutsideScope()

Processes the tokens outside the scope.

Parameters

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

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

Return value

void

File

coder_sniffer/Drupal/Sniffs/NamingConventions/ValidFunctionNameSniff.php, line 98

Class

ValidFunctionNameSniff
\Drupal\Sniffs\NamingConventions\ValidFunctionNameSniff.

Namespace

Drupal\Sniffs\NamingConventions

Code

protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) {
  $functionName = $phpcsFile
    ->getDeclarationName($stackPtr);
  if ($functionName === null) {

    // Ignore closures.
    return;
  }
  $isApiFile = substr($phpcsFile
    ->getFilename(), -8) === '.api.php';
  $isHookExample = substr($functionName, 0, 5) === 'hook_';
  if ($isApiFile === true && $isHookExample === true) {

    // Ignore for examaple hook_ENTITY_TYPE_insert() functions in .api.php
    // files.
    return;
  }
  if ($functionName !== strtolower($functionName)) {
    $expected = strtolower(preg_replace('/([^_])([A-Z])/', '$1_$2', $functionName));
    $error = 'Invalid function name, expected %s but found %s';
    $data = array(
      $expected,
      $functionName,
    );
    $phpcsFile
      ->addError($error, $stackPtr, 'InvalidName', $data);
  }
}