Skip to content

Commit

Permalink
Generic.Files.LineLength ignoreComments property now only ignores com…
Browse files Browse the repository at this point in the history
…ments that are on a line by themselves (ref squizlabs#2533, squizlabs#1510)
  • Loading branch information
gsherwood committed Jul 31, 2019
1 parent ce3c28f commit 2fc1327
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
2 changes: 2 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- Enforce the use of a strict types declaration in PHP files
- Added PSR12.Files.DeclareStatement sniff
-- Enforce the formatting of declare statements within a file
- Generic.Files.LineLength ignoreComments property now only ignores comments that are on a line by themselves
-- Previously, this property was incorrectly causing the sniff to ignore any line that ended with a comment
- Generic.Functions.FunctionCallArgumentSpacing no longer checks spacing around assignment operators inside function calls
-- Use the Squiz.WhiteSpace.OperatorSpacing sniff to enforce spacing around assignment operators
--- Note that this sniff checks spacing around all assignment operators, not just inside function calls
Expand Down
10 changes: 7 additions & 3 deletions src/Standards/Generic/Sniffs/Files/LineLengthSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ protected function checkLineLength($phpcsFile, $tokens, $stackPtr)
// Ignore PHPCS annotation comments if they are on a line by themselves.
return;
}

unset($prevNonWhiteSpace);
}

$lineLength = ($tokens[$stackPtr]['column'] + $tokens[$stackPtr]['length'] - 1);
Expand All @@ -135,8 +133,14 @@ protected function checkLineLength($phpcsFile, $tokens, $stackPtr)
if ($tokens[$stackPtr]['code'] === T_COMMENT
|| $tokens[$stackPtr]['code'] === T_DOC_COMMENT_STRING
) {
// The current line ends with a comment.
// If the comment is the only thing on the line and we are ignoring comments,
// we can stop processing the line here.
if ($this->ignoreComments === true) {
return;
$prevNonWhiteSpace = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
if ($tokens[$stackPtr]['line'] !== $tokens[$prevNonWhiteSpace]['line']) {
return;
}
}

// If this is a long comment, check if it can be broken up onto multiple lines.
Expand Down
2 changes: 2 additions & 0 deletions src/Standards/Generic/Tests/Files/LineLengthUnitTest.4.inc
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ if($thisIsOk === true) {}

/* This line is too long but will be ignored. This line is too long but will be ignored. */
if (($anotherReallyLongVarName === true) || (is_array($anotherReallyLongVarName) === false)) {}

if (($anotherReallyLongVarName === true) {} // This comment makes the line too long.
5 changes: 4 additions & 1 deletion src/Standards/Generic/Tests/Files/LineLengthUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ public function getWarningList($testFile='')
return [6 => 1];
break;
case 'LineLengthUnitTest.4.inc':
return [10 => 1];
return [
10 => 1,
12 => 1,
];
break;
default:
return [];
Expand Down

0 comments on commit 2fc1327

Please sign in to comment.