Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Standards/Generic/Sniffs/Files/LineLengthSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class LineLengthSniff implements Sniff
*/
public $ignoreComments = false;

/**
* Whether or not to ignore use statements.
*
* @var boolean
*/
public $ignoreUseStatements = false;


/**
* Returns an array of tokens this test wants to listen for.
Expand Down Expand Up @@ -141,6 +148,16 @@ protected function checkLineLength($phpcsFile, $tokens, $stackPtr)
$lineLength -= $tokens[$stackPtr]['length'];
}

if ($this->ignoreUseStatements === true
&& $lineLength > $this->lineLimit
) {
$prevUseStatement = $phpcsFile->findPrevious([T_USE], ($stackPtr - 1));
if ($tokens[$stackPtr]['line'] === $tokens[$prevUseStatement]['line']) {
// Ignore use statements as these can only be on one line
return;
}
}

// Record metrics for common line length groupings.
if ($lineLength <= 80) {
$phpcsFile->recordMetric($stackPtr, 'Line length', '80 or less');
Expand Down
6 changes: 6 additions & 0 deletions src/Standards/Generic/Tests/Files/LineLengthUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,9 @@ $ab = $ab = $ab = $ab = $ab = $ab = $ab = $ab = $ab = $ab;
$a = $b; // phpcs:ignore Standard.Category.Sniff.ErrorCode1,Standard.Category.Sniff.ErrorCode2 -- for reasons ...

if (($anotherReallyLongVarName === true) {} // This comment makes the line too long.

?>

<?php

use ThisReallyLong\Long\Loooooooooooooooong\Loooooooooooooooong\UseStatement\ThatShouldNotBeAllowed\AndThrowErrorsBecauseItsTooLong;
8 changes: 8 additions & 0 deletions src/Standards/Generic/Tests/Files/LineLengthUnitTest.5.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
phpcs:set Generic.Files.LineLength ignoreUseStatements true
<?php

/* This line is fine. */
use ThisShort\UseStatement\ThatShouldBeAllowed;

/* This line is too long but will be ignored. This line is too long but will be ignored. */
use ThisReallyLong\Long\Loooooooooooooooong\Loooooooooooooooong\UseStatement\ThatShouldBeAllowed\AndThrowNoWarningsOrErrors;
1 change: 1 addition & 0 deletions src/Standards/Generic/Tests/Files/LineLengthUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function getErrorList($testFile='')
34 => 1,
45 => 1,
82 => 1,
90 => 1,
];

case 'LineLengthUnitTest.2.inc':
Expand Down