@@ -30,6 +30,8 @@ final class OptionAutoloadSniff extends AbstractFunctionParameterSniff {
30
30
/**
31
31
* The phrase to use for the metric recorded by this sniff.
32
32
*
33
+ * @since 3.2.0
34
+ *
33
35
* @var string
34
36
*/
35
37
const METRIC_NAME = 'Value of the `$autoload` parameter in the option functions ' ;
@@ -42,7 +44,7 @@ final class OptionAutoloadSniff extends AbstractFunctionParameterSniff {
42
44
*
43
45
* @var string[]
44
46
*/
45
- protected static $ valid_values_add_and_update = array ( 'true ' , 'false ' , 'null ' );
47
+ private $ valid_values_add_and_update = array ( 'true ' , 'false ' , 'null ' );
46
48
47
49
/**
48
50
* List of valid values for the `$autoload` parameter in the wp_set_options_autoload(),
@@ -52,7 +54,7 @@ final class OptionAutoloadSniff extends AbstractFunctionParameterSniff {
52
54
*
53
55
* @var string[]
54
56
*/
55
- protected static $ valid_values_other_functions = array ( 'true ' , 'false ' );
57
+ private $ valid_values_other_functions = array ( 'true ' , 'false ' );
56
58
57
59
/**
58
60
* List of deprecated values for the `$autoload` parameter.
@@ -61,7 +63,7 @@ final class OptionAutoloadSniff extends AbstractFunctionParameterSniff {
61
63
*
62
64
* @var string[]
63
65
*/
64
- protected static $ deprecated_values = array ( 'yes ' , 'no ' );
66
+ private $ deprecated_values = array ( 'yes ' , 'no ' );
65
67
66
68
/**
67
69
* Internal-use only values for `$autoload` that cannot be fixed automatically by the sniff.
@@ -70,7 +72,7 @@ final class OptionAutoloadSniff extends AbstractFunctionParameterSniff {
70
72
*
71
73
* @var string[]
72
74
*/
73
- protected static $ internal_values_non_fixable = array ( 'auto ' , 'auto-on ' , 'auto-off ' );
75
+ private $ internal_values_non_fixable = array ( 'auto ' , 'auto-on ' , 'auto-off ' );
74
76
75
77
/**
76
78
* Internal-use only values for `$autoload` that can be fixed automatically by the sniff.
@@ -79,16 +81,16 @@ final class OptionAutoloadSniff extends AbstractFunctionParameterSniff {
79
81
*
80
82
* @var string[]
81
83
*/
82
- protected static $ internal_values_fixable = array ( 'on ' , 'off ' );
84
+ private $ internal_values_fixable = array ( 'on ' , 'off ' );
83
85
84
86
/**
85
87
* List of replacements for fixable values.
86
88
*
87
89
* @since 3.2.0
88
90
*
89
- * @var string[]
91
+ * @var array< string, string>
90
92
*/
91
- protected static $ fixable_values = array (
93
+ private $ fixable_values = array (
92
94
'yes ' => 'true ' ,
93
95
'no ' => 'false ' ,
94
96
'on ' => 'true ' ,
@@ -102,7 +104,7 @@ final class OptionAutoloadSniff extends AbstractFunctionParameterSniff {
102
104
*
103
105
* @var string[]
104
106
*/
105
- protected static $ autoload_is_optional = array ( 'add_option ' , 'update_option ' );
107
+ private $ autoload_is_optional = array ( 'add_option ' , 'update_option ' );
106
108
107
109
/**
108
110
* The group name for this group of functions.
@@ -215,7 +217,7 @@ public function process_no_parameters( $stackPtr, $group_name, $function_name )
215
217
*
216
218
* @return void
217
219
*/
218
- protected function handle_wp_set_option_autoload_values ( array $ options_param , $ stackPtr ) {
220
+ private function handle_wp_set_option_autoload_values ( array $ options_param , $ stackPtr ) {
219
221
$ array_token = $ this ->phpcsFile ->findNext (
220
222
Tokens::$ emptyTokens ,
221
223
$ options_param ['start ' ],
@@ -266,15 +268,16 @@ protected function handle_wp_set_option_autoload_values( array $options_param, $
266
268
* @since 3.2.0
267
269
*
268
270
* @param int $stackPtr The position of the current token in the stack.
269
- * @param string $function_name The name of the function being checked.
271
+ * @param string $function_name The token content (function name) which was matched
272
+ * in lowercase.
270
273
*
271
274
* @return void
272
275
*/
273
- protected function maybe_display_missing_autoload_warning ( $ stackPtr , $ function_name ) {
276
+ private function maybe_display_missing_autoload_warning ( $ stackPtr , $ function_name ) {
274
277
$ this ->phpcsFile ->recordMetric ( $ stackPtr , self ::METRIC_NAME , 'param missing ' );
275
278
276
279
// Only display a warning for the functions in which the `$autoload` parameter is optional.
277
- if ( in_array ( $ function_name , self :: $ autoload_is_optional , true ) ) {
280
+ if ( in_array ( $ function_name , $ this -> autoload_is_optional , true ) ) {
278
281
$ this ->phpcsFile ->addWarning (
279
282
'It is recommended to always pass the `$autoload` parameter when using %s() function. ' ,
280
283
$ stackPtr ,
@@ -290,13 +293,13 @@ protected function maybe_display_missing_autoload_warning( $stackPtr, $function_
290
293
* @since 3.2.0
291
294
*
292
295
* @param array $autoload_info Information about the autoload value (start and end tokens and
293
- * the clean and raw value).
294
- * @param string $function_name The name of the function being checked.
296
+ * the clean value).
297
+ * @param string $function_name The token content (function name) which was matched
298
+ * in lowercase.
295
299
*
296
300
* @return void
297
301
*/
298
- protected function check_autoload_value ( array $ autoload_info , $ function_name ) {
299
- // Find the first and second param non-empty tokens (the second token might not exist).
302
+ private function check_autoload_value ( array $ autoload_info , $ function_name ) {
300
303
$ param_first_token = $ this ->phpcsFile ->findNext (
301
304
Tokens::$ emptyTokens ,
302
305
$ autoload_info ['start ' ],
@@ -312,9 +315,9 @@ protected function check_autoload_value( array $autoload_info, $function_name )
312
315
313
316
$ normalized_value = strtolower ( $ autoload_info ['clean ' ] );
314
317
315
- if ( T_NS_SEPARATOR === $ this ->tokens [ $ param_first_token ]['code ' ]
318
+ if ( \ T_NS_SEPARATOR === $ this ->tokens [ $ param_first_token ]['code ' ]
316
319
&& $ param_second_token
317
- && in_array ( strtolower ( $ this ->tokens [ $ param_second_token ]['content ' ] ), self :: $ valid_values_add_and_update , true )
320
+ && in_array ( strtolower ( $ this ->tokens [ $ param_second_token ]['content ' ] ), $ this -> valid_values_add_and_update , true )
318
321
) {
319
322
// Ensure the sniff handles correctly `true`, `false` and `null` when they are
320
323
// namespaced (preceded by a backslash).
@@ -323,18 +326,18 @@ protected function check_autoload_value( array $autoload_info, $function_name )
323
326
$ normalized_value = substr ( $ normalized_value , 1 );
324
327
}
325
328
326
- if ( in_array ( $ function_name , self :: $ autoload_is_optional , true ) ) {
327
- $ valid_values = self :: $ valid_values_add_and_update ;
329
+ if ( in_array ( $ function_name , $ this -> autoload_is_optional , true ) ) {
330
+ $ valid_values = $ this -> valid_values_add_and_update ;
328
331
} else {
329
- $ valid_values = self :: $ valid_values_other_functions ;
332
+ $ valid_values = $ this -> valid_values_other_functions ;
330
333
}
331
334
332
335
if ( in_array ( $ normalized_value , $ valid_values , true ) ) {
333
336
$ this ->phpcsFile ->recordMetric ( $ param_first_token , self ::METRIC_NAME , $ normalized_value );
334
337
return ;
335
338
}
336
339
337
- if ( in_array ( $ this ->tokens [ $ param_first_token ]['code ' ], array ( T_VARIABLE , T_STRING ), true )
340
+ if ( in_array ( $ this ->tokens [ $ param_first_token ]['code ' ], array ( \ T_VARIABLE , \ T_STRING ), true )
338
341
&& 'null ' !== strtolower ( $ this ->tokens [ $ param_first_token ]['content ' ] )
339
342
) {
340
343
// Bail early if the first non-empty token in the parameter is T_VARIABLE or T_STRING as
@@ -344,7 +347,7 @@ protected function check_autoload_value( array $autoload_info, $function_name )
344
347
}
345
348
346
349
if ( $ param_second_token
347
- && ! in_array ( $ this ->tokens [ $ param_first_token ]['code ' ], array ( T_ARRAY , T_OPEN_SHORT_ARRAY ), true )
350
+ && ! isset ( Collections:: arrayOpenTokensBC ()[ $ this ->tokens [ $ param_first_token ]['code ' ] ] )
348
351
) {
349
352
// Bail early if the parameter has two or more non-empty tokens and the second token is
350
353
// not an array opener as this means an undetermined param value or a value that is not
@@ -355,7 +358,7 @@ protected function check_autoload_value( array $autoload_info, $function_name )
355
358
356
359
$ autoload_value = TextStrings::stripQuotes ( $ autoload_info ['clean ' ] );
357
360
358
- $ known_discouraged_values = array_merge ( self :: $ deprecated_values , self :: $ internal_values_non_fixable , self :: $ internal_values_fixable );
361
+ $ known_discouraged_values = array_merge ( $ this -> deprecated_values , $ this -> internal_values_non_fixable , $ this -> internal_values_fixable );
359
362
360
363
if ( in_array ( $ autoload_value , $ known_discouraged_values , true ) ) {
361
364
$ metric_value = $ autoload_value ;
@@ -365,15 +368,15 @@ protected function check_autoload_value( array $autoload_info, $function_name )
365
368
366
369
$ this ->phpcsFile ->recordMetric ( $ param_first_token , self ::METRIC_NAME , $ metric_value );
367
370
368
- if ( in_array ( $ autoload_value , self :: $ deprecated_values , true ) ) {
371
+ if ( in_array ( $ autoload_value , $ this -> deprecated_values , true ) ) {
369
372
$ message = 'The use of `%s` as the value of the `$autoload` parameter is deprecated. Use `%s` instead. ' ;
370
373
$ error_code = 'Deprecated ' ;
371
- $ data = array ( $ autoload_info ['clean ' ], self :: $ fixable_values [ $ autoload_value ] );
372
- } elseif ( in_array ( $ autoload_value , self :: $ internal_values_fixable , true ) ) {
374
+ $ data = array ( $ autoload_info ['clean ' ], $ this -> fixable_values [ $ autoload_value ] );
375
+ } elseif ( in_array ( $ autoload_value , $ this -> internal_values_fixable , true ) ) {
373
376
$ message = 'The use of `%s` as the value of the `$autoload` parameter is discouraged. Use `%s` instead. ' ;
374
377
$ error_code = 'InternalUseOnly ' ;
375
- $ data = array ( $ autoload_info ['clean ' ], self :: $ fixable_values [ $ autoload_value ] );
376
- } elseif ( in_array ( $ autoload_value , self :: $ internal_values_non_fixable , true ) ) {
378
+ $ data = array ( $ autoload_info ['clean ' ], $ this -> fixable_values [ $ autoload_value ] );
379
+ } elseif ( in_array ( $ autoload_value , $ this -> internal_values_non_fixable , true ) ) {
377
380
$ message = 'The use of `%s` as the value of the `$autoload` parameter is discouraged. ' ;
378
381
$ error_code = 'InternalUseOnly ' ;
379
382
$ data = array ( $ autoload_info ['clean ' ] );
@@ -391,7 +394,7 @@ function ( $value ) {
391
394
$ data = array ( $ autoload_info ['clean ' ], $ valid_values_string );
392
395
}
393
396
394
- if ( in_array ( $ autoload_value , array_keys ( self :: $ fixable_values ), true ) ) {
397
+ if ( in_array ( $ autoload_value , array_keys ( $ this -> fixable_values ), true ) ) {
395
398
$ fix = $ this ->phpcsFile ->addFixableWarning (
396
399
$ message ,
397
400
$ param_first_token ,
@@ -400,7 +403,7 @@ function ( $value ) {
400
403
);
401
404
402
405
if ( $ fix ) {
403
- $ this ->phpcsFile ->fixer ->replaceToken ( $ param_first_token , self :: $ fixable_values [ $ autoload_value ] );
406
+ $ this ->phpcsFile ->fixer ->replaceToken ( $ param_first_token , $ this -> fixable_values [ $ autoload_value ] );
404
407
}
405
408
406
409
return ;
0 commit comments