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

Auto-fix PEAR.Commenting.FunctionComment.SpacingAfter #3908

Merged

Conversation

fredden
Copy link
Contributor

@fredden fredden commented Oct 27, 2023

Description

I have witnessed some complaints coming from Squiz.Commenting.FunctionComment.SpacingAfter which look like they could be trivially auto-fixed. This pull request adds auto-fixing for this particular complaint. I intend to investigate feasibility of auto-fixing other cases in this sniff another time.

Suggested changelog entry

Allow auto-fixing ...FunctionComment.SpacingAfter

Types of changes

  • New feature (non-breaking change which adds functionality)

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.

Copy link
Contributor

@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.

@fredden Thanks for proposing this change. The principle of the change makes sense to me, however not so, the implementation.

Consider the following code:

/**
 * Has space after
 */




function foo() {}

With the proposed change, the fixer would need 7 passes before the issue is fixed as each time only one line is removed + the fixer will skip a few passes due to a suspicion of a conflict.
You can check this yourself by running the fixer with -vvv.

So, instead of the currently proposed change, the implementation should remove all blank lines between the comment closer and the line containing the function keyword in one go.


For the record: this change will affect the following sniffs:

  • PEAR.Commenting.FunctionComment (sniff in which the change is being made)
  • Squiz.Commenting.FunctionComment (extends the PEAR sniff)
  • MySource.Commenting.FunctionComment (extends the PEAR sniff)

@fredden
Copy link
Contributor Author

fredden commented Oct 28, 2023

So, instead of the currently proposed change, the implementation should remove all blank lines between the comment closer and the line containing the function keyword in one go.

Yes, this makes sense. I'll look at making this change in the coming days.

@fredden fredden force-pushed the auto-fix/function-comment/spacing-after branch from 86cb183 to a69c1d6 Compare October 29, 2023 15:44
@fredden fredden requested a review from jrfnl October 29, 2023 15:46
@fredden fredden force-pushed the auto-fix/function-comment/spacing-after branch from a69c1d6 to 1b5bad7 Compare October 29, 2023 15:56
Copy link
Contributor

@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.

@fredden Thanks for updating the patch, but this is still not correct.

With the patch as is, if any of the blank lines between the comment closer and the first code has spaces/tabs on them (still a blank line), the fixer will now go into conflict state.

Try it by adding some spaces on one of the lines in the code sample I provided and see the "FAILED TO FIX" appear.

The test I provided should also be added.

@fredden fredden force-pushed the auto-fix/function-comment/spacing-after branch from 1b5bad7 to 92b0c99 Compare October 30, 2023 11:23
@fredden
Copy link
Contributor Author

fredden commented Oct 30, 2023

Thanks for the feedback @jrfnl.

With the patch as is, if any of the blank lines between the comment closer and the first code has spaces/tabs on them (still a blank line), the fixer will now go into conflict state.

I did consider the case where there are lines with only spaces/tabs. I thought that another sniff (like Squiz.WhiteSpace.SuperfluousWhitespace) should handle removing trailing white-space from such lines. I did not however check that this sniff would not produce a "failed to fix" situation in that case.

I have updated the code to treat lines containing only spaces/tabs as blank too. I'm now using exactly the same condition/logic to determine if a line is "blank" in the fixer as was used to identify the issue and add the error.

I have added tests to cover these scenarios.

Copy link
Contributor

@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.

@fredden I haven't looked at the complete updated patch yet, just the change in the sniff.

Getting closer, but as the fixer now potentially touches more than one token, the fixer block should be wrapped within a changeset as otherwise the fixer can not correctly revert changes when it detects conflicts.

if ( $fix === true ) {
    $phpcsFile->fixer->beginChangeset();
    ...
    $phpcsFile->fixer->endChangeset();
}

@fredden fredden force-pushed the auto-fix/function-comment/spacing-after branch from 92b0c99 to 9a5b392 Compare October 30, 2023 11:44
@fredden fredden requested a review from jrfnl October 30, 2023 11:44
Copy link
Contributor

@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.

@fredden Thanks for making those updates. Looks like this is now working correctly, including correct handling of attributes (not removing the indent whitespace). ✅

@jrfnl jrfnl merged commit 9187910 into squizlabs:master Nov 2, 2023
26 of 27 checks passed
@jrfnl jrfnl added this to the 3.8.0 milestone Nov 2, 2023
jrfnl added a commit that referenced this pull request Nov 2, 2023
@fredden fredden deleted the auto-fix/function-comment/spacing-after branch November 2, 2023 11:53
@jrfnl
Copy link
Contributor

jrfnl commented Dec 8, 2023

FYI: this fix is included in today's PHP_CodeSniffer 3.8.0 release.

As per #3932, development on PHP_CodeSniffer will continue in the PHPCSStandards/PHP_CodeSniffer repository. If you want to stay informed, you may want to start "watching" that repo (or watching releases from that repo).

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