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/OpeningFunctionBraceKernighanRichie: improve test coverage #707

Conversation

rodrigoprimo
Copy link
Contributor

Description

This PR improves code coverage for the Generic.Functions.OpeningFunctionBraceKernighanRichie sniff and makes some tiny improvements to the sniff code. No user-facing changes were made.

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.

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.

@rodrigoprimo Thanks for this PR. I think it's a great start, but can be improved further.

Looking at the tests, I see the following:

Missing:

  • Test with an abstract method (no scope opener).
  • Live coding test missing the opening curly.
  • Live coding test with an open curly, but missing the close curly x 3 - one with correct spacing, one with too much space before the curly, one with no space before the curly. Both these last two should be fixable (and fixed).

Superfluous:

  • Live coding test missing open parentheses. The sniff is not looking at that.

Looking at the handling of tabs... I don't think the multi-tab situation is handled correctly.

If you run the sniff against test case file 2, you will get the following error for the multi-tab line:

 10 | ERROR | [x] Expected 1 space before opening brace; found 3 (Generic.Functions.OpeningFunctionBraceKernighanRitchie.SpaceBeforeBrace)

... which has no indication that this is 3 tabs, not 3 spaces.

If tabs are called out separately anyway (as is done in this sniff), let's do it correctly and consistently.

And if we're talking redundant code, I think the whole code block from line 75 to line 82 can be removed as long as the $closeBracket reference on line 85 is replaced with null.

What do you think ?

@rodrigoprimo rodrigoprimo force-pushed the test-converage-opening-function-brace-kernighan-ritchie branch from e4f38f1 to 0111d93 Compare November 26, 2024 19:59
@rodrigoprimo
Copy link
Contributor Author

Thanks for your detailed review, @jrfnl!

Test with an abstract method (no scope opener).

There is an abstract method declared in an interface, and I assumed it was enough as the sniff doesn't really check where the method is defined. That being said, having an abstract method declared in a class is not a problem, so I went ahead and added one.

Live coding test missing the opening curly.

👍

Live coding test with an open curly, but missing the close curly x 3 - one with correct spacing, one with too much space before the curly, one with no space before the curly. Both these last two should be fixable (and fixed).

I might be missing something, but it seems to me the sniff will bail early in those three cases, as when there is no closing curly brace, scope_opener is not set.

This is one of the code examples that I have in mind reading your comment:

function missingClosingCurlyBrace()     {

I'm assuming you are not suggesting that I change the behavior of the sniff to handle those cases.

Live coding test missing open parentheses. The sniff is not looking at that.

I replaced this test with a test for the missing opening curly brace.

Looking at the handling of tabs... I don't think the multi-tab situation is handled correctly.
If you run the sniff against test case file 2, you will get the following error for the multi-tab line:
10 | ERROR | [x] Expected 1 space before opening brace; found 3 (Generic.Functions.OpeningFunctionBraceKernighanRitchie.SpaceBeforeBrace)
... which has no indication that this is 3 tabs, not 3 spaces.
If tabs are called out separately anyway (as is done in this sniff), let's do it correctly and consistently.

I agree, but I don't see an easy and straightforward way to fix it. Especially as there could be a mix of spaces and tabs used. Would it be ok to open an issue for this instead of fixing it in this PR?

And if we're talking redundant code, I think the whole code block from line 75 to line 82 can be removed as long as the $closeBracket reference on line 85 is replaced with null.
What do you think ?

Good catch! I removed it in a new commit.

@jrfnl
Copy link
Member

jrfnl commented Nov 27, 2024

Live coding test with an open curly, but missing the close curly x 3 - one with correct spacing, one with too much space before the curly, one with no space before the curly. Both these last two should be fixable (and fixed).

I might be missing something, but it seems to me the sniff will bail early in those three cases, as when there is no closing curly brace, scope_opener is not set.

This is one of the code examples that I have in mind reading your comment:

function missingClosingCurlyBrace()     {

I'm assuming you are not suggesting that I change the behavior of the sniff to handle those cases.

Well, sort of. I mean, this sniff is limiting its own ability by checking for the scope_opener instead of checking for a T_OPEN_CURLY_BRACKET after the function signature.

Having said that, I agree that should probably be left as "future scope" (and low priority).

Looking at the handling of tabs... I don't think the multi-tab situation is handled correctly.
If you run the sniff against test case file 2, you will get the following error for the multi-tab line:
10 | ERROR | [x] Expected 1 space before opening brace; found 3 (Generic.Functions.OpeningFunctionBraceKernighanRitchie.SpaceBeforeBrace)
... which has no indication that this is 3 tabs, not 3 spaces.
If tabs are called out separately anyway (as is done in this sniff), let's do it correctly and consistently.

I agree, but I don't see an easy and straightforward way to fix it. Especially as there could be a mix of spaces and tabs used. Would it be ok to open an issue for this instead of fixing it in this PR?

I do see a straight-forward way to fix this, so I will submit a follow-up PR after merging this one. No need for the issue.

…findPrevious()

This commit removes a repeated call to findPrevious() to get the pointer
to the end of the function declaration. The same `$prev` variable that
was defined a few lines above can be reused and there is no need to
define it again calling `findPrevious()` with exactly the same
parameters.
… in a fixer

This commit makes a tiny performance improvement to the sniff when
handling fixing code with tabs. Before this change, the sniff would need
two passes of the fixer for code with one tab before the opening brace.
First, it would add a space between the tab and the opening brace. Then,
on a second pass, it would replace the tab and the space with just one
space.

Now, it replaces the tab with the space directly without the need for a
second pass.
Doing this to be able to create tests with syntax errors on separate
files.
Includes removing unnecessary trailing spaces from empty lines in the test file.
…end of the function signature

When finding the end of the function signature, the sniff had an
unnecessary block of code to change the `$end` parameter passed to
`findPrevious()` when handling a closure with a `use` statement.

Since we are looking for the first non-empty token before the opening
curly brace, it is not necessary to change the value of the `$end`
parameter. Actually, we don't even need to limit the search and we can
pass `null` (the default value). The returned first non-empty token will
be the same anyway regardless if `$end` is the closing parenthesis
before `use`, after `use` or `null`.
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.

I'm going to rebase the PR and squash a few commits together. Merging once the build has passed.

@jrfnl jrfnl force-pushed the test-converage-opening-function-brace-kernighan-ritchie branch from 0111d93 to 286cd81 Compare November 27, 2024 09:49
@jrfnl jrfnl added this to the 3.11.2 milestone Nov 27, 2024
@jrfnl jrfnl merged commit 941a00e into PHPCSStandards:master Nov 27, 2024
45 checks passed
@jrfnl jrfnl deleted the test-converage-opening-function-brace-kernighan-ritchie branch November 27, 2024 10:21
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