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

Breadcrumb

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

function ValidFunctionNameSniff::processTokenWithinScope

Same name in this branch
  1. 11.1.x vendor/drupal/coder/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

Overrides AbstractScopeSniff::processTokenWithinScope

File

vendor/squizlabs/php_codesniffer/src/Standards/PEAR/Sniffs/NamingConventions/ValidFunctionNameSniff.php, line 73

Class

ValidFunctionNameSniff

Namespace

PHP_CodeSniffer\Standards\PEAR\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) {
        // Ignore closures.
        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) {
            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;
    }
    $methodProps = $phpcsFile->getMethodProperties($stackPtr);
    $scope = $methodProps['scope'];
    $scopeSpecified = $methodProps['scope_specified'];
    if ($methodProps['scope'] === 'private') {
        $isPublic = false;
    }
    else {
        $isPublic = true;
    }
    // If it's a private method, it must have an underscore on the front.
    if ($isPublic === false) {
        if ($methodName[0] !== '_') {
            $error = 'Private method name "%s" must be prefixed with an underscore';
            $phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $errorData);
            $phpcsFile->recordMetric($stackPtr, 'Private method prefixed with underscore', 'no');
        }
        else {
            $phpcsFile->recordMetric($stackPtr, 'Private method prefixed with underscore', 'yes');
        }
    }
    // If it's not a private method, it must not have an underscore on the front.
    if ($isPublic === true && $scopeSpecified === true && $methodName[0] === '_') {
        $error = '%s method name "%s" must not be prefixed with an underscore';
        $data = [
            ucfirst($scope),
            $errorData[0],
        ];
        $phpcsFile->addError($error, $stackPtr, 'PublicUnderscore', $data);
    }
    $testMethodName = ltrim($methodName, '_');
    if (Common::isCamelCaps($testMethodName, false, true, false) === false) {
        if ($scopeSpecified === true) {
            $error = '%s method name "%s" is not in camel caps format';
            $data = [
                ucfirst($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);
        }
    }
}

API Navigation

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