Skip to content

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

Merged

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Jul 17, 2025

... 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)

... 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.
@jrfnl
Copy link
Member Author

jrfnl commented Jul 18, 2025

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.
It is only the "calling of the new process_first_class_callable() method" line, which is run, but will never call an implemented method in a sniff. So what I meant by "not covered by tests", is that there is no test confirming the method was called via an error or warning message being thrown.

Copy link
Collaborator

@rodrigoprimo rodrigoprimo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for following up on #2518 and improving how first class callables is handled by AbstractFunctionParameterSniff, @jrfnl. It makes sense to me to give the sniff the option to handle first class callable if needed.

@jrfnl
Copy link
Member Author

jrfnl commented Jul 21, 2025

@GaryJones @dingo-d Could you please have a look at this ?

@jrfnl jrfnl merged commit 5428700 into develop Jul 22, 2025
52 checks passed
@jrfnl jrfnl deleted the feature/abstractfunctionparams-process-first-class-callables branch July 22, 2025 12:24
jrfnl added a commit to Automattic/VIP-Coding-Standards that referenced this pull request Jul 22, 2025
…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
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.

4 participants