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

Breadcrumb

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

function SemicolonSpacingSniff::process

Same name in this branch
  1. 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/CSS/SemicolonSpacingSniff.php \PHP_CodeSniffer\Standards\Squiz\Sniffs\CSS\SemicolonSpacingSniff::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/SemicolonSpacingSniff.php, line 51

Class

SemicolonSpacingSniff

Namespace

PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    $prevType = $tokens[$stackPtr - 1]['code'];
    if (isset(Tokens::$emptyTokens[$prevType]) === false) {
        return;
    }
    $nonSpace = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 2, null, true);
    // Detect whether this is a semicolon for a condition in a `for()` control structure.
    $forCondition = false;
    if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
        $nestedParens = $tokens[$stackPtr]['nested_parenthesis'];
        $closeParenthesis = end($nestedParens);
        if (isset($tokens[$closeParenthesis]['parenthesis_owner']) === true) {
            $owner = $tokens[$closeParenthesis]['parenthesis_owner'];
            if ($tokens[$owner]['code'] === T_FOR) {
                $forCondition = true;
                $nonSpace = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 2, null, true);
            }
        }
    }
    if ($tokens[$nonSpace]['code'] === T_SEMICOLON || $forCondition === true && $nonSpace === $tokens[$owner]['parenthesis_opener'] || isset($tokens[$nonSpace]['scope_opener']) === true && $tokens[$nonSpace]['scope_opener'] === $nonSpace) {
        // Empty statement.
        return;
    }
    $expected = $tokens[$nonSpace]['content'] . ';';
    $found = $phpcsFile->getTokensAsString($nonSpace, $stackPtr - $nonSpace) . ';';
    $found = str_replace("\n", '\\n', $found);
    $found = str_replace("\r", '\\r', $found);
    $found = str_replace("\t", '\\t', $found);
    $error = 'Space found before semicolon; expected "%s" but found "%s"';
    $data = [
        $expected,
        $found,
    ];
    $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Incorrect', $data);
    if ($fix === true) {
        $phpcsFile->fixer
            ->beginChangeset();
        $i = $stackPtr - 1;
        while ($tokens[$i]['code'] === T_WHITESPACE && $i > $nonSpace) {
            $phpcsFile->fixer
                ->replaceToken($i, '');
            $i--;
        }
        $phpcsFile->fixer
            ->addContent($nonSpace, ';');
        $phpcsFile->fixer
            ->replaceToken($stackPtr, '');
        $phpcsFile->fixer
            ->endChangeset();
    }
}
RSS feed
Powered by Drupal