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

Breadcrumb

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

function ConstructorNameSniff::processTokenWithinScope

Processes this test when one of its tokens is encountered.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The current file being scanned.:

int $stackPtr The position of the current token: in the stack passed in $tokens.

int $currScope A pointer to the start of the scope.:

Return value

void

Overrides AbstractScopeSniff::processTokenWithinScope

File

vendor/squizlabs/php_codesniffer/src/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php, line 58

Class

ConstructorNameSniff

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;
    }
    $className = $phpcsFile->getDeclarationName($currScope);
    if (empty($className) === false) {
        // Not an anonymous class.
        $className = strtolower($className);
    }
    if ($className !== $this->currentClass) {
        $this->loadFunctionNamesInScope($phpcsFile, $currScope);
        $this->currentClass = $className;
    }
    $methodName = strtolower($phpcsFile->getDeclarationName($stackPtr));
    if ($methodName === $className) {
        if (in_array('__construct', $this->functionList, true) === false) {
            $error = 'PHP4 style constructors are not allowed; use "__construct()" instead';
            $phpcsFile->addError($error, $stackPtr, 'OldStyle');
        }
    }
    else {
        if ($methodName !== '__construct') {
            // Not a constructor.
            return;
        }
    }
    // Stop if the constructor doesn't have a body, like when it is abstract.
    if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) {
        return;
    }
    $parentClassName = $phpcsFile->findExtendedClassName($currScope);
    if ($parentClassName === false) {
        return;
    }
    $parentClassNameLc = strtolower($parentClassName);
    $endFunctionIndex = $tokens[$stackPtr]['scope_closer'];
    $startIndex = $tokens[$stackPtr]['scope_opener'];
    while (($doubleColonIndex = $phpcsFile->findNext(T_DOUBLE_COLON, $startIndex + 1, $endFunctionIndex)) !== false) {
        $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, $doubleColonIndex + 1, null, true);
        if ($tokens[$nextNonEmpty]['code'] !== T_STRING || strtolower($tokens[$nextNonEmpty]['content']) !== $parentClassNameLc) {
            $startIndex = $nextNonEmpty;
            continue;
        }
        $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, $doubleColonIndex - 1, null, true);
        if ($tokens[$prevNonEmpty]['code'] === T_PARENT || $tokens[$prevNonEmpty]['code'] === T_SELF || $tokens[$prevNonEmpty]['code'] === T_STATIC || $tokens[$prevNonEmpty]['code'] === T_STRING && strtolower($tokens[$prevNonEmpty]['content']) === $parentClassNameLc) {
            $error = 'PHP4 style calls to parent constructors are not allowed; use "parent::__construct()" instead';
            $phpcsFile->addError($error, $nextNonEmpty, 'OldStyleCall');
        }
        $startIndex = $nextNonEmpty;
    }
    
    //end while
}

API Navigation

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