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

Breadcrumb

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

function ArrayDeclarationSniff::process

Processes this sniff, when one of its tokens is encountered.

Parameters

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

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

Return value

void

Overrides Sniff::process

File

vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php, line 44

Class

ArrayDeclarationSniff

Namespace

PHP_CodeSniffer\Standards\Squiz\Sniffs\Arrays

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    if ($tokens[$stackPtr]['code'] === T_ARRAY) {
        $phpcsFile->recordMetric($stackPtr, 'Short array syntax used', 'no');
        // Array keyword should be lower case.
        if ($tokens[$stackPtr]['content'] !== strtolower($tokens[$stackPtr]['content'])) {
            if ($tokens[$stackPtr]['content'] === strtoupper($tokens[$stackPtr]['content'])) {
                $phpcsFile->recordMetric($stackPtr, 'Array keyword case', 'upper');
            }
            else {
                $phpcsFile->recordMetric($stackPtr, 'Array keyword case', 'mixed');
            }
            $error = 'Array keyword should be lower case; expected "array" but found "%s"';
            $data = [
                $tokens[$stackPtr]['content'],
            ];
            $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotLowerCase', $data);
            if ($fix === true) {
                $phpcsFile->fixer
                    ->replaceToken($stackPtr, 'array');
            }
        }
        else {
            $phpcsFile->recordMetric($stackPtr, 'Array keyword case', 'lower');
        }
        $arrayStart = $tokens[$stackPtr]['parenthesis_opener'];
        if (isset($tokens[$arrayStart]['parenthesis_closer']) === false) {
            return;
        }
        $arrayEnd = $tokens[$arrayStart]['parenthesis_closer'];
        if ($arrayStart !== $stackPtr + 1) {
            $error = 'There must be no space between the "array" keyword and the opening parenthesis';
            $next = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, $arrayStart, true);
            if (isset(Tokens::$commentTokens[$tokens[$next]['code']]) === true) {
                // We don't have anywhere to put the comment, so don't attempt to fix it.
                $phpcsFile->addError($error, $stackPtr, 'SpaceAfterKeyword');
            }
            else {
                $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterKeyword');
                if ($fix === true) {
                    $phpcsFile->fixer
                        ->beginChangeset();
                    for ($i = $stackPtr + 1; $i < $arrayStart; $i++) {
                        $phpcsFile->fixer
                            ->replaceToken($i, '');
                    }
                    $phpcsFile->fixer
                        ->endChangeset();
                }
            }
        }
    }
    else {
        $phpcsFile->recordMetric($stackPtr, 'Short array syntax used', 'yes');
        $arrayStart = $stackPtr;
        $arrayEnd = $tokens[$stackPtr]['bracket_closer'];
    }
    
    //end if
    // Check for empty arrays.
    $content = $phpcsFile->findNext(T_WHITESPACE, $arrayStart + 1, $arrayEnd + 1, true);
    if ($content === $arrayEnd) {
        // Empty array, but if the brackets aren't together, there's a problem.
        if ($arrayEnd - $arrayStart !== 1) {
            $error = 'Empty array declaration must have no space between the parentheses';
            $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceInEmptyArray');
            if ($fix === true) {
                $phpcsFile->fixer
                    ->beginChangeset();
                for ($i = $arrayStart + 1; $i < $arrayEnd; $i++) {
                    $phpcsFile->fixer
                        ->replaceToken($i, '');
                }
                $phpcsFile->fixer
                    ->endChangeset();
            }
        }
        // We can return here because there is nothing else to check. All code
        // below can assume that the array is not empty.
        return;
    }
    if ($tokens[$arrayStart]['line'] === $tokens[$arrayEnd]['line']) {
        $this->processSingleLineArray($phpcsFile, $stackPtr, $arrayStart, $arrayEnd);
    }
    else {
        $this->processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $arrayEnd);
    }
}

API Navigation

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