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

Breadcrumb

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

function LanguageConstructSpacingSniff::process

Same name in this branch
  1. 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/Generic/Sniffs/WhiteSpace/LanguageConstructSpacingSniff.php \PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace\LanguageConstructSpacingSniff::process()

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

Parameters

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

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/WhiteSpace/LanguageConstructSpacingSniff.php, line 52

Class

LanguageConstructSpacingSniff

Namespace

PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    if (isset($tokens[$stackPtr + 1]) === false) {
        // Skip if there is no next token.
        return;
    }
    if ($tokens[$stackPtr + 1]['code'] === T_SEMICOLON) {
        // No content for this language construct.
        return;
    }
    if ($tokens[$stackPtr + 1]['code'] === T_WHITESPACE) {
        $content = $tokens[$stackPtr + 1]['content'];
        if ($content !== ' ') {
            $error = 'Language constructs must be followed by a single space; expected 1 space but found "%s"';
            $data = [
                Util\Common::prepareForOutput($content),
            ];
            $fix = $phpcsFile->addFixableError($error, $stackPtr, 'IncorrectSingle', $data);
            if ($fix === true) {
                $phpcsFile->fixer
                    ->replaceToken($stackPtr + 1, ' ');
            }
        }
    }
    else {
        if ($tokens[$stackPtr + 1]['code'] !== T_OPEN_PARENTHESIS) {
            $error = 'Language constructs must be followed by a single space; expected "%s" but found "%s"';
            $data = [
                $tokens[$stackPtr]['content'] . ' ' . $tokens[$stackPtr + 1]['content'],
                $tokens[$stackPtr]['content'] . $tokens[$stackPtr + 1]['content'],
            ];
            $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Incorrect', $data);
            if ($fix === true) {
                $phpcsFile->fixer
                    ->addContent($stackPtr, ' ');
            }
        }
    }
    
    //end if
}
RSS feed
Powered by Drupal