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

Breadcrumb

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

function EmbeddedPhpSniff::validateMultilineEmbeddedPhp

Validates embedded PHP that exists on multiple lines.

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|false $closingTag The position of the PHP close tag in the: stack passed in $tokens.

Return value

void

1 call to EmbeddedPhpSniff::validateMultilineEmbeddedPhp()
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 71

Class

EmbeddedPhpSniff

Namespace

PHP_CodeSniffer\Standards\Squiz\Sniffs\PHP

Code

private function validateMultilineEmbeddedPhp($phpcsFile, $stackPtr, $closingTag) {
    $tokens = $phpcsFile->getTokens();
    $prevTag = $phpcsFile->findPrevious($this->register(), $stackPtr - 1);
    if ($prevTag === false) {
        // This is the first open tag.
        return;
    }
    $firstContent = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
    if ($firstContent === false) {
        // Unclosed PHP open tag at the end of a file. Nothing to do.
        return;
    }
    if ($closingTag !== false) {
        $firstContentAfterBlock = $phpcsFile->findNext(T_WHITESPACE, $closingTag + 1, $phpcsFile->numTokens, true);
        if ($firstContentAfterBlock === false) {
            // Final closing tag. It will be handled elsewhere.
            return;
        }
        // We have an opening and a closing tag, that lie within other content.
        if ($firstContent === $closingTag) {
            $this->reportEmptyTagSet($phpcsFile, $stackPtr, $closingTag);
            return;
        }
    }
    
    //end if
    if ($tokens[$firstContent]['line'] === $tokens[$stackPtr]['line']) {
        $error = 'Opening PHP tag must be on a line by itself';
        $fix = $phpcsFile->addFixableError($error, $stackPtr, 'ContentAfterOpen');
        if ($fix === true) {
            $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true);
            $padding = strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']));
            $phpcsFile->fixer
                ->beginChangeset();
            $phpcsFile->fixer
                ->replaceToken($stackPtr, rtrim($tokens[$stackPtr]['content']));
            $phpcsFile->fixer
                ->addNewline($stackPtr);
            $phpcsFile->fixer
                ->addContent($stackPtr, str_repeat(' ', $padding));
            if ($tokens[$stackPtr + 1]['code'] === T_WHITESPACE) {
                $phpcsFile->fixer
                    ->replaceToken($stackPtr + 1, '');
            }
            $phpcsFile->fixer
                ->endChangeset();
        }
    }
    else {
        // Check the indent of the first line, except if it is a scope closer.
        if (isset($tokens[$firstContent]['scope_closer']) === false || $tokens[$firstContent]['scope_closer'] !== $firstContent) {
            // Check for a blank line at the top.
            if ($tokens[$firstContent]['line'] > $tokens[$stackPtr]['line'] + 1) {
                // Find a token on the blank line to throw the error on.
                $i = $stackPtr;
                do {
                    $i++;
                } while ($tokens[$i]['line'] !== $tokens[$stackPtr]['line'] + 1);
                $error = 'Blank line found at start of embedded PHP content';
                $fix = $phpcsFile->addFixableError($error, $i, 'SpacingBefore');
                if ($fix === true) {
                    $phpcsFile->fixer
                        ->beginChangeset();
                    for ($i = $stackPtr + 1; $i < $firstContent; $i++) {
                        if ($tokens[$i]['line'] === $tokens[$firstContent]['line'] || $tokens[$i]['line'] === $tokens[$stackPtr]['line']) {
                            continue;
                        }
                        $phpcsFile->fixer
                            ->replaceToken($i, '');
                    }
                    $phpcsFile->fixer
                        ->endChangeset();
                }
            }
            
            //end if
            $indent = 0;
            $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr);
            if ($first === false) {
                $first = $phpcsFile->findFirstOnLine(T_INLINE_HTML, $stackPtr);
                if ($first !== false) {
                    $indent = strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']));
                }
            }
            else {
                $indent = $tokens[$first + 1]['column'] - 1;
            }
            $contentColumn = $tokens[$firstContent]['column'] - 1;
            if ($contentColumn !== $indent) {
                $error = 'First line of embedded PHP code must be indented %s spaces; %s found';
                $data = [
                    $indent,
                    $contentColumn,
                ];
                $fix = $phpcsFile->addFixableError($error, $firstContent, 'Indent', $data);
                if ($fix === true) {
                    $padding = str_repeat(' ', $indent);
                    if ($contentColumn === 0) {
                        $phpcsFile->fixer
                            ->addContentBefore($firstContent, $padding);
                    }
                    else {
                        $phpcsFile->fixer
                            ->replaceToken($firstContent - 1, $padding);
                    }
                }
            }
        }
        
        //end if
    }
    
    //end if
    $lastContentBeforeBlock = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
    if ($tokens[$lastContentBeforeBlock]['line'] === $tokens[$stackPtr]['line'] && trim($tokens[$lastContentBeforeBlock]['content']) !== '') {
        $error = 'Opening PHP tag must be on a line by itself';
        $fix = $phpcsFile->addFixableError($error, $stackPtr, 'ContentBeforeOpen');
        if ($fix === true) {
            $padding = 0;
            $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr);
            if ($first === false) {
                $first = $phpcsFile->findFirstOnLine(T_INLINE_HTML, $stackPtr);
                if ($first !== false) {
                    $padding = strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']));
                }
            }
            else {
                $padding = $tokens[$first + 1]['column'] - 1;
            }
            $phpcsFile->fixer
                ->addContentBefore($stackPtr, $phpcsFile->eolChar . str_repeat(' ', $padding));
        }
    }
    else {
        // Find the first token on the first non-empty line we find.
        for ($first = $lastContentBeforeBlock - 1; $first > 0; $first--) {
            if ($tokens[$first]['line'] === $tokens[$stackPtr]['line']) {
                continue;
            }
            else {
                if (trim($tokens[$first]['content']) !== '') {
                    $first = $phpcsFile->findFirstOnLine([], $first, true);
                    if ($tokens[$first]['code'] === T_COMMENT && $tokens[$first]['content'] !== ltrim($tokens[$first]['content'])) {
                        // This is a subsequent line in a star-slash comment containing leading indent.
                        // We'll need the first line of the comment to correctly determine the indent.
                        continue;
                    }
                    break;
                }
            }
        }
        $expected = 0;
        if ($tokens[$first]['code'] === T_INLINE_HTML && trim($tokens[$first]['content']) !== '') {
            $expected = strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']));
        }
        else {
            if ($tokens[$first]['code'] === T_WHITESPACE) {
                $expected = $tokens[$first + 1]['column'] - 1;
            }
        }
        $expected += 4;
        $found = $tokens[$stackPtr]['column'] - 1;
        if ($found > $expected) {
            $error = 'Opening PHP tag indent incorrect; expected no more than %s spaces but found %s';
            $data = [
                $expected,
                $found,
            ];
            $fix = $phpcsFile->addFixableError($error, $stackPtr, 'OpenTagIndent', $data);
            if ($fix === true) {
                $phpcsFile->fixer
                    ->replaceToken($stackPtr - 1, str_repeat(' ', $expected));
            }
        }
    }
    
    //end if
    if ($closingTag === false) {
        return;
    }
    $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, $closingTag - 1, $stackPtr + 1, true);
    $firstContentAfterBlock = $phpcsFile->findNext(T_WHITESPACE, $closingTag + 1, null, true);
    if ($tokens[$lastContent]['line'] === $tokens[$closingTag]['line']) {
        $error = 'Closing PHP tag must be on a line by itself';
        $fix = $phpcsFile->addFixableError($error, $closingTag, 'ContentBeforeEnd');
        if ($fix === true) {
            // Calculate the indent for the close tag.
            // If the close tag is on the same line as the first content, re-use the indent
            // calculated for the first content line to prevent the indent being based on an
            // "old" indent, not the _new_ (fixed) indent.
            if ($tokens[$firstContent]['line'] === $tokens[$lastContent]['line'] && isset($indent) === true) {
                $closerIndent = $indent;
            }
            else {
                $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $closingTag, true);
                while ($tokens[$first]['code'] === T_COMMENT && $tokens[$first]['content'] !== ltrim($tokens[$first]['content'])) {
                    // This is a subsequent line in a star-slash comment containing leading indent.
                    // We'll need the first line of the comment to correctly determine the indent.
                    $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $first - 1, true);
                }
                $closerIndent = $tokens[$first]['column'] - 1;
            }
            $phpcsFile->fixer
                ->beginChangeset();
            if ($tokens[$closingTag - 1]['code'] === T_WHITESPACE) {
                $phpcsFile->fixer
                    ->replaceToken($closingTag - 1, '');
            }
            $phpcsFile->fixer
                ->addContentBefore($closingTag, str_repeat(' ', $closerIndent));
            $phpcsFile->fixer
                ->addNewlineBefore($closingTag);
            $phpcsFile->fixer
                ->endChangeset();
        }
        
        //end if
    }
    else {
        if ($firstContentAfterBlock !== false && $tokens[$firstContentAfterBlock]['line'] === $tokens[$closingTag]['line']) {
            $error = 'Closing PHP tag must be on a line by itself';
            $fix = $phpcsFile->addFixableError($error, $closingTag, 'ContentAfterEnd');
            if ($fix === true) {
                $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $closingTag, true);
                $phpcsFile->fixer
                    ->beginChangeset();
                $phpcsFile->fixer
                    ->addNewline($closingTag);
                $phpcsFile->fixer
                    ->addContent($closingTag, str_repeat(' ', $tokens[$first]['column'] - 1));
                if ($tokens[$firstContentAfterBlock]['code'] === T_INLINE_HTML) {
                    $trimmedHtmlContent = ltrim($tokens[$firstContentAfterBlock]['content']);
                    if ($trimmedHtmlContent === '') {
                        // HTML token contains only whitespace and the next token after is PHP, not HTML, so remove the whitespace.
                        $phpcsFile->fixer
                            ->replaceToken($firstContentAfterBlock, '');
                    }
                    else {
                        // The HTML token has content, so remove leading whitespace in favour of the indent.
                        $phpcsFile->fixer
                            ->replaceToken($firstContentAfterBlock, $trimmedHtmlContent);
                    }
                }
                if ($tokens[$firstContentAfterBlock]['code'] === T_OPEN_TAG || $tokens[$firstContentAfterBlock]['code'] === T_OPEN_TAG_WITH_ECHO) {
                    // Next token is a PHP open tag which will also have thrown an error.
                    // Prevent both fixers running in the same loop by making sure the token is "touched" during this loop.
                    // This prevents a stray new line being added between the close and open tags.
                    $phpcsFile->fixer
                        ->replaceToken($firstContentAfterBlock, $tokens[$firstContentAfterBlock]['content']);
                }
                $phpcsFile->fixer
                    ->endChangeset();
            }
            
            //end if
        }
    }
    
    //end if
    $next = $phpcsFile->findNext($this->register(), $closingTag + 1);
    if ($next === false) {
        return;
    }
    // Check for a blank line at the bottom.
    if ((isset($tokens[$lastContent]['scope_closer']) === false || $tokens[$lastContent]['scope_closer'] !== $lastContent) && $tokens[$lastContent]['line'] < $tokens[$closingTag]['line'] - 1) {
        // Find a token on the blank line to throw the error on.
        $i = $closingTag;
        do {
            $i--;
        } while ($tokens[$i]['line'] !== $tokens[$closingTag]['line'] - 1);
        $error = 'Blank line found at end of embedded PHP content';
        $fix = $phpcsFile->addFixableError($error, $i, 'SpacingAfter');
        if ($fix === true) {
            $phpcsFile->fixer
                ->beginChangeset();
            for ($i = $lastContent + 1; $i < $closingTag; $i++) {
                if ($tokens[$i]['line'] === $tokens[$lastContent]['line'] || $tokens[$i]['line'] === $tokens[$closingTag]['line']) {
                    continue;
                }
                $phpcsFile->fixer
                    ->replaceToken($i, '');
            }
            $phpcsFile->fixer
                ->endChangeset();
        }
    }
    
    //end if
}

API Navigation

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