File::getDeclarationName(): stop accepting tokens for non-named structures#1007
Merged
jrfnl merged 3 commits intoApr 15, 2025
Merged
Conversation
... in anticipation of additional test case files for parse error being added.
Includes fixing up some inline comments for sniffs which don't search for closures, but could encounter live coding. Includes one minor bug fix/defensive coding fix in the `Squiz.Commenting.ClosingDeclarationComment` sniff.
…tures The `File::getDeclarationName()` method - for historic reasons - accepted the `T_CLOSURE` and `T_ANON_CLASS` tokens, even though these structures will never have a name, and returned `null` for those tokens. This commit changes the `File::getDeclarationName()` method to no longer accept those tokens and throw an exception if they are passed to the method instead. As a secondary change, when the name of a valid structure cannot be determined, the method will now no longer return `null`, but will return an empty string. This normalizes the return type of the method to always return a string (or throw an exception). Includes updated unit tests to match. Related to squizlabs/PHP_CodeSniffer 3766 --- All PHPCS native sniffs using the `File::getDeclarationName()` method have been reviewed and where necessary fixed to allow for this change. Also note that in some cases, the sniff already contained sufficient protection and in other case, the fact that the method will no longer return `null`, meant that the sniff code could be simplified. List of sniffs reviewed: - `AbstractScopeSniff` - `Generic.Classes.DuplicateClassName` - `Generic.CodeAnalysis.UnusedFunctionParameter` - `Generic.CodeAnalysis.UselessOverridingMethod` - `Generic.NamingConventions.AbstractClassNamePrefix` - `Generic.NamingConventions.CamelCapsFunctionName` - `Generic.NamingConventions.ConstructorName` - `Generic.NamingConventions.InterfaceNameSuffix` - `Generic.NamingConventions.TraitNameSuffix` - `PEAR.Commenting.ClassComment` - `PEAR.Commenting.FunctionComment` - `PEAR.Functions.FunctionDeclaration` - `PEAR.NamingConventions.ValidFunctionName` - `PSR1.Methods.CamelCapsMethodName` - `PSR2.Methods.MethodDeclaration` - `Squiz.Classes.ClassFileName` - `Squiz.Classes.SelfMemberReference` - `Squiz.Commenting.ClassComment` - `Squiz.Commenting.ClosingDeclarationComment` - `Squiz.Commenting.FunctionComment` - `Squiz.Functions.GlobalFunction` - `Squiz.NamingConventions.ValidFunctionName` - `Squiz.Scope.MethodScope` :point_right: The changes to the PEAR/FunctionDeclaration sniff will be easier to review while ignoring whitespace.
rodrigoprimo
approved these changes
Apr 15, 2025
petitphp
approved these changes
Apr 15, 2025
jrfnl
deleted the
phpcs-4.0/feature/file-getdeclarationname-stop-handling-non-named
branch
April 15, 2025 19:12
This was referenced Jun 4, 2025
4 tasks
This file contains hidden or 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
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.
Description
Tests: rename some test case files
... in anticipation of additional test case files for parse error being added.
Various sniffs: add extra tests with live coding/parse errors
Includes fixing up some inline comments for sniffs which don't search for closures, but could encounter live coding.
Includes one minor bug fix/defensive coding fix in the
Squiz.Commenting.ClosingDeclarationCommentsniff.File::getDeclarationName(): stop accepting tokens for non-named structures
The
File::getDeclarationName()method - for historic reasons - accepted theT_CLOSUREandT_ANON_CLASStokens, even though these structures will never have a name, and returnednullfor those tokens.This commit changes the
File::getDeclarationName()method to no longer accept those tokens and throw an exception if they are passed to the method instead.As a secondary change, when the name of a valid structure cannot be determined, the method will now no longer return
null, but will return an empty string.This normalizes the return type of the method to always return a string (or throw an exception).
Includes updated unit tests to match.
Related to squizlabs/PHP_CodeSniffer#3766
All PHPCS native sniffs using the
File::getDeclarationName()method have been reviewed and where necessary fixed to allow for this change.Also note that in some cases, the sniff already contained sufficient protection and in other cases, the fact that the method will no longer return
null, meant that the sniff code could be simplified.List of sniffs reviewed:
AbstractScopeSniffGeneric.Classes.DuplicateClassNameGeneric.CodeAnalysis.UnusedFunctionParameterGeneric.CodeAnalysis.UselessOverridingMethodGeneric.NamingConventions.AbstractClassNamePrefixGeneric.NamingConventions.CamelCapsFunctionNameGeneric.NamingConventions.ConstructorNameGeneric.NamingConventions.InterfaceNameSuffixGeneric.NamingConventions.TraitNameSuffixPEAR.Commenting.ClassCommentPEAR.Commenting.FunctionCommentPEAR.Functions.FunctionDeclarationPEAR.NamingConventions.ValidFunctionNamePSR1.Methods.CamelCapsMethodNamePSR2.Methods.MethodDeclarationSquiz.Classes.ClassFileNameSquiz.Classes.SelfMemberReferenceSquiz.Commenting.ClassCommentSquiz.Commenting.ClosingDeclarationCommentSquiz.Commenting.FunctionCommentSquiz.Functions.GlobalFunctionSquiz.NamingConventions.ValidFunctionNameSquiz.Scope.MethodScope👉 The changes to the PEAR/FunctionDeclaration sniff will be easier to review while ignoring whitespace.
Suggested changelog entry
Changed:
T_ANON_CLASSorT_CLOSUREtokens.nullif the name could not be determined, like during live coding.Now it will return an empty string in those situations.