-
-
Notifications
You must be signed in to change notification settings - Fork 57
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
Generic/DisallowYodaConditions: improve code coverage #465
Generic/DisallowYodaConditions: improve code coverage #465
Conversation
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.
@rodrigoprimo Thanks for this PR. I've left a few small comments inline to have a look at, but generally speaking, this is looking all good.
I've not done a detailed test vs sniff review as this is known to be a sniff with numerous bugs still to be fixed, so most issues are probably already known and/or will be found when addressing the known issues.
Not something which should hold back this PR.
src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php
Show resolved
Hide resolved
src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php
Outdated
Show resolved
Hide resolved
src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php
Outdated
Show resolved
Hide resolved
* Remove unused variable. * Fix typo in code comment. * Remove incorrect code comment.
Includes removing two unreachable conditions The first condition is unreachable because PHPCS will never call DisallowYodaConditionsSniff::process() if there is no non-empty token before a comparison token. Even if the file contains a syntax error, there must be at least a PHP opening tag before the comparison token for the method to be called. The second condition is unreachable because at this point in the code there will always be at least two non-empty tokens before the comparison token. If there is only one, it must be a PHP opening tag and, in this case, the method will bail before reaching the code that sets the `$prevIndex` variable. This commit also documents why a certain line is uncovered by tests - and cannot be covered, nor can it be removed as external sniffs may call `DisallowYodaConditionsSniff::isArrayStatic()` directly.
This commit renames a variable to use a better name that actually matches what the variable contains. The previous name was misleading as the variable did not contain the index of the closing parenthesis.
adcaad4
to
48a1a43
Compare
I've rebased the PR without changes (other than re-organizing the commits, but no changes to the code). Merging once the build has passed. |
This commit adds the `@codeCoverageIgnoreStart/@codeCoverageIgnoreEnd` PHPUnit annotations to ignore, for the purposes of code coverage, a line that can't be tested. I considered doing this initially when I worked on improving code coverage for this sniff in PHPCSStandards#465, but I wrongly assumed that was not an option due to the `Squiz.Commenting.InlineComment.InvalidEndChar` error (which is part of the standard used by PHPCS). What I failed to notice back then is that I was adding the annotation below a comment, and this triggers the error: ``` // Shouldn't be possible but may happen if external sniffs are using this method. // @codeCoverageIgnoreStart ``` Adding the annotation before the comment as it is done in this commit does not trigger the error because the `Squiz.Commenting.InlineComment` sniff will only trigger the `InvalidEndChar` error if the first character of the comment is a letter. Using `return true; // @codeCoverageIgnore` is not an option because it triggers `Squiz.Commenting.PostStatementComment.Found`.
Description
This PR improves code coverage for the Generic.ControlStructures.DisallowYodaConditions sniff. It adds a few tests for uncovered lines and documents using tests a few comparison operators that this sniff listens for that were not part of the tests before.
Besides that, this PR removes an unused variable, fixes a typo in a code comment, removes two unreachable variables, and renames a variable to make it more clear what it contains.
It is impossible to add code coverage for the line below using the current test framework and we should not remove it as external sniffs could be using the
DisallowYodaConditionsSniff::isArrayStatic()
and relying on the current behavior (returningtrue
if the passed token index is not an array):PHP_CodeSniffer/src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php
Line 152 in 30b3148
I left a comment documenting that the line cannot be covered by tests but cannot be removed as well.
I considered using
// @codeCoverageIgnore
or// @codeCoverageIgnoreStart
and// @codeCoverageIgnoreEnd
, but that is not possible as well as those annotations trigger different sniff errors for the coding standard used by PHPCS (Squiz.Commenting.PostStatementComment.Found
andSquiz.Commenting.InlineComment.InvalidEndChar
). I will open a separate issue for this.Related issues/external references
Part of #146
Types of changes
PR checklist