Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. CamelCapsFunctionNameSniff.php

function CamelCapsFunctionNameSniff::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

Overrides AbstractScopeSniff::processTokenWithinScope

2 methods override CamelCapsFunctionNameSniff::processTokenWithinScope()
CamelCapsMethodNameSniff::processTokenWithinScope in vendor/squizlabs/php_codesniffer/src/Standards/PSR1/Sniffs/Methods/CamelCapsMethodNameSniff.php
Processes the tokens within the scope.
ValidFunctionNameSniff::processTokenWithinScope in vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidFunctionNameSniff.php
Processes the tokens within the scope.

File

vendor/squizlabs/php_codesniffer/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php, line 102

Class

CamelCapsFunctionNameSniff

Namespace

PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions

Code

protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope) {
    $tokens = $phpcsFile->getTokens();
    // Determine if this is a function which needs to be examined.
    $conditions = $tokens[$stackPtr]['conditions'];
    end($conditions);
    $deepestScope = key($conditions);
    if ($deepestScope !== $currScope) {
        return;
    }
    $methodName = $phpcsFile->getDeclarationName($stackPtr);
    if ($methodName === null) {
        // Live coding or parse error. Bow out.
        return;
    }
    $className = $phpcsFile->getDeclarationName($currScope);
    if (isset($className) === false) {
        $className = '[Anonymous Class]';
    }
    $errorData = [
        $className . '::' . $methodName,
    ];
    $methodNameLc = strtolower($methodName);
    $classNameLc = strtolower($className);
    // Is this a magic method. i.e., is prefixed with "__" ?
    if (preg_match('|^__[^_]|', $methodName) !== 0) {
        $magicPart = substr($methodNameLc, 2);
        if (isset($this->magicMethods[$magicPart]) === true || isset($this->methodsDoubleUnderscore[$magicPart]) === true) {
            return;
        }
        $error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
        $phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData);
    }
    // PHP4 constructors are allowed to break our rules.
    if ($methodNameLc === $classNameLc) {
        return;
    }
    // PHP4 destructors are allowed to break our rules.
    if ($methodNameLc === '_' . $classNameLc) {
        return;
    }
    // Ignore leading underscores in the method name.
    $methodName = ltrim($methodName, '_');
    $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 camel caps format';
            $data = [
                ucfirst($methodProps['scope']),
                $errorData[0],
            ];
            $phpcsFile->addError($error, $stackPtr, 'ScopeNotCamelCaps', $data);
        }
        else {
            $error = 'Method name "%s" is not in camel caps format';
            $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData);
        }
        $phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'no');
    }
    else {
        $phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'yes');
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal