Skip to content

Commit 77bc492

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 77bc492

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ protected function checkLineLength($phpcsFile, $tokens, $stackPtr)
141141
$lineLength -= $tokens[$stackPtr]['length'];
142142
}
143143

144+
if ($lineLength > $this->lineLimit) {
145+
$prevUseStatement = $phpcsFile->findPrevious([T_USE], ($stackPtr - 1), null, true);
146+
if ($tokens[$stackPtr]['line'] === $tokens[$prevUseStatement]['line']) {
147+
// Ignore use statements as these can only be on one line
148+
return;
149+
}
150+
}
151+
144152
// Record metrics for common line length groupings.
145153
if ($lineLength <= 80) {
146154
$phpcsFile->recordMetric($stackPtr, 'Line length', '80 or less');

0 commit comments

Comments
 (0)