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

Breadcrumb

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

class LineLengthSniff

Same name in this branch
  1. 11.1.x vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Files/LineLengthSniff.php \SlevomatCodingStandard\Sniffs\Files\LineLengthSniff
  2. 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/Generic/Sniffs/Files/LineLengthSniff.php \PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff

Checks comment lines in the file, and throws warnings if they are over 80 characters in length.

@category PHP @package PHP_CodeSniffer @link http://pear.php.net/package/PHP_CodeSniffer

Hierarchy

  • class \PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff implements \PHP_CodeSniffer\Sniffs\Sniff
    • class \Drupal\Sniffs\Files\LineLengthSniff extends \PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff

Expanded class hierarchy of LineLengthSniff

1 file declares its use of LineLengthSniff
UnnecessaryStringConcatSniff.php in vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Strings/UnnecessaryStringConcatSniff.php

File

vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Files/LineLengthSniff.php, line 24

Namespace

Drupal\Sniffs\Files
View source
class LineLengthSniff extends GenericLineLengthSniff {
    
    /**
     * The limit that the length of a line should not exceed.
     *
     * @var integer
     */
    public $lineLimit = 80;
    
    /**
     * The limit that the length of a line must not exceed.
     * But just check the line length of comments....
     *
     * Set to zero (0) to disable.
     *
     * @var integer
     */
    public $absoluteLineLimit = 0;
    
    /**
     * Checks if a line is too long.
     *
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
     * @param array<int, mixed>           $tokens    The token stack.
     * @param int                         $stackPtr  The first token on the next line.
     *
     * @return void
     */
    protected function checkLineLength($phpcsFile, $tokens, $stackPtr) {
        if (isset(Tokens::$commentTokens[$tokens[$stackPtr - 1]['code']]) === true) {
            $docCommentTag = $phpcsFile->findFirstOnLine(T_DOC_COMMENT_TAG, $stackPtr - 1);
            if ($docCommentTag !== false) {
                // Allow doc comment tags such as long @param tags to exceed the 80
                // character limit.
                return;
            }
            if ($tokens[$stackPtr - 1]['code'] === T_COMMENT && (preg_match('/^[[:space:]]*\\/\\/ @.+/', $tokens[$stackPtr - 1]['content']) === 1 || strpos(trim($tokens[$stackPtr - 1]['content'], "/ \n"), ' ') === false)) {
                return;
            }
            // Code examples between @code and @endcode are allowed to exceed 80
            // characters.
            if (isset($tokens[$stackPtr]) === true && $tokens[$stackPtr]['code'] === T_DOC_COMMENT_WHITESPACE) {
                $tag = $phpcsFile->findPrevious([
                    T_DOC_COMMENT_TAG,
                    T_DOC_COMMENT_OPEN_TAG,
                ], $stackPtr - 1);
                if ($tokens[$tag]['content'] === '@code') {
                    return;
                }
            }
            // Drupal 8 annotations can have long translatable descriptions and we
            // allow them to exceed 80 characters.
            if ($tokens[$stackPtr - 2]['code'] === T_DOC_COMMENT_STRING && (strpos($tokens[$stackPtr - 2]['content'], '@Translation(') !== false || strpos($tokens[$stackPtr - 2]['content'], ' ') === false || preg_match('/^Contains [a-zA-Z_\\\\.]+$/', $tokens[$stackPtr - 2]['content']) === 1 || preg_match('#= ("|\')?\\S+[\\\\/]\\S+("|\')?,*$#', $tokens[$stackPtr - 2]['content']) === 1) || strpos($tokens[$stackPtr - 2]['content'], '- @link') !== false || preg_match('/^Implements hook_[a-zA-Z0-9_]+\\(\\)/', $tokens[$stackPtr - 2]['content']) === 1) {
                return;
            }
            parent::checkLineLength($phpcsFile, $tokens, $stackPtr);
        }
        
        //end if
    }
    
    //end checkLineLength()
    
    /**
     * Returns the length of a defined line.
     *
     * @param \PHP_CodeSniffer\Files\File $phpcsFile   The file being scanned.
     * @param int                         $currentLine The current line.
     *
     * @return int
     */
    public function getLineLength(File $phpcsFile, $currentLine) {
        $tokens = $phpcsFile->getTokens();
        $tokenCount = 0;
        $currentLineContent = '';
        $trim = strlen($phpcsFile->eolChar) * -1;
        for (; $tokenCount < $phpcsFile->numTokens; $tokenCount++) {
            if ($tokens[$tokenCount]['line'] === $currentLine) {
                $currentLineContent .= $tokens[$tokenCount]['content'];
            }
        }
        return strlen($currentLineContent);
    }
    
    //end getLineLength()

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
LineLengthSniff::$absoluteLineLimit public property The limit that the length of a line must not exceed.
But just check the line length of comments....
Overrides LineLengthSniff::$absoluteLineLimit
LineLengthSniff::$ignoreComments public property Whether or not to ignore trailing comments.
LineLengthSniff::$lineLimit public property The limit that the length of a line should not exceed. Overrides LineLengthSniff::$lineLimit
LineLengthSniff::checkLineLength protected function Checks if a line is too long. Overrides LineLengthSniff::checkLineLength
LineLengthSniff::getLineLength public function Returns the length of a defined line.
LineLengthSniff::process public function Processes this test, when one of its tokens is encountered. Overrides Sniff::process
LineLengthSniff::register public function Returns an array of tokens this test wants to listen for. Overrides Sniff::register
RSS feed
Powered by Drupal