LineLengthSniff.php in Coder 8.3.x
File
coder_sniffer/Drupal/Sniffs/Files/LineLengthSniff.php
View source
<?php
namespace Drupal\Sniffs\Files;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff as GenericLineLengthSniff;
use PHP_CodeSniffer\Util\Tokens;
class LineLengthSniff extends GenericLineLengthSniff {
public $lineLimit = 80;
public $absoluteLineLimit = 0;
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) {
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;
}
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;
}
}
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);
}
}
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);
}
}
Classes
Name |
Description |
LineLengthSniff |
Checks comment lines in the file, and throws warnings if they are over 80
characters in length. |