-
-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PEAR/FunctionComment: bug fix - handling of blank lines in pre-amble #830
Open
jrfnl
wants to merge
1
commit into
master
Choose a base branch
from
feature/pear-functioncomment-improve-blank-lines-between-check
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,33 +119,44 @@ public function process(File $phpcsFile, $stackPtr) | |
return; | ||
} | ||
|
||
// Check there are no blank lines in the preamble for the property, | ||
// but ignore blank lines _within_ attributes as that's not the concern of this sniff. | ||
if ($tokens[$commentEnd]['line'] !== ($tokens[$stackPtr]['line'] - 1)) { | ||
for ($i = ($commentEnd + 1); $i < $stackPtr; $i++) { | ||
if ($tokens[$i]['column'] !== 1) { | ||
// Skip over the contents of attributes. | ||
if (isset($tokens[$i]['attribute_closer']) === true) { | ||
$i = $tokens[$i]['attribute_closer']; | ||
continue; | ||
} | ||
|
||
if ($tokens[$i]['code'] === T_WHITESPACE | ||
&& $tokens[$i]['line'] !== $tokens[($i + 1)]['line'] | ||
if ($tokens[$i]['column'] !== 1 | ||
|| $tokens[$i]['code'] !== T_WHITESPACE | ||
|| $tokens[$i]['line'] === $tokens[($i + 1)]['line'] | ||
// Do not report blank lines after a PHPCS annotation as removing the blank lines could change the meaning. | ||
|| isset(Tokens::$phpcsCommentTokens[$tokens[($i - 1)]['code']]) === true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the above, have you considered adding a test where |
||
) { | ||
$error = 'There must be no blank lines after the function comment'; | ||
$fix = $phpcsFile->addFixableError($error, $commentEnd, 'SpacingAfter'); | ||
continue; | ||
} | ||
|
||
if ($fix === true) { | ||
$phpcsFile->fixer->beginChangeset(); | ||
$nextNonWhitespace = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), null, true); | ||
$error = 'There must be no blank lines between the function comment and the declaration'; | ||
$fix = $phpcsFile->addFixableError($error, $i, 'SpacingAfter'); | ||
|
||
while ($i < $stackPtr | ||
&& $tokens[$i]['code'] === T_WHITESPACE | ||
&& $tokens[$i]['line'] !== $tokens[($i + 1)]['line'] | ||
) { | ||
$phpcsFile->fixer->replaceToken($i++, ''); | ||
if ($fix === true) { | ||
$phpcsFile->fixer->beginChangeset(); | ||
|
||
for ($j = $i; $j < $nextNonWhitespace; $j++) { | ||
if ($tokens[$j]['line'] === $tokens[$nextNonWhitespace]['line']) { | ||
break; | ||
} | ||
|
||
$phpcsFile->fixer->endChangeset(); | ||
$phpcsFile->fixer->replaceToken($j, ''); | ||
} | ||
|
||
break; | ||
$phpcsFile->fixer->endChangeset(); | ||
} | ||
|
||
$i = $nextNonWhitespace; | ||
}//end for | ||
}//end if | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you considered adding a test where this condition is
true
and$tokens[$i]['column']
isfalse
? As far as I can check, in the current tests whenever$tokens[$i]['code'] !== T_WHITESPACE
istrue
,$tokens[$i]['column'] !== 1
is alsotrue
. Thus the former is never executed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I did not, as those are pre-existing conditions which I've not changed. I've just grouped them into one
if - continue
as having multiple didn't make much sense.Writing tests for pre-existing code is outside the scope of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is fair.
I asked because this condition was inverted. There are test cases that cover the first token of a line between the comment end and the function declaration being
T_WHITESPACE
, but there is no test case for it not beingT_WHITESPACE
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are inverted because the flow has changed from "complies with condition -> throw error" to "does not comply with condition -> continue without error".
That is not a functional change. The behaviour of the sniff with regards to those conditions is unchanged - and outside the scope of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not saying the sniff couldn't use more tests, most sniffs can. Those additional tests though, do not belong with the functional change this PR is making, so should be added in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me.