You are here

protected function Drupal_Sniffs_NamingConventions_ValidFunctionNameSniff::processTokenWithinScope in Coder 7.2

Processes the tokens within the scope.

Parameters

PHP_CodeSniffer_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 50

Class

Drupal_Sniffs_NamingConventions_ValidFunctionNameSniff
Drupal_Sniffs_NamingConventions_ValidFunctionNameSniff.

Code

protected function processTokenWithinScope(PHP_CodeSniffer_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 (in_array($magicPart, $this->magicMethods) === 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);
  $scope = $methodProps['scope'];
  $scopeSpecified = $methodProps['scope_specified'];

  // Methods should not contain underscores.
  if (strpos($methodName, '_') !== false) {
    if ($scopeSpecified === true) {
      $error = '%s method name "%s" is not in lowerCamel format, it must not contain underscores';
      $data = array(
        ucfirst($scope),
        $errorData[0],
      );
      $phpcsFile
        ->addError($error, $stackPtr, 'ScopeNotLowerCamel', $data);
    }
    else {
      $error = 'Method name "%s" is not in lowerCamel format, it must not contain underscores';
      $phpcsFile
        ->addError($error, $stackPtr, 'NotLowerCamel', $errorData);
    }
  }
}