-
-
Notifications
You must be signed in to change notification settings - Fork 471
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
Core: add section about import use statement rules with select new sniffs #2252
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…iffs > ### Using import use statements > > Using import `use` statements allows you to refer to constants, functions, classes, interfaces, namespaces, enums and traits that live outside of the current namespace. > > Import `use` statements should be at the top of the file and follow the (optional) `namespace` declaration. They should follow a specific order based on the **type** of the import: > > 1. `use` statements for namespaces, classes, interfaces, traits and enums > 2. `use` statements for functions > 3. `use` statements for constants > > Aliases can be used to prevent name collisions (two classes in different namespaces using the same class name). > When using aliases, make sure the aliases follow the WordPress naming convention and are unique. > > The following examples showcase the correct and incorrect usage of import use statements regarding things like spacing, groupings, leading backslashes, etc. Refs: * https://make.wordpress.org/core/2020/03/20/updating-the-coding-standards-for-modern-php/ - Import use statement section * https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/#using-import-use-statements * WordPress/wpcs-docs 98 * PHPCSStandards/PHPCSExtra 46 * PHPCSStandards/PHPCSExtra 58 Notes: * The "Import use statements should be at the top of a file and should directly follow the (optional) namespace declaration." rule is _sort of_ covered by the `PSR12.Files.FileHeader.IncorrectOrder` sniff. This sniff will flag incorrect order when a `use` statement is part of the file header, but will **not** pick up on `use` statements which are not at the top of the file. * The "There should be exactly one blank line before the first import use statement of each type and at least one blank line after the last import use statement." will probably need a new sniff. My research shows the following: - The `PSR12.Files.FileHeader` sniff can check both "before" and "after", but will check for exactly one blank line. The problem with that sniff is that it currently is "all or nothing", it does not have modular error codes for the various file headers sections for the blank line check, so we cannot ignore some other things from that sniff (requires blank line between PHP open tag and file docblock), which makes it problematic to include the sniff. If upstream PR squizlabs/PHP_CodeSniffer 2729 would (finally) be merged, we could reconsider adding that sniff though. * The "There should be exactly one space after each keyword." rule is currently only checked for the `use` keyword via the `Generic.WhiteSpace.LanguageConstructSpacing` sniff, which was moved to `Core` in 2097. PHPCSExtra 1.1.0 will contain a new `Universal.UseStatements.KeywordSpacing` sniff which can check the spacing around the `function`, `const` and `as` keywords. * The "Alias names should comply with the existing WordPress naming conventions for class/function/constant names." rule will need a new dedicated sniff. An issue will need to be opened about this in WPCS. The corresponding issue in PHPCSExtra is PHPCSStandards/PHPCSExtra 232 * The "Only set an alias when using a different name." rule as mentioned in the Make post is currently not checked. PHPCSExtra 1.1.0 will contain a new `Universal.UseStatements.NoUselessAliases` sniff which will be able to check this. * The "When using group use statements ... There should be one statement for each type – OO constructs (classes/interfaces/traits), functions, constants. The various types should not be combined in one group use statement." rule as mentioned in the Make post is currently not checked. PHPCSExtra 1.1.0 will contain a new `Universal.UseStatements.DisallowMixedGroupUse` sniff which will be able to check this. * As for the formatting rules for group use statements: there are currently no sniffs available for this, so a new sniff is needed to check this. Other notes: * The "There should be no whitespace or comments within the name part of a use declaration.", as mentioned in the Make post, can (and should) be flagged by PHPCompatibility 10.0.
GaryJones
approved these changes
Jun 19, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅
dingo-d
approved these changes
Jun 19, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Refs:
Universal.UseStatements.NoLeadingBackslash
sniff PHPCSStandards/PHPCSExtra#46Universal.UseStatements.LowercaseFunctionConst
sniff PHPCSStandards/PHPCSExtra#58Notes:
PSR12.Files.FileHeader.IncorrectOrder
sniff.This sniff will flag incorrect order when a
use
statement is part of the file header, but will not pick up onuse
statements which are not at the top of the file.My research shows the following:
PSR12.Files.FileHeader
sniff can check both "before" and "after", but will check for exactly one blank line.The problem with that sniff is that it currently is "all or nothing", it does not have modular error codes for the various file headers sections for the blank line check, so we cannot ignore some other things from that sniff (requires blank line between PHP open tag and file docblock), which makes it problematic to include the sniff.
If upstream PR PSR12/FileHeader: make "SpacingAfter" and "SpacingInside" errorcodes modular squizlabs/PHP_CodeSniffer#2729 would (finally) be merged, we could reconsider adding that sniff though.
use
keyword via theGeneric.WhiteSpace.LanguageConstructSpacing
sniff, which was moved toCore
in Core: move rules related to include/require statements fromExtra
toCore
#2097.PHPCSExtra 1.1.0 will contain a new
Universal.UseStatements.KeywordSpacing
sniff which can check the spacing around thefunction
,const
andas
keywords.An issue will need to be opened about this in WPCS. The corresponding issue in PHPCSExtra is Sniff to enforce naming conventions for class/function/const aliases PHPCSStandards/PHPCSExtra#232
PHPCSExtra 1.1.0 will contain a new
Universal.UseStatements.NoUselessAliases
sniff which will be able to check this.PHPCSExtra 1.1.0 will contain a new
Universal.UseStatements.DisallowMixedGroupUse
sniff which will be able to check this.Other notes: