Skip to content

Commit 91b053f

Browse files
committed
CronInterval: implement new Sniff::is_in_function_call() method
This fixes some potential false positives. Includes unit tests.
1 parent 6e470de commit 91b053f

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

WordPress/Sniffs/WP/CronIntervalSniff.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ class CronIntervalSniff extends Sniff {
5555
'YEAR_IN_SECONDS' => 31536000,
5656
);
5757

58+
/**
59+
* Function within which the hook should be found.
60+
*
61+
* @var array
62+
*/
63+
protected $valid_functions = array(
64+
'add_filter' => true,
65+
);
66+
5867
/**
5968
* Returns an array of tokens this test wants to listen for.
6069
*
@@ -82,8 +91,8 @@ public function process_token( $stackPtr ) {
8291
}
8392

8493
// If within add_filter.
85-
$functionPtr = $this->phpcsFile->findPrevious( \T_STRING, key( $token['nested_parenthesis'] ) );
86-
if ( false === $functionPtr || 'add_filter' !== $this->tokens[ $functionPtr ]['content'] ) {
94+
$functionPtr = $this->is_in_function_call( $stackPtr, $this->valid_functions );
95+
if ( false === $functionPtr ) {
8796
return;
8897
}
8998

@@ -92,6 +101,11 @@ public function process_token( $stackPtr ) {
92101
return;
93102
}
94103

104+
if ( $stackPtr >= $callback['start'] ) {
105+
// "cron_schedules" found in the second parameter, not the first.
106+
return;
107+
}
108+
95109
// Detect callback function name.
96110
$callbackArrayPtr = $this->phpcsFile->findNext( Tokens::$emptyTokens, $callback['start'], ( $callback['end'] + 1 ), true );
97111

WordPress/Tests/WP/CronIntervalUnitTest.inc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ add_filter( 'cron_schedules' ); // Ignore, no callback parameter.
5252

5353
add_filter( 'cron_schedules', [ 'Foo', 'my_add_quicklier' ] ); // Error: 5 min.
5454

55-
// Ignore, not our function. Currently gives false positive, will be fixed later when function call detection utility functions are added.
55+
// Ignore, not our function.
5656
My_Custom::add_filter( 'cron_schedules', [ 'Foo', 'my_add_quicklier' ] );
5757

5858
// Deal correctly with the WP time constants.
@@ -139,3 +139,8 @@ add_filter( 'cron_schedules', function ( $schedules ) {
139139
);
140140
return $schedules;
141141
} ); // Error: 9 min.
142+
143+
Custom::add_filter( 'cron_schedules', array( $class, $method ) ); // OK, not the WP function.
144+
add_filter( 'some_hook', array( $place, 'cron_schedules' ) ); // OK, not the hook we're looking for.
145+
add_filter( function() { return get_hook_name('cron_schedules'); }(), array( $class, $method ) ); // OK, nested in another function call.
146+

WordPress/Tests/WP/CronIntervalUnitTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public function getWarningList() {
4545
41 => 1,
4646
43 => 1,
4747
53 => 1,
48-
56 => 1, // False positive.
4948
67 => 1,
5049
76 => 1,
5150
85 => 1,

0 commit comments

Comments
 (0)