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

Breadcrumb

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

function UnnecessaryHeredocSniff::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/Generic/Sniffs/Strings/UnnecessaryHeredocSniff.php, line 40

Class

UnnecessaryHeredocSniff

Namespace

PHP_CodeSniffer\Standards\Generic\Sniffs\Strings

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    if (isset($tokens[$stackPtr]['scope_closer']) === false) {
        // Just to be safe. Shouldn't be possible as in that case, the opener shouldn't be tokenized
        // to T_START_HEREDOC by PHP.
        return;
        // @codeCoverageIgnore
    }
    $closer = $tokens[$stackPtr]['scope_closer'];
    $body = '';
    // Collect all the tokens within the heredoc body.
    for ($i = $stackPtr + 1; $i < $closer; $i++) {
        $body .= $tokens[$i]['content'];
    }
    $tokenizedBody = token_get_all(sprintf("<?php <<<EOD\n%s\nEOD;\n?>", $body));
    foreach ($tokenizedBody as $ptr => $bodyToken) {
        if (is_array($bodyToken) === false) {
            continue;
        }
        if ($bodyToken[0] === T_DOLLAR_OPEN_CURLY_BRACES || $bodyToken[0] === T_VARIABLE) {
            // Contains interpolation or expression.
            $phpcsFile->recordMetric($stackPtr, 'Heredoc contains interpolation or expression', 'yes');
            return;
        }
        if ($bodyToken[0] === T_CURLY_OPEN && is_array($tokenizedBody[$ptr + 1]) === false && $tokenizedBody[$ptr + 1] === '$') {
            // Contains interpolation or expression.
            $phpcsFile->recordMetric($stackPtr, 'Heredoc contains interpolation or expression', 'yes');
            return;
        }
    }
    
    //end foreach
    $phpcsFile->recordMetric($stackPtr, 'Heredoc contains interpolation or expression', 'no');
    $warning = 'Detected heredoc without interpolation or expressions. Use nowdoc syntax instead';
    $fix = $phpcsFile->addFixableWarning($warning, $stackPtr, 'Found');
    if ($fix === true) {
        $identifier = trim(ltrim($tokens[$stackPtr]['content'], '<'));
        $replaceWith = "'" . trim($identifier, '"') . "'";
        $replacement = str_replace($identifier, $replaceWith, $tokens[$stackPtr]['content']);
        $phpcsFile->fixer
            ->replaceToken($stackPtr, $replacement);
    }
}
RSS feed
Powered by Drupal