@@ -10,12 +10,12 @@ wp_cache_set( $testing, $data, 'test_group', 5*MINUTE_IN_SECONDS );
1010wp_cache_set ( 123 , $ data , 'test_group ' , 5 * MINUTE_IN_SECONDS );
1111wp_cache_set ( 1234 , $ data , '' , 425 );
1212wp_cache_set ( $ testing , $ data , null , 350 );
13- wp_cache_set ( $ testing , $ data );
13+ \ wp_cache_set ( $ testing , $ data );
1414wp_cache_set ( 'test ' , $ data , $ group );
1515
1616wp_cache_add ( 'test ' , $ data , $ group , 300 );
1717wp_cache_add ( $ testing , $ data , 'test_group ' , 6 *MINUTE_IN_SECONDS );
18- wp_cache_add ( 1234 , $ data , '' , 425 );
18+ WP_CACHE_ADD ( 1234 , $ data , '' , 425 );
1919wp_cache_add ( $ testing , $ data , null , 350 );
2020
2121wp_cache_replace ( 'test ' , $ data , $ group , 300 );
@@ -30,13 +30,13 @@ wp_cache_set( 123, $data, null, 1.5 * MINUTE_IN_SECONDS ); // Lower than 300.
3030wp_cache_set ( $ testing , $ data , '' , 1.5 * MINUTE_IN_SECONDS ); // Lower than 300.
3131
3232wp_cache_add ( 'test ' , $ data , $ group , 100 ); // Lower than 300.
33- wp_cache_add ( 'test ' , $ data , $ group , 2 *MINUTE_IN_SECONDS ); // Lower than 300.
33+ \ wp_cache_add ( 'test ' , $ data , $ group , 2 *MINUTE_IN_SECONDS ); // Lower than 300.
3434wp_cache_add ( 123 , $ data , null , 1.5 * MINUTE_IN_SECONDS ); // Lower than 300.
3535wp_cache_add ( $ testing , $ data , '' , 1.5 * MINUTE_IN_SECONDS ); // Lower than 300.
3636
3737wp_cache_replace ( 'test ' , $ data , $ group , 100 ); // Lower than 300.
3838wp_cache_replace ( 'test ' , $ data , $ group , 2 *MINUTE_IN_SECONDS ); // Lower than 300.
39- wp_cache_replace ( 123 , $ data , null , 1.5 * MINUTE_IN_SECONDS ); // Lower than 300.
39+ WP_CACHE_REPLACE ( 123 , $ data , null , 1.5 * MINUTE_IN_SECONDS ); // Lower than 300.
4040wp_cache_replace ( $ testing , $ data , '' , 1.5 * MINUTE_IN_SECONDS ); // Lower than 300.
4141
4242// Test error being reported on the line containing the parameter.
@@ -49,7 +49,7 @@ wp_cache_replace(
4949
5050// Test calculations with floats.
5151wp_cache_replace ( $ testing , $ data , '' , 7.5 * MINUTE_IN_SECONDS ); // OK.
52- wp_cache_replace ( $ testing , $ data , '' , 500 * 0.1 ); // Bad.
52+ wp_cache_replace ( $ testing , $ data , '' , 500 * 0.1 , ); // Bad.
5353
5454// Test comment handling.
5555wp_cache_add ( 'test ' , $ data , $ group , /* Deliberately left empty */ ); // OK.
@@ -71,11 +71,11 @@ wp_cache_add(
7171); // OK.
7272
7373// Test variable/constant with or without calculation being passed.
74- wp_cache_set ( $ key , $ data , '' , $ time ); // Manual inspection warning.
74+ WP_Cache_Set ( $ key , $ data , '' , $ time ); // Manual inspection warning.
7575wp_cache_set ( $ key , $ data , '' , PREFIX_FIVE_MINUTES ); // Manual inspection warning.
76- wp_cache_set ( $ key , $ data , '' , 20 * $ time ); // Manual inspection warning.
77- wp_cache_set ( $ key , $ data , '' , $ base + $ extra ); // Manual inspection warning.
78- wp_cache_set ( $ key , $ data , '' , 300 + $ extra ); // Manual inspection warning.
76+ wp_cache_set ( $ key , $ data , '' , 20 * $ time /*comment*/ ); // Manual inspection warning.
77+ wp_cache_set ( $ key , $ data , '' , $ base + $ extra, ); // Manual inspection warning.
78+ \ wp_cache_set ( $ key , $ data , '' , 300 + $ extra ); // Manual inspection warning.
7979wp_cache_set ( $ key , $ data , '' , PREFIX_CUSTOM_TIME * 5 ); // Manual inspection warning.
8080
8181// Test calculations with additional aritmetic operators.
@@ -84,9 +84,9 @@ wp_cache_add( 'test', $data, $group, WEEK_IN_SECONDS / 3 + HOUR_IN_SECONDS ); /
8484
8585// Test calculations grouped with parentheses.
8686wp_cache_set ( $ key , $ data , '' , (24 * 60 * 60 ) ); // OK.
87- wp_cache_set ( $ key , $ data , '' , (-(2 * 60 ) + 600 ) ); // OK.
87+ wp_cache_set ( $ key , $ data , '' , (-(2 * 60 ) + 600 ), ); // OK.
8888wp_cache_set ( $ key , $ data , '' , (2 * 60 ) ); // Bad.
89- wp_cache_set ( $ key , $ data , '' , (-( 2 * 60 ) + 600 ); // OK - includes parse error, close parenthesis missing.
89+
9090
9191// Test handling of numbers passed as strings.
9292wp_cache_set ( 'test ' , $ data , $ group , '300 ' ); // OK - type cast to integer within the function.
@@ -110,14 +110,52 @@ wp_cache_set( 'test', $data, $group, \MONTH_IN_SECONDS ); // OK.
110110
111111// Test passing something which may look like one of the time constants, but isn't.
112112wp_cache_set ( 'test ' , $ data , $ group , month_in_seconds ); // Bad - constants are case-sensitive.
113- wp_cache_set ( 'test ' , $ data , $ group , HOUR_IN_SECONDS ::methodName () ); // Bad - not a constant.
113+ wp_cache_set ( 'test ' , $ data , $ group , /*comment*/ HOUR_IN_SECONDS ::methodName () ); // Bad - not a constant.
114114wp_cache_set ( 'test ' , $ data , $ group , $ obj ->MONTH_IN_SECONDS ); // Bad - not a constant.
115115wp_cache_set ( 'test ' , $ data , $ group , $ obj ::MONTH_IN_SECONDS ); // Bad - not the WP constant.
116- wp_cache_set ( 'test ' , $ data , $ group , PluginNamespace \SubLevel \DAY_IN_SECONDS ); // Bad - not the WP constant.
116+ WP_Cache_Set ( 'test ' , $ data , $ group , PluginNamespace \SubLevel \DAY_IN_SECONDS , ); // Bad - not the WP constant.
117117
118118// Test passing negative number as cache time.
119119wp_cache_set ( 'test ' , $ data , $ group , -300 ); // Bad.
120120wp_cache_add ( $ testing , $ data , 'test_group ' , -6 * MINUTE_IN_SECONDS ); // Bad.
121121
122122// Test more complex logic in the parameter.
123123wp_cache_add ( $ key , $ data , '' , ($ toggle ? 200 : 400 ) ); // Manual inspection warning.
124+
125+ // Test handling of non-numeric data in text string.
126+ wp_cache_set ( 'test ' , $ data , $ group , '' ); // Manual inspection warning.
127+ wp_cache_set ( 'test ' , $ data , $ group , '300 Mulberry Street ' ); // Manual inspection warning.
128+
129+ // Test handling of edge case/parse error.
130+ wp_cache_set ( 'test ' , $ data , $ group ,\); // OK, ignore.
131+
132+ // Test handling of some modern PHP syntaxes.
133+ wp_cache_add ('test ' , $ data , $ group , ...$ params ); // PHP 5.6 argument unpacking. Manual inspection warning.
134+ wp_cache_add ( $ key , $ data , '' , $ toggle ?? 400 ) ); // PHP 7.0 null coalesce. Manual inspection warning.
135+ add_action ('my_action ' , wp_cache_set (...)); // PHP 8.1 first class callable. OK, ignore.
136+
137+ // Looks like a function call, but is a PHP 8.0+ class instantiation via an attribute.
138+ #[WP_Cache_Replace('text ' , 'data ' , 'group ' , 50 )]
139+ function foo () {}
140+
141+ // Alternative numeric base.
142+ wp_cache_set ( $ key , $ data , '' , 0620 ); // Octal number. OK (=400).
143+ wp_cache_set ( $ key , $ data , '' , 0x190 ); // Hexidecimal number. OK (=400).
144+ wp_cache_set ( $ key , $ data , '' , 0b110010000 ); // PHP 5.4 binary number. OK (=400).
145+ wp_cache_set ( $ key , $ data , '' , 1_000 ); // PHP 7.4 numeric literal with underscore. OK.
146+ wp_cache_set ( $ key , $ data , '' , 0o620 ); // PHP 8.1 octal literal. OK (=400).
147+
148+ wp_cache_set ( $ key , $ data , '' , 0226 ); // Octal number. Bad (=150).
149+ wp_cache_set ( $ key , $ data , '' , 0x96 ); // Hexidecimal number. Bad (=150).
150+ wp_cache_set ( $ key , $ data , '' , 0b10010110 ); // PHP 5.4 binary number. Bad (=150).
151+ wp_cache_set ( $ key , $ data , '' , 1_50 ); // PHP 7.4 numeric literal with underscore. Bad.
152+ wp_cache_set ( $ key , $ data , '' , 0o226 ); // PHP 8.1 octal literal. Bad (=150).
153+
154+ // Safeguard handling of function calls using PHP 8.0+ named parameters.
155+ wp_cache_add (data: $ data , group: $ group ); // OK, well, not really, missing required $key param, but that's not the concern of this sniff.
156+ wp_cache_replace (data: $ data , expire: 400 , group: $ group ); // OK.
157+ wp_cache_add ($ key , group: $ group , data: $ data , expires: 100 ,); // OK, well, not really, typo in param name, but that's not the concern of the sniff.
158+ wp_cache_replace ($ key , expire: 400 , group: $ group , data: 100 ,); // OK.
159+
160+ wp_cache_replace ($ key , $ data , expire: 100 ); // Bad.
161+ wp_cache_replace (expire: 100 , data: $ data , key: $ group ); // Bad.
0 commit comments