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

Breadcrumb

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

function DisallowImplicitArrayCreationSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $bracketOpenerPointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Arrays/DisallowImplicitArrayCreationSniff.php, line 50

Class

DisallowImplicitArrayCreationSniff

Namespace

SlevomatCodingStandard\Sniffs\Arrays

Code

public function process(File $phpcsFile, $bracketOpenerPointer) : void {
    $tokens = $phpcsFile->getTokens();
    $assignmentPointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$bracketOpenerPointer]['bracket_closer'] + 1);
    if ($tokens[$assignmentPointer]['code'] !== T_EQUAL) {
        return;
    }
    
    /** @var int $variablePointer */
    $variablePointer = TokenHelper::findPreviousEffective($phpcsFile, $bracketOpenerPointer - 1);
    if ($tokens[$variablePointer]['code'] !== T_VARIABLE) {
        return;
    }
    if (in_array($tokens[$variablePointer]['content'], [
        '$GLOBALS',
        '$_SERVER',
        '$_REQUEST',
        '$_POST',
        '$_GET',
        '$_FILES',
        '$_ENV',
        '$_COOKIE',
        '$_SESSION',
        '$this',
    ], true)) {
        return;
    }
    $pointerBeforeVariable = TokenHelper::findPreviousEffective($phpcsFile, $variablePointer - 1);
    if (in_array($tokens[$pointerBeforeVariable]['code'], [
        T_OBJECT_OPERATOR,
        T_DOUBLE_COLON,
    ], true)) {
        return;
    }
    $scopeOwnerPointer = null;
    foreach (array_reverse($tokens[$variablePointer]['conditions'], true) as $conditionPointer => $conditionTokenCode) {
        if (!in_array($conditionTokenCode, TokenHelper::$functionTokenCodes, true)) {
            continue;
        }
        $scopeOwnerPointer = $conditionPointer;
        break;
    }
    if ($scopeOwnerPointer === null) {
        $scopeOwnerPointer = TokenHelper::findPrevious($phpcsFile, T_OPEN_TAG, $variablePointer - 1);
    }
    $scopeOpenerPointer = $tokens[$scopeOwnerPointer]['code'] === T_OPEN_TAG ? $scopeOwnerPointer : $tokens[$scopeOwnerPointer]['scope_opener'];
    $scopeCloserPointer = $tokens[$scopeOwnerPointer]['code'] === T_OPEN_TAG ? count($tokens) - 1 : $tokens[$scopeOwnerPointer]['scope_closer'];
    if (in_array($tokens[$scopeOwnerPointer]['code'], TokenHelper::$functionTokenCodes, true)) {
        if ($this->isParameter($phpcsFile, $scopeOwnerPointer, $variablePointer)) {
            return;
        }
        if ($tokens[$scopeOwnerPointer]['code'] === T_CLOSURE && $this->isInheritedVariable($phpcsFile, $scopeOwnerPointer, $variablePointer)) {
            return;
        }
    }
    if ($this->hasExplicitCreation($phpcsFile, $scopeOpenerPointer, $scopeCloserPointer, $variablePointer)) {
        return;
    }
    $phpcsFile->addError('Implicit array creation is disallowed.', $variablePointer, self::CODE_IMPLICIT_ARRAY_CREATION_USED);
}

API Navigation

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