Skip to content

Commit 0f40736

Browse files
committed
Generic/LineLength: ignore line length for use statements
From PHP8 on use statements are considered a single token. For this reason they cannot be splitted up to shorter namespaces. PHPCS doesn't account for that and will throw line length errors and warnings for the use statements. This change ignores the line length for the use statements. RFC link: https://wiki.php.net/rfc/namespaced_names_as_token Issue link: squizlabs/PHP_CodeSniffer#3606
1 parent 028501d commit 0f40736

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

src/Standards/Generic/Sniffs/Files/LineLengthSniff.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ class LineLengthSniff implements Sniff
4646
*/
4747
public $ignoreComments = false;
4848

49+
/**
50+
* Whether or not to ignore use statements.
51+
*
52+
* @var boolean
53+
*/
54+
public $ignoreUseStatements = false;
55+
4956

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

151+
if ($this->ignoreUseStatements === true
152+
&& $lineLength > $this->lineLimit
153+
) {
154+
$prevUseStatement = $phpcsFile->findPrevious([T_USE], ($stackPtr - 1));
155+
if ($tokens[$stackPtr]['line'] === $tokens[$prevUseStatement]['line']) {
156+
// Ignore use statements as these can only be on one line
157+
return;
158+
}
159+
}
160+
144161
// Record metrics for common line length groupings.
145162
if ($lineLength <= 80) {
146163
$phpcsFile->recordMetric($stackPtr, 'Line length', '80 or less');

src/Standards/Generic/Tests/Files/LineLengthUnitTest.1.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,9 @@ $ab = $ab = $ab = $ab = $ab = $ab = $ab = $ab = $ab = $ab;
8282
$a = $b; // phpcs:ignore Standard.Category.Sniff.ErrorCode1,Standard.Category.Sniff.ErrorCode2 -- for reasons ...
8383

8484
if (($anotherReallyLongVarName === true) {} // This comment makes the line too long.
85+
86+
?>
87+
88+
<?php
89+
90+
use ThisReallyLong\Long\Loooooooooooooooong\Loooooooooooooooong\UseStatement\ThatShouldBeAllowed\AndThrowNoWarningsOrErrors;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
phpcs:set Generic.Files.LineLength ignoreUseStatements true
2+
<?php
3+
4+
/* This line is fine. */
5+
use ThisShort\UseStatement\ThatShouldBeAllowed;
6+
7+
/* This line is too long but will be ignored. This line is too long but will be ignored. */
8+
use ThisReallyLong\Long\Loooooooooooooooong\Loooooooooooooooong\UseStatement\ThatShouldBeAllowed\AndThrowNoWarningsOrErrors;

src/Standards/Generic/Tests/Files/LineLengthUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function getErrorList($testFile='')
5454
34 => 1,
5555
45 => 1,
5656
82 => 1,
57+
90 => 1,
5758
];
5859

5960
case 'LineLengthUnitTest.2.inc':

0 commit comments

Comments
 (0)