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

fix(ScopeKeywordSpacing): Check spaces after final keyword #604

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

klausi
Copy link
Contributor

@klausi klausi commented Sep 1, 2024

The spacing after the final keyword should be checked by ScopeKeywordSpacingSniff.

Description

This should throw an error because there are 6 spaces after final:

class FinalTest {
    final      public static function create(ContainerInterface $container) {}
}

Suggested changelog entry

Fixed bug XYZ : Squiz.WhiteSpace.ScopeKeywordSpacing detect white space problems after final keyword.

Related issues/external references

Fir reported at Drupal's coder: https://www.drupal.org/project/coder/issues/3468410

Types of changes

Bug fix

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
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 @klausi, thanks for this PR!

While in principle I agree with this change, the change you've made does more than "it says on the box".
Previously, the sniff checked the spacing after visibility keywords, static and readonly.
Now, it will also check final and abstract.

So this PR is not just an addition of final, but also of abstract to the tokens which are being checked.

I think, originally, the sniff was only intended for method and property prefixes.
And even though abstract and final could be used on methods, they were not included in the sniff as they would also affect class declarations.

Checking of scope keywords for anonymous functions snuck in with the introduction of static closures in PHP 5.4 and then for arrow functions in PHP 7.4
Checking of scope keywords for class constant sort of snuck in with the introduction of constant visibility in PHP 7.1 (and final constants in PHP 8.1 if this change is accepted).
And the introduction of the readonly keyword for properties being handled in PHP 8.1, had a side-effect that as of PHP 8.2, the readonly keyword would also be checked for class declarations.

So, to come back to the proposed change in this PR. While it would now formally widen the scope of the sniff from "methods and properties" to "methods, properties, class constants and class declaration", I don't think that's a bad thing, so I have no objections against this PR.

I do, however, think that this means more tests are needed.

  • Tests with final and abstract on class declarations.
  • Tests with final on class constant declarations.

And if adding extra tests anyway, there should probably also be tests with the visibility keywords and class constants. And tests for static arrow functions also look to be missing.

Note: these last two test additions would not be a blocker for this PR, but would still need to be done anyhow, either in this PR or in a follow-up PR.

@fredden
Copy link
Member

fredden commented Sep 2, 2024

Given these keywords can be used in more than one context, perhaps we should be producing more than one error code (ie, one per context, possibly also one per keyword?). We currently only throw Incorrect.

To avoid a significant breaking change, we should endeavour to maintain the existing code for the intended/existing behaviour, and add a new code for new contexts (ie, class declaration). So, perhaps keep Incorrect for method / property / class constant contexts, and a new error code for class declaration context.

@jrfnl
Copy link
Member

jrfnl commented Sep 29, 2024

@klausi Just checking in. It would be great to include this change in the 3.11.0 release if it is ready in time. Will you have a chance to finish off the work on this ?

@klausi
Copy link
Contributor Author

klausi commented Sep 30, 2024

Hi, currently swamped with other work, so it could take some time until I can check back. Feel free to take over or postpone it for a future release!

@jrfnl
Copy link
Member

jrfnl commented Sep 30, 2024

Hi, currently swamped with other work, so it could take some time until I can check back. Feel free to take over or postpone it for a future release!

Hi @klausi Thanks for letting me know. I understand, just wanted to prevent this PR from being forgotten as it is definitely a good candidate for merge. Pretty swamped myself atm, so take your time and we'll touch base again in a while ;-)

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.

3 participants