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

Breadcrumb

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

function EchoedStringsSniff::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/Strings/EchoedStringsSniff.php, line 41

Class

EchoedStringsSniff

Namespace

PHP_CodeSniffer\Standards\Squiz\Sniffs\Strings

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    $firstContent = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
    // If the first non-whitespace token is not an opening parenthesis, then we are not concerned.
    if ($tokens[$firstContent]['code'] !== T_OPEN_PARENTHESIS) {
        $phpcsFile->recordMetric($stackPtr, 'Brackets around echoed strings', 'no');
        return;
    }
    $end = $phpcsFile->findNext([
        T_SEMICOLON,
        T_CLOSE_TAG,
    ], $stackPtr, null, false);
    // If the token before the semicolon is not a closing parenthesis, then we are not concerned.
    $prev = $phpcsFile->findPrevious(T_WHITESPACE, $end - 1, null, true);
    if ($tokens[$prev]['code'] !== T_CLOSE_PARENTHESIS) {
        $phpcsFile->recordMetric($stackPtr, 'Brackets around echoed strings', 'no');
        return;
    }
    // If the parenthesis don't match, then we are not concerned.
    if ($tokens[$firstContent]['parenthesis_closer'] !== $prev) {
        $phpcsFile->recordMetric($stackPtr, 'Brackets around echoed strings', 'no');
        return;
    }
    $phpcsFile->recordMetric($stackPtr, 'Brackets around echoed strings', 'yes');
    if ($phpcsFile->findNext(Tokens::$operators, $stackPtr, $end, false) === false) {
        // There are no arithmetic operators in this.
        $error = 'Echoed strings should not be bracketed';
        $fix = $phpcsFile->addFixableError($error, $stackPtr, 'HasBracket');
        if ($fix === true) {
            $phpcsFile->fixer
                ->beginChangeset();
            $phpcsFile->fixer
                ->replaceToken($firstContent, '');
            if ($tokens[$firstContent - 1]['code'] !== T_WHITESPACE) {
                $phpcsFile->fixer
                    ->addContent($firstContent - 1, ' ');
            }
            $phpcsFile->fixer
                ->replaceToken($prev, '');
            $phpcsFile->fixer
                ->endChangeset();
        }
    }
}

API Navigation

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