WordPress / WordPress/WordPress-Coding-Standards

WP/CronInterval: incorrect handling of namespaced first-class callables

Open
#2,644 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Component: Extra Focus: Modern PHP Type: Bug
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:

https://github.com/WordPress/WordPress-Coding-Standards/blob/20b2c5a58609312f4b815a8d0d9610ecfa308467/WordPress/Sniffs/WP/CronIntervalSniff.php#L157

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 develop branch of WordPressCS.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.