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

Breadcrumb

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

function LineLengthSniff::checkLineLength

Same name in this branch
  1. 11.1.x vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Files/LineLengthSniff.php \Drupal\Sniffs\Files\LineLengthSniff::checkLineLength()
  2. 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/Generic/Sniffs/Files/LineLengthSniff.php \PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff::checkLineLength()
1 call to LineLengthSniff::checkLineLength()
LineLengthSniff::process in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Files/LineLengthSniff.php
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint * @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter *

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Files/LineLengthSniff.php, line 71

Class

LineLengthSniff

Namespace

SlevomatCodingStandard\Sniffs\Files

Code

private function checkLineLength(File $phpcsFile, int $pointer) : void {
    $tokens = $phpcsFile->getTokens();
    if ($tokens[$pointer]['column'] === 1 && $tokens[$pointer]['length'] === 0) {
        // Blank line.
        return;
    }
    $line = $tokens[$pointer]['line'];
    $nextLineStartPtr = $pointer;
    while (isset($tokens[$nextLineStartPtr]) && $line === $tokens[$nextLineStartPtr]['line']) {
        $pointer = $nextLineStartPtr;
        $nextLineStartPtr++;
    }
    if ($tokens[$pointer]['content'] === $phpcsFile->eolChar) {
        $pointer--;
    }
    $lineLength = $tokens[$pointer]['column'] + $tokens[$pointer]['length'] - 1;
    if ($lineLength <= $this->lineLengthLimit) {
        return;
    }
    if (in_array($tokens[$pointer]['code'], [
        T_COMMENT,
        T_DOC_COMMENT_STRING,
    ], true)) {
        if ($this->ignoreComments === true) {
            return;
        }
        // If this is a long comment, check if it can be broken up onto multiple lines.
        // Some comments contain unbreakable strings like URLs and so it makes sense
        // to ignore the line length in these cases if the URL would be longer than the max
        // line length once you indent it to the correct level.
        if ($lineLength > $this->lineLengthLimit) {
            $oldLength = strlen($tokens[$pointer]['content']);
            $newLength = strlen(ltrim($tokens[$pointer]['content'], "/#\t "));
            $indent = $tokens[$pointer]['column'] - 1 + $oldLength - $newLength;
            $nonBreakingLength = $tokens[$pointer]['length'];
            $space = strrpos($tokens[$pointer]['content'], ' ');
            if ($space !== false) {
                $nonBreakingLength -= $space + 1;
            }
            if ($nonBreakingLength + $indent > $this->lineLengthLimit) {
                return;
            }
        }
    }
    if ($this->ignoreImports) {
        $usePointer = UseStatementHelper::getUseStatementPointer($phpcsFile, $pointer - 1);
        if (is_int($usePointer) && $tokens[$usePointer]['line'] === $tokens[$pointer]['line'] && UseStatementHelper::isImportUse($phpcsFile, $usePointer)) {
            return;
        }
    }
    $error = sprintf('Line exceeds maximum limit of %s characters, contains %s characters.', $this->lineLengthLimit, $lineLength);
    $phpcsFile->addError($error, $pointer, self::CODE_LINE_TOO_LONG);
}
RSS feed
Powered by Drupal