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

Breadcrumb

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

function SingleLineArrayWhitespaceSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $stackPointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Arrays/SingleLineArrayWhitespaceSniff.php, line 44

Class

SingleLineArrayWhitespaceSniff

Namespace

SlevomatCodingStandard\Sniffs\Arrays

Code

public function process(File $phpcsFile, $stackPointer) : int {
    $this->spacesAroundBrackets = SniffSettingsHelper::normalizeInteger($this->spacesAroundBrackets);
    $tokens = $phpcsFile->getTokens();
    [
        $arrayOpenerPointer,
        $arrayCloserPointer,
    ] = ArrayHelper::openClosePointers($tokens[$stackPointer]);
    // Check only single-line arrays.
    if ($tokens[$arrayOpenerPointer]['line'] !== $tokens[$arrayCloserPointer]['line']) {
        return $arrayCloserPointer;
    }
    $pointerContent = TokenHelper::findNextNonWhitespace($phpcsFile, $arrayOpenerPointer + 1, $arrayCloserPointer + 1);
    if ($pointerContent === $arrayCloserPointer) {
        // Empty array, but if the brackets aren't together, there's a problem.
        if ($this->enableEmptyArrayCheck) {
            $this->checkWhitespaceInEmptyArray($phpcsFile, $arrayOpenerPointer, $arrayCloserPointer);
        }
        // We can return here because there is nothing else to check.
        // All code below can assume that the array is not empty.
        return $arrayCloserPointer + 1;
    }
    $this->checkWhitespaceAfterOpeningBracket($phpcsFile, $arrayOpenerPointer);
    $this->checkWhitespaceBeforeClosingBracket($phpcsFile, $arrayCloserPointer);
    for ($i = $arrayOpenerPointer + 1; $i < $arrayCloserPointer; $i++) {
        // Skip bracketed statements, like function calls.
        if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS) {
            $i = $tokens[$i]['parenthesis_closer'];
            continue;
        }
        // Skip nested arrays as they will be processed separately
        if (in_array($tokens[$i]['code'], TokenHelper::$arrayTokenCodes, true)) {
            $i = ArrayHelper::openClosePointers($tokens[$i])[1];
            continue;
        }
        if ($tokens[$i]['code'] !== T_COMMA) {
            continue;
        }
        // Before checking this comma, make sure we are not at the end of the array.
        $next = TokenHelper::findNextNonWhitespace($phpcsFile, $i + 1, $arrayCloserPointer);
        if ($next === null) {
            return $arrayOpenerPointer + 1;
        }
        $this->checkWhitespaceBeforeComma($phpcsFile, $i);
        $this->checkWhitespaceAfterComma($phpcsFile, $i);
    }
    return $arrayOpenerPointer + 1;
}
RSS feed
Powered by Drupal