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

Breadcrumb

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

function EmbeddedPhpSniff::validateInlineEmbeddedPhp

Validates embedded PHP that exists on one line.

Parameters

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

int $stackPtr The position of the current token in the: stack passed in $tokens.

int $closeTag The position of the PHP close tag in the: stack passed in $tokens.

Return value

void

1 call to EmbeddedPhpSniff::validateInlineEmbeddedPhp()
EmbeddedPhpSniff::process in vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php
Processes this test, when one of its tokens is encountered.

File

vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php, line 370

Class

EmbeddedPhpSniff

Namespace

PHP_CodeSniffer\Standards\Squiz\Sniffs\PHP

Code

private function validateInlineEmbeddedPhp($phpcsFile, $stackPtr, $closeTag) {
    $tokens = $phpcsFile->getTokens();
    $firstContent = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, $closeTag, true);
    if ($firstContent === false) {
        $this->reportEmptyTagSet($phpcsFile, $stackPtr, $closeTag);
        return;
    }
    // Check that there is one, and only one space at the start of the statement.
    $leadingSpace = 0;
    $isLongOpenTag = false;
    if ($tokens[$stackPtr]['code'] === T_OPEN_TAG && stripos($tokens[$stackPtr]['content'], '<?php') === 0) {
        // The long open tag token in a single line tag set always contains a single space after it.
        $leadingSpace = 1;
        $isLongOpenTag = true;
    }
    if ($tokens[$stackPtr + 1]['code'] === T_WHITESPACE) {
        $leadingSpace += $tokens[$stackPtr + 1]['length'];
    }
    if ($leadingSpace !== 1) {
        $error = 'Expected 1 space after opening PHP tag; %s found';
        $data = [
            $leadingSpace,
        ];
        $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterOpen', $data);
        if ($fix === true) {
            if ($isLongOpenTag === true) {
                $phpcsFile->fixer
                    ->replaceToken($stackPtr + 1, '');
            }
            else {
                if ($tokens[$stackPtr + 1]['code'] === T_WHITESPACE) {
                    // Short open tag with too much whitespace.
                    $phpcsFile->fixer
                        ->replaceToken($stackPtr + 1, ' ');
                }
                else {
                    // Short open tag without whitespace.
                    $phpcsFile->fixer
                        ->addContent($stackPtr, ' ');
                }
            }
        }
    }
    $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, $closeTag - 1, $stackPtr, true);
    if ($prev !== $stackPtr) {
        if ((isset($tokens[$prev]['scope_opener']) === false || $tokens[$prev]['scope_opener'] !== $prev) && (isset($tokens[$prev]['scope_closer']) === false || $tokens[$prev]['scope_closer'] !== $prev) && $tokens[$prev]['code'] !== T_SEMICOLON) {
            $error = 'Inline PHP statement must end with a semicolon';
            $code = 'NoSemicolon';
            if ($tokens[$stackPtr]['code'] === T_OPEN_TAG_WITH_ECHO) {
                $code = 'ShortOpenEchoNoSemicolon';
            }
            $fix = $phpcsFile->addFixableError($error, $stackPtr, $code);
            if ($fix === true) {
                $phpcsFile->fixer
                    ->addContent($prev, ';');
            }
        }
        else {
            if ($tokens[$prev]['code'] === T_SEMICOLON) {
                $statementCount = 1;
                for ($i = $stackPtr + 1; $i < $prev; $i++) {
                    if ($tokens[$i]['code'] === T_SEMICOLON) {
                        $statementCount++;
                    }
                }
                if ($statementCount > 1) {
                    $error = 'Inline PHP statement must contain a single statement; %s found';
                    $data = [
                        $statementCount,
                    ];
                    $phpcsFile->addError($error, $stackPtr, 'MultipleStatements', $data);
                }
            }
        }
        
        //end if
    }
    
    //end if
    $trailingSpace = 0;
    if ($tokens[$closeTag - 1]['code'] === T_WHITESPACE) {
        $trailingSpace = $tokens[$closeTag - 1]['length'];
    }
    else {
        if (($tokens[$closeTag - 1]['code'] === T_COMMENT || isset(Tokens::$phpcsCommentTokens[$tokens[$closeTag - 1]['code']]) === true) && substr($tokens[$closeTag - 1]['content'], -1) === ' ') {
            $trailingSpace = strlen($tokens[$closeTag - 1]['content']) - strlen(rtrim($tokens[$closeTag - 1]['content']));
        }
    }
    if ($trailingSpace !== 1) {
        $error = 'Expected 1 space before closing PHP tag; %s found';
        $data = [
            $trailingSpace,
        ];
        $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBeforeClose', $data);
        if ($fix === true) {
            if ($trailingSpace === 0) {
                $phpcsFile->fixer
                    ->addContentBefore($closeTag, ' ');
            }
            else {
                if ($tokens[$closeTag - 1]['code'] === T_COMMENT || isset(Tokens::$phpcsCommentTokens[$tokens[$closeTag - 1]['code']]) === true) {
                    $phpcsFile->fixer
                        ->replaceToken($closeTag - 1, rtrim($tokens[$closeTag - 1]['content']) . ' ');
                }
                else {
                    $phpcsFile->fixer
                        ->replaceToken($closeTag - 1, ' ');
                }
            }
        }
    }
}
RSS feed
Powered by Drupal