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

Breadcrumb

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

class EndFileNewlineSniff

Same name in this branch
  1. 11.1.x vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Files/EndFileNewlineSniff.php \Drupal\Sniffs\Files\EndFileNewlineSniff
  2. 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/Generic/Sniffs/Files/EndFileNewlineSniff.php \PHP_CodeSniffer\Standards\Generic\Sniffs\Files\EndFileNewlineSniff

Hierarchy

  • class \PHP_CodeSniffer\Standards\PSR2\Sniffs\Files\EndFileNewlineSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of EndFileNewlineSniff

File

vendor/squizlabs/php_codesniffer/src/Standards/PSR2/Sniffs/Files/EndFileNewlineSniff.php, line 15

Namespace

PHP_CodeSniffer\Standards\PSR2\Sniffs\Files
View source
class EndFileNewlineSniff implements Sniff {
    
    /**
     * Returns an array of tokens this test wants to listen for.
     *
     * @return array<int|string>
     */
    public function register() {
        return [
            T_OPEN_TAG,
            T_OPEN_TAG_WITH_ECHO,
        ];
    }
    
    //end register()
    
    /**
     * Processes this sniff, when one of its tokens is encountered.
     *
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
     * @param int                         $stackPtr  The position of the current token in
     *                                               the stack passed in $tokens.
     *
     * @return int
     */
    public function process(File $phpcsFile, $stackPtr) {
        if ($phpcsFile->findNext(T_INLINE_HTML, $stackPtr + 1) !== false) {
            return $phpcsFile->numTokens;
        }
        // Skip to the end of the file.
        $tokens = $phpcsFile->getTokens();
        $lastToken = $phpcsFile->numTokens - 1;
        if ($tokens[$lastToken]['content'] === '') {
            $lastToken--;
        }
        // Hard-coding the expected \n in this sniff as it is PSR-2 specific and
        // PSR-2 enforces the use of unix style newlines.
        if (substr($tokens[$lastToken]['content'], -1) !== "\n") {
            $error = 'Expected 1 newline at end of file; 0 found';
            $fix = $phpcsFile->addFixableError($error, $lastToken, 'NoneFound');
            if ($fix === true) {
                $phpcsFile->fixer
                    ->addNewline($lastToken);
            }
            $phpcsFile->recordMetric($stackPtr, 'Number of newlines at EOF', '0');
            return $phpcsFile->numTokens;
        }
        // Go looking for the last non-empty line.
        $lastLine = $tokens[$lastToken]['line'];
        if ($tokens[$lastToken]['code'] === T_WHITESPACE || $tokens[$lastToken]['code'] === T_DOC_COMMENT_WHITESPACE) {
            $lastCode = $phpcsFile->findPrevious([
                T_WHITESPACE,
                T_DOC_COMMENT_WHITESPACE,
            ], $lastToken - 1, null, true);
        }
        else {
            $lastCode = $lastToken;
        }
        $lastCodeLine = $tokens[$lastCode]['line'];
        $blankLines = $lastLine - $lastCodeLine + 1;
        $phpcsFile->recordMetric($stackPtr, 'Number of newlines at EOF', $blankLines);
        if ($blankLines > 1) {
            $error = 'Expected 1 blank line at end of file; %s found';
            $data = [
                $blankLines,
            ];
            $fix = $phpcsFile->addFixableError($error, $lastCode, 'TooMany', $data);
            if ($fix === true) {
                $phpcsFile->fixer
                    ->beginChangeset();
                $phpcsFile->fixer
                    ->replaceToken($lastCode, rtrim($tokens[$lastCode]['content']));
                for ($i = $lastCode + 1; $i < $lastToken; $i++) {
                    $phpcsFile->fixer
                        ->replaceToken($i, '');
                }
                $phpcsFile->fixer
                    ->replaceToken($lastToken, $phpcsFile->eolChar);
                $phpcsFile->fixer
                    ->endChangeset();
            }
        }
        // Skip the rest of the file.
        return $phpcsFile->numTokens;
    }
    
    //end process()

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
EndFileNewlineSniff::process public function Processes this sniff, when one of its tokens is encountered. Overrides Sniff::process
EndFileNewlineSniff::register public function Returns an array of tokens this test wants to listen for. Overrides Sniff::register
RSS feed
Powered by Drupal