Match attributes on methods and functions with allowInMethods and allowInFunctions - #428
Merged
Merged
Conversation
…allowInFunctions` A disallowed attribute placed on a method or a function is technically not *in* the method or the function, so `$scope->getFunction()` returns `null` and the name compared against the `allowInMethods` (and aliases) patterns would always be empty, never matching anything. The name is now resolved from the node when there's no function in the scope, the same way `allowInMethodsWithAttributes` already reads the attributes off the node, and produces the same name the reflection-based branches would produce for the same symbol. This also means `allowExceptInMethods` (and aliases) can now report an attribute placed on a matching method or function, which was previously always allowed. Close #427
The item pointed to `docs/allow-in-methods.md`, the same file as the by-name item above it, most likely a copy-paste leftover, while the attributes variant has its own file in `docs/allow-in-methods-with-attributes.md`.
There was a problem hiding this comment.
Pull request overview
Fixes name-based allowInMethods / allowInFunctions matching for attributes placed on method/function declarations by resolving the symbol name from the AST node when $scope->getFunction() is null, aligning behavior with the existing attribute-based allow mechanisms and enabling allowExceptInMethods to correctly report such cases.
Changes:
- Update
Allowed::callMatches()to accept the current AST node and derive method/function names fromClassMethod/Function_nodes when needed. - Add a dedicated fixture and rule test covering
allowInMethods,allowExceptInMethods, andallowInFunctionsfor attribute placements (on class/method/property/nested function/top-level function). - Update documentation to clarify that attribute allow/disallow-by-name applies to attributes placed on methods/functions as well as inside them, and fix the README link to the correct “with-attributes” doc.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
src/Allowed/Allowed.php |
Threads the AST node into call-context matching and adds node-based name resolution for method/function declaration nodes. |
tests/Usages/AttributeUsagesAllowInMethodsTest.php |
Adds coverage for allow/except matching on attributes applied directly to method/function declarations. |
tests/src/AttributeMethods.php |
Provides a fixture exercising allowed/disallowed attribute placements across methods, nested functions, properties, classes, and functions. |
README.md |
Corrects the README link for the “methods/functions with given attributes” feature. |
docs/allow-in-methods.md |
Documents that allowInMethods/allowExceptInMethods apply to attributes on method/function declarations as well as inside bodies. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
A disallowed attribute placed on a method or a function is technically not in the method or the function, so
$scope->getFunction()returnsnulland the name compared against theallowInMethods(and aliases) patterns would always be empty, never matching anything. The name is now resolved from the node when there's no function in the scope, the same wayallowInMethodsWithAttributesalready reads the attributes off the node, and produces the same name the reflection-based branches would produce for the same symbol.This also means
allowExceptInMethods(and aliases) can now report an attribute placed on a matching method or function, which was previously always allowed.Close #427