Skip to content

Commit

Permalink
Generic/LineLength: ignore line length for use statements
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rjd22 committed Oct 25, 2024
1 parent 028501d commit 0f40736
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
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\ThatShouldBeAllowed\AndThrowNoWarningsOrErrors;
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

0 comments on commit 0f40736

Please sign in to comment.