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

Breadcrumb

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

function IncludeSystemSniff::processTokenOutsideScope

Processes a token within the scope that this test is listening to.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.:

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

Return value

void

Overrides AbstractScopeSniff::processTokenOutsideScope

File

vendor/squizlabs/php_codesniffer/src/Standards/MySource/Sniffs/Channels/IncludeSystemSniff.php, line 206

Class

IncludeSystemSniff

Namespace

PHP_CodeSniffer\Standards\MySource\Sniffs\Channels

Code

protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    if ($tokens[$stackPtr]['code'] === T_EXTENDS) {
        // Find the class name.
        $classNameToken = $phpcsFile->findNext(T_STRING, $stackPtr + 1);
        $className = $tokens[$classNameToken]['content'];
    }
    else {
        // Determine the name of the class that the static function
        // is being called on. But don't process class names represented by
        // variables as this can be an inexact science.
        $classNameToken = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
        if ($tokens[$classNameToken]['code'] === T_VARIABLE) {
            return;
        }
        $className = $tokens[$classNameToken]['content'];
    }
    // Some systems are always available.
    if (isset($this->ignore[strtolower($className)]) === true) {
        return;
    }
    $includedClasses = [];
    $fileName = strtolower($phpcsFile->getFilename());
    $matches = [];
    if (preg_match('|/systems/([^/]+)/([^/]+)?actions.inc$|', $fileName, $matches) !== 0) {
        // This is an actions file, which means we don't
        // have to include the system in which it exists
        // We know the system from the path.
        $includedClasses[$matches[1]] = true;
    }
    // Go searching for includeSystem, includeAsset or require/include
    // calls outside our scope.
    for ($i = 0; $i < $stackPtr; $i++) {
        // Skip classes and functions as will we never get
        // into their scopes when including this file, although
        // we have a chance of getting into IF, WHILE etc.
        if (($tokens[$i]['code'] === T_CLASS || $tokens[$i]['code'] === T_INTERFACE || $tokens[$i]['code'] === T_FUNCTION) && isset($tokens[$i]['scope_closer']) === true) {
            $i = $tokens[$i]['scope_closer'];
            continue;
        }
        $name = $this->getIncludedClassFromToken($phpcsFile, $tokens, $i);
        if ($name !== false) {
            $includedClasses[$name] = true;
            // Special case for Widgets cause they are, well, special.
        }
        else {
            if (strtolower($tokens[$i]['content']) === 'includewidget') {
                $typeName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, $i + 1);
                $typeName = trim($tokens[$typeName]['content'], " '");
                $includedClasses[strtolower($typeName) . 'widgettype'] = true;
            }
        }
    }
    
    //end for
    if (isset($includedClasses[strtolower($className)]) === false) {
        if ($tokens[$stackPtr]['code'] === T_EXTENDS) {
            $error = 'Class extends non-included class or system "%s"; include system with Channels::includeSystem() or include class with require_once';
            $data = [
                $className,
            ];
            $phpcsFile->addError($error, $stackPtr, 'NotIncludedExtends', $data);
        }
        else {
            $error = 'Static method called on non-included class or system "%s"; include system with Channels::includeSystem() or include class with require_once';
            $data = [
                $className,
            ];
            $phpcsFile->addError($error, $stackPtr, 'NotIncludedCall', $data);
        }
    }
}

API Navigation

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