Skip to content
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

Squiz/ClosingDeclarationComment: fix a non-problematic bug plus some other small improvements #381

Conversation

rodrigoprimo
Copy link
Contributor

Description

This PR makes the following changes to the Squiz.Commenting.ClosingDeclarationComment sniff:

  • Improve test coverage
  • Remove an unreachable condition
  • Prevent a non-problematic bug on line 96 where the code if (rtrim($tokens[$next]['content']) === $comment) was not taking into account that $next can be false. To fix it, a $next !== false check was added to the if condition.

@jrfnl suggested those changes while reviewing another PR: #340 (comment). In the same comment, it was also suggested that the sniff should handle traits. This will be addressed in a separate PR.

I noticed that this sniff triggers a warning when there is a missing opening or closing bracket. I imagine this could be misleading when using PHPCS with live code, and it is the first sniff that I see doing this. I'm highlighting this behavior in case we want to address it in another PR. For now, I added two tests documenting this behavior.

Suggested changelog entry

Squiz.Commenting.ClosingDeclarationComment: fixed a non-problematic bug when $next is false

Related issues/external references

Related to #340 (comment) and #146

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
    • This change is only breaking for integrators, not for external standards or end-users.
  • Documentation improvement

PR checklist

  • I have checked there is no other PR open for the same change.
  • I have read the Contribution Guidelines.
  • I grant the project the right to include and distribute the code under the BSD-3-Clause license (and I have the right to grant these rights).
  • I have added tests to cover my changes.
  • I have verified that the code complies with the projects coding standards.
  • [Required for new sniffs] I have added XML documentation for the sniff.

@jrfnl
Copy link
Member

jrfnl commented Mar 6, 2024

I noticed that this sniff triggers a warning when there is a missing opening or closing bracket. I imagine this could be misleading when using PHPCS with live code, and it is the first sniff that I see doing this. I'm highlighting this behavior in case we want to address it in another PR. For now, I added two tests documenting this behavior.

@rodrigoprimo There are currently a small number of sniffs which do this and this behaviour will be removed in PHPCS 4.0 as per squizlabs/PHP_CodeSniffer#2455

Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @rodrigoprimo Sorry for taking so long before reviewing this PR properly.

We've now looked at it together in a call, so you are already aware of the remarks I'm making.

Overall: looking good! Just some small changes needed to finish this off properly.

Comment on lines 88 to 89
function misplacedClosingCommentIndentation() {
} //end misplacedClosingCommentIndentation()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you meant to address this comment with this test ?

No tests with a misplaced comment with indentation.

While I do like this new test and I think we should keep it (though maybe rename the function ?), it doesn't actually address the above comment as the function keyword and the close brace are still at column 1 (start of line).

With indentation in the above comment, I meant a function/method in a class which has whitespace before the function keyword and before the closing brace, then a new line and then a closing comment on the next line, again with indentation before it.

And while writing this, I can think of another few variations, like:

function CommentHasMoreIndentationThanFunction() {
}
         //end CommentHasMoreIndentationThanFunction()

            function CommentHasLessIndentationThanFunction() {
            }
    //end CommentHasLessIndentationThanFunction()

@@ -0,0 +1,8 @@
<?php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is a good test to have anyway, this test doesn't actually highlight the problem with the missing $next !== false condition.

We looked at this during our call and the following additional test should be added to safeguard the fix:

//end closingBraceAtEndOfFileMissingComment()

<?php

function closingBraceAtEndOfFileMissingComment() {
}

This test will show that without that extra condition the wrong error is being thrown (though an error is still being thrown).

Note: as the test framework as-is doesn't safeguard that the correct error code is being thrown for each line, it's not a hard safeguard, but having the test in place will allow us to safeguard this properly if/when the test framework gets updates to check per error code/per line.

@rodrigoprimo
Copy link
Contributor Author

rodrigoprimo commented Apr 8, 2024

Thanks for the review, @jrfnl!

I just pushed a few new commits implementing all the changes that you suggested and this PR is ready for a final check.

Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for those updates @rodrigoprimo ! All looks good to me now!

This is necessary to add more tests that need to go into separate files.
This commit improve the test coverage for the
Squiz.Commenting.ClosingDeclarationComment sniff by adding the following
tests:

- Function without a closing comment
- Misplaced closing comment with indentation
- Misplaced closing comment with multiple newlines
- Class missing a opening or closing bracket

It also documents via tests that anonymous classes and arrow functions
do not require a closing comment.
This commit removes an if condition that cannot be true. The
`$closingBracket` variable can never be null as the sniff already bows
out before setting the variable when checking if
`isset($tokens[$stackPtr]['scope_closer']) === false`.
This commit fixes a non-problematic bug on line 96. The code `if
(rtrim($tokens[$next]['content']) === $comment)` was not taking into
account that `$next` can be false. To fix it, a `$next !== false` check
was added to the if condition.

This problem did not cause any issues because when `$next` is `false`, the
code would check the content of the first token
(`$tokens[0]['content']`) and this token can only be the PHP open tag or
inline HTML. The code would only produce false positives if the content
of the first token would be `// end ...` which is unlikely and would be
invalid HTML.

A test was added exercising the code path where `$next` is `false`.

And another tests which actually hits the bug and safeguards against potential regressions for the
non-problematic bug fix.
@jrfnl jrfnl force-pushed the closing-declaration-comment-improvements branch from 1533fe5 to 5b597d8 Compare April 9, 2024 13:29
@jrfnl
Copy link
Member

jrfnl commented Apr 9, 2024

Rebased without changes, just some reorganization of the commits. Merging once the build passes.

@jrfnl jrfnl added this to the 3.9.2 milestone Apr 9, 2024
@jrfnl jrfnl merged commit 2d4783c into PHPCSStandards:master Apr 9, 2024
38 checks passed
@rodrigoprimo rodrigoprimo deleted the closing-declaration-comment-improvements branch April 9, 2024 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants