diff --git a/src/Standards/Generic/Sniffs/Files/LineLengthSniff.php b/src/Standards/Generic/Sniffs/Files/LineLengthSniff.php index a65baf76b5..bf4f62c580 100644 --- a/src/Standards/Generic/Sniffs/Files/LineLengthSniff.php +++ b/src/Standards/Generic/Sniffs/Files/LineLengthSniff.php @@ -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. @@ -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'); diff --git a/src/Standards/Generic/Tests/Files/LineLengthUnitTest.1.inc b/src/Standards/Generic/Tests/Files/LineLengthUnitTest.1.inc index 0c30cc424d..5794ed3240 100644 --- a/src/Standards/Generic/Tests/Files/LineLengthUnitTest.1.inc +++ b/src/Standards/Generic/Tests/Files/LineLengthUnitTest.1.inc @@ -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. + +?> + + 1, 45 => 1, 82 => 1, + 90 => 1, ]; case 'LineLengthUnitTest.2.inc':