-
-
Notifications
You must be signed in to change notification settings - Fork 523
Description
Bug Description
The WordPress.WP.CronInterval sniff incorrectly handles namespaced first-class callables (PHP 8.1+) passed as callbacks to add_filter(). It treats namespaced function references as if they were global functions with the same name, leading to both false positives and false negatives.
Minimal Code Snippet
Problem 1: false negatives
// Global function with a long interval
function first_class_weekly_schedule( $schedules ) {
$schedules['weekly'] = array(
'interval' => WEEK_IN_SECONDS,
'display' => __( 'Once Weekly' )
);
return $schedules;
}
// These should trigger ChangeDetected warning but don't trigger any warning
add_filter( 'cron_schedules', MyNamespace\first_class_weekly_schedule(...));
add_filter( 'cron_schedules', \MyNamespace\first_class_weekly_schedule(...));When running the sniff against this code snippet, I expect to see two WordPress.WP.CronInterval.ChangeDetected warnings, but instead there are no warnings.
Problem 2: false positives
// Global function with a short interval
function first_class_six_min_schedule( $schedules ) {
$schedules['every_6_mins'] = array(
'interval' => 360,
'display' => __( 'Once every 6 minutes' )
);
return $schedules;
}
// These should trigger ChangeDetected but trigger CronSchedulesInterval instead
add_filter( 'cron_schedules', MyNamespace\first_class_six_min_schedule(...));
add_filter( 'cron_schedules', \MyNamespace\first_class_six_min_schedule(...));
add_filter( 'cron_schedules', namespace\Sub\first_class_six_min_schedule(...));When running the sniff against this code snippet, I expect to see three WordPress.WP.CronInterval.ChangeDetected warnings, but instead I get three WordPress.WP.CronInterval.CronSchedulesInterval warnings.
$ vendor/bin/phpcs -s test.php --standard=WordPress --sniffs=WordPress.WP.CronInterval test.php at 15:19:02
FILE: test.php
---------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 3 WARNINGS AFFECTING 3 LINES
---------------------------------------------------------------------------------------------------------------------------------------
13 | WARNING | Scheduling crons at 360 sec ( less than 15 minutes ) is discouraged. (WordPress.WP.CronInterval.CronSchedulesInterval)
14 | WARNING | Scheduling crons at 360 sec ( less than 15 minutes ) is discouraged. (WordPress.WP.CronInterval.CronSchedulesInterval)
15 | WARNING | Scheduling crons at 360 sec ( less than 15 minutes ) is discouraged. (WordPress.WP.CronInterval.CronSchedulesInterval)
---------------------------------------------------------------------------------------------------------------------------------------Environment
| Question | Answer |
|---|---|
| PHP version | 8.4 |
| PHP_CodeSniffer version | 3.13.4 |
| WordPressCS version | develop |
| PHPCSUtils version | |
| PHPCSExtra version | |
| WordPressCS install type | git clone |
| IDE (if relevant) | N/A |
Additional Context (optional)
When handling first class callables, the sniff only considers the function name ignoring its namespace if any is present:
| $found_function = $this->find_function_by_name( $this->tokens[ $beforeOpen ]['content'] ); |
Different namespace scenarios have varying complexity to fix.. In some cases, it will be necessary to track the current namespace context making the solution more complex. In some cases, this won't be necessary.
Tested Against develop Branch?
- I have verified the issue still exists in the
developbranch of WordPressCS.