-
-
Notifications
You must be signed in to change notification settings - Fork 507
AbstractFunctionParameterSniff: don't ignore first class callables #2544
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
AbstractFunctionParameterSniff: don't ignore first class callables #2544
Conversation
... but pass them to a dedicated `process_first_class_callable()` method instead to allow sniffs to decide whether to flag these or not. Typical use-case for why first class callables should not be ignored by default: A sniff which ALWAYS flags the use of a certain function, but has different error messages depending on whether parameters are passed or not. In that situation, I believe first class callables should be treated the same as other uses of the target function. First class callables are basically syntax sugar for closures (example: https://3v4l.org/cra8s) and if the sniff would flag the use of the target function within a closure, it is only reasonable to also flag the use of the target function as a first class callable. This commit implements this. The commit does not include tests as there are no sniffs in WPCS for which the above would apply. However, I have manually tested this change via a sniff in an external standard for which this change is relevant.
Just to clarify - the previous PR did add tests with first class callables, so the logic in this PR is actually covered by tests. |
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.
@GaryJones @dingo-d Could you please have a look at this ? |
…lables First class callables are basically syntax sugar for closures. So: ```php add_action('my_action', strip_tags(...)); ``` ... could also be written as: ```php add_action('my_action', function(...$args) { return strip_tags(...$args); }); ``` Example: https://3v4l.org/cra8s#veol As the code would be flagged when used in a closure, it is my opinion, it should also be flagged when used as a first class callable. This commit implements this in a WPCS cross-version compatible manner. Prior to WPCS 3.2.0, first class callables would be send to the `process_no_parameters()` method. As of WPCS 3.2.0, they will be send to the dedicated `process_first_class_callable()` method. See WordPress/WordPress-Coding-Standards#2544
... but pass them to a dedicated
process_first_class_callable()
method instead to allow sniffs to decide whether to flag these or not.Typical use-case for why first class callables should not be ignored by default:
A sniff which ALWAYS flags the use of a certain function, but has different error messages depending on whether parameters are passed or not.
In that situation, I believe first class callables should be treated the same as other uses of the target function.
First class callables are basically syntax sugar for closures (example: https://3v4l.org/cra8s) and if the sniff would flag the use of the target function within a closure, it is only reasonable to also flag the use of the target function as a first class callable.
This commit implements this.
The commit does not include tests as there are no sniffs in WPCS for which the above would apply. However, I have manually tested this change via a sniff in an external standard for which this change is relevant.
Follow up on #2518, also see: #2518 (comment)