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

Generic/EmptyPHPStatement: improve code coverage #699

Merged

Conversation

rodrigoprimo
Copy link
Contributor

@rodrigoprimo rodrigoprimo commented Nov 20, 2024

Description

This PR improves code coverage for the Generic.CodeAnalysis.EmptyPHPStatement sniff.

While adding tests for the short open tag, I noticed a behavior in the sniff related to #593 that we might want to address in this PR or in a separate PR if it is not intentional behavior. The sniff fixes <? ; something_else(); ?> to <?something_else(); ?>. Note that there is no space between the PHP open tag and the function call. Should the sniff change to preserve one space between the PHP open tag and the function call? It is probably not a high priority as the resulting code is not a parse error, and short open tags are not very popular.

Update: question above moved to #728

Related issues/external references

Part of #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 Nov 25, 2024

Please move the question regarding the behaviour with short open tags to its own issue (linking to this PR). Different decision point, so it doesn't belong in this PR.

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.

Looking good. Left a few nitpicks in-line, but other than that, all good.

Now wondering whether the sniff would benefit from a test with multiple superfluous semi-colons after a statement and/or between tags ?

@rodrigoprimo
Copy link
Contributor Author

Please move the question regarding the behaviour with short open tags to its own issue (linking to this PR). Different decision point, so it doesn't belong in this PR.

#728

@rodrigoprimo
Copy link
Contributor Author

rodrigoprimo commented Nov 25, 2024

Thanks for reviewing this PR, @jrfnl! I implemented everything you suggested, and this is now ready for a second review.

I split my changes into three different commits as I believe they should be squashed into three different commits before this PR is merged. My suggestion is to combine the second commit that removes an unreachable condition with d23d5ae and edit its commit message to reflect this change.

Now wondering whether the sniff would benefit from a test with multiple superfluous semi-colons after a statement and/or between tags ?

I think it is a good idea. There is already a test with two superfluous semi-colons after a class definition (src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.1.inc#L63), so I updated other existing tests to include a test with two superfluous semi-colons between PHP tags and a test with two superfluous semi-colons between PHP tags and after a statement.

@rodrigoprimo
Copy link
Contributor Author

@jrfnl, just FYI, the Windows build failed, and I don't think the problem is related to the changes in the PR: https://github.com/PHPCSStandards/PHP_CodeSniffer/actions/runs/12016963721/job/33498304890?pr=699

It seems it was a Coveralls hiccup: https://github.com/PHPCSStandards/PHP_CodeSniffer/actions/runs/12016963721/job/33498304890?pr=699#step:19:193

@rodrigoprimo
Copy link
Contributor Author

I believe you ran the build again while I was typing the previous comment and it passed. Thanks!

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 @rodrigoprimo! I'll rebase & squash select commits and will merge this PR once the build has passed.

* Removed two unreachable conditions.
    `$prevNonEmpty` will never be `false` as there will always be
    at least a PHP open tag token before the semicolon token.
* Removed unreachable default case
    `$tokens[$stackPtr]['type']` will always be either `T_SEMICOLON` or
    `T_CLOSE_TAG` as those are the two tokens that this sniff listens for.
    So, the default case will never be reached.
Doing this to be able to create another test file to test the sniff
with the short open tag.
* Updated tests to have more examples with more than one superfluous semicolon.
* Minor test comment punctuation tweaks.
@jrfnl jrfnl force-pushed the test-coverage-empty-php-statement branch from ed6233e to eb7c3a9 Compare November 26, 2024 03:59
@jrfnl jrfnl added this to the 3.11.2 milestone Nov 26, 2024
@jrfnl jrfnl enabled auto-merge November 26, 2024 04:01
@jrfnl jrfnl merged commit c88c241 into PHPCSStandards:master Nov 26, 2024
58 checks passed
@jrfnl jrfnl deleted the test-coverage-empty-php-statement branch November 26, 2024 04:28
jrfnl added a commit that referenced this pull request Nov 26, 2024
Follow up on #699

Commit 529bcba removed the unreachable `default` case from the `switch`, but even when it is not explicitly there, this still leaves an unreachable branch in the code flow.

This commit takes the previous change one step further and removes the `switch` completely in favour of two separate `private` functions which each handle one specific token.

N.B.: this commit will be easier to review while ignoring whitespace changes.
jrfnl added a commit that referenced this pull request Nov 26, 2024
Follow up on #699

Commit 529bcba removed the unreachable `default` case from the `switch`, but even when it is not explicitly there, this still leaves an unreachable branch in the code flow.

This commit takes the previous change one step further and removes the `switch` completely in favour of two separate `private` functions which each handle one specific token.

N.B.: this commit will be easier to review while ignoring whitespace changes.
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