function LineLengthSniff::checkLineLength
Same name in this branch
- 11.1.x vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Files/LineLengthSniff.php \SlevomatCodingStandard\Sniffs\Files\LineLengthSniff::checkLineLength()
- 11.1.x vendor/drupal/coder/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
3 calls to LineLengthSniff::checkLineLength()
- LineLengthSniff::checkLineLength in vendor/
drupal/ coder/ coder_sniffer/ Drupal/ Sniffs/ Files/ LineLengthSniff.php - Checks if a line is too long.
- LineLengthSniff::checkLineLength in vendor/
drupal/ coder/ coder_sniffer/ Drupal/ Sniffs/ Files/ LineLengthSniff.php - Checks if a line is too long.
- LineLengthSniff::process in vendor/
squizlabs/ php_codesniffer/ src/ Standards/ Generic/ Sniffs/ Files/ LineLengthSniff.php - Processes this test, when one of its tokens is encountered.
1 method overrides LineLengthSniff::checkLineLength()
- LineLengthSniff::checkLineLength in vendor/
drupal/ coder/ coder_sniffer/ Drupal/ Sniffs/ Files/ LineLengthSniff.php - Checks if a line is too long.
File
-
vendor/
squizlabs/ php_codesniffer/ src/ Standards/ Generic/ Sniffs/ Files/ LineLengthSniff.php, line 97
Class
Namespace
PHP_CodeSniffer\Standards\Generic\Sniffs\FilesCode
protected function checkLineLength($phpcsFile, $tokens, $stackPtr) {
// The passed token is the first on the line.
$stackPtr--;
if ($tokens[$stackPtr]['column'] === 1 && $tokens[$stackPtr]['length'] === 0) {
// Blank line.
return;
}
if ($tokens[$stackPtr]['column'] !== 1 && $tokens[$stackPtr]['content'] === $phpcsFile->eolChar) {
$stackPtr--;
}
$onlyComment = false;
if (isset(Tokens::$commentTokens[$tokens[$stackPtr]['code']]) === true) {
$prevNonWhiteSpace = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true);
if ($tokens[$stackPtr]['line'] !== $tokens[$prevNonWhiteSpace]['line']) {
$onlyComment = true;
}
}
if ($onlyComment === true && isset(Tokens::$phpcsCommentTokens[$tokens[$stackPtr]['code']]) === true) {
// Ignore PHPCS annotation comments that are on a line by themselves.
return;
}
$lineLength = $tokens[$stackPtr]['column'] + $tokens[$stackPtr]['length'] - 1;
if ($this->ignoreComments === true && isset(Tokens::$commentTokens[$tokens[$stackPtr]['code']]) === true) {
// Trailing comments are being ignored in line length calculations.
if ($onlyComment === true) {
// The comment is the only thing on the line, so no need to check length.
return;
}
$lineLength -= $tokens[$stackPtr]['length'];
}
// Record metrics for common line length groupings.
if ($lineLength <= 80) {
$phpcsFile->recordMetric($stackPtr, 'Line length', '80 or less');
}
else {
if ($lineLength <= 120) {
$phpcsFile->recordMetric($stackPtr, 'Line length', '81-120');
}
else {
if ($lineLength <= 150) {
$phpcsFile->recordMetric($stackPtr, 'Line length', '121-150');
}
else {
$phpcsFile->recordMetric($stackPtr, 'Line length', '151 or more');
}
}
}
if ($onlyComment === true) {
// 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->lineLimit) {
$oldLength = strlen($tokens[$stackPtr]['content']);
$newLength = strlen(ltrim($tokens[$stackPtr]['content'], "/#\t "));
$indent = $tokens[$stackPtr]['column'] - 1 + ($oldLength - $newLength);
$nonBreakingLength = $tokens[$stackPtr]['length'];
$space = strrpos($tokens[$stackPtr]['content'], ' ');
if ($space !== false) {
$nonBreakingLength -= $space + 1;
}
if ($nonBreakingLength + $indent > $this->lineLimit) {
return;
}
}
}
//end if
if ($this->absoluteLineLimit > 0 && $lineLength > $this->absoluteLineLimit) {
$data = [
$this->absoluteLineLimit,
$lineLength,
];
$error = 'Line exceeds maximum limit of %s characters; contains %s characters';
$phpcsFile->addError($error, $stackPtr, 'MaxExceeded', $data);
}
else {
if ($lineLength > $this->lineLimit) {
$data = [
$this->lineLimit,
$lineLength,
];
$warning = 'Line exceeds %s characters; contains %s characters';
$phpcsFile->addWarning($warning, $stackPtr, 'TooLong', $data);
}
}
}