WordPress / WordPress/WordPress-Coding-Standards
WP/CronInterval: incorrect handling of namespaced first-class callables
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 2.8k
- Forks
- 521
- Avg merge
- 5d 20h
- Merged PRs (30d)
- 1
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:
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in WordPress/Sniffs/WP/CronIntervalSniff.php around line 157, where first-class callable names are handled, and reproduce the issue with the supplied vendor/bin/phpcs command and snippets. Trace how namespaced callable references are resolved, then verify that the examples produce ChangeDetected warnings rather than incorrect or missing diagnostics.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100