You are here

protected function LineLengthSniff::checkLineLength in Coder 8.2

Same name and namespace in other branches
  1. 8.3 coder_sniffer/Drupal/Sniffs/Files/LineLengthSniff.php \Drupal\Sniffs\Files\LineLengthSniff::checkLineLength()
  2. 8.3.x coder_sniffer/Drupal/Sniffs/Files/LineLengthSniff.php \Drupal\Sniffs\Files\LineLengthSniff::checkLineLength()

Checks if a line is too long.

Parameters

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

array $tokens The token stack.:

int $stackPtr The first token on the next line.:

Return value

void

File

coder_sniffer/Drupal/Sniffs/Files/LineLengthSniff.php, line 54

Class

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

Namespace

Drupal\Sniffs\Files

Code

protected function checkLineLength($phpcsFile, $tokens, $stackPtr) {
  if (isset(Tokens::$commentTokens[$tokens[$stackPtr - 1]['code']]) === true) {
    $doc_comment_tag = $phpcsFile
      ->findFirstOnLine(T_DOC_COMMENT_TAG, $stackPtr - 1);
    if ($doc_comment_tag !== 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(array(
        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
}