@@ -37,54 +37,70 @@ final class OptionAutoloadSniff extends AbstractFunctionParameterSniff {
37
37
const METRIC_NAME = 'Value of the `$autoload` parameter in the option functions ' ;
38
38
39
39
/**
40
- * List of valid values for the `$autoload` parameter in the add_option() and update_option()
41
- * functions.
40
+ * Valid values for the `$autoload` parameter in the add_option() and update_option() functions.
42
41
*
43
42
* @since 3.2.0
44
43
*
45
- * @var string[]
44
+ * @var array< string, string>
46
45
*/
47
- private $ valid_values_add_and_update = array ( 'true ' , 'false ' , 'null ' );
46
+ private $ valid_values_add_and_update = array (
47
+ 'true ' => 'true ' ,
48
+ 'false ' => 'false ' ,
49
+ 'null ' => 'null ' ,
50
+ );
48
51
49
52
/**
50
- * List of valid values for the `$autoload` parameter in the wp_set_options_autoload(),
53
+ * Valid values for the `$autoload` parameter in the wp_set_options_autoload(),
51
54
* wp_set_option_autoload(), and wp_set_option_autoload_values() functions.
52
55
*
53
56
* @since 3.2.0
54
57
*
55
- * @var string[]
58
+ * @var array< string, string>
56
59
*/
57
- private $ valid_values_other_functions = array ( 'true ' , 'false ' );
60
+ private $ valid_values_other_functions = array (
61
+ 'true ' => 'true ' ,
62
+ 'false ' => 'false ' ,
63
+ );
58
64
59
65
/**
60
- * List of deprecated values for the `$autoload` parameter.
66
+ * Deprecated values for the `$autoload` parameter.
61
67
*
62
68
* @since 3.2.0
63
69
*
64
- * @var string[]
70
+ * @var array< string, true>
65
71
*/
66
- private $ deprecated_values = array ( 'yes ' , 'no ' );
72
+ private $ deprecated_values = array (
73
+ 'yes ' => true ,
74
+ 'no ' => true ,
75
+ );
67
76
68
77
/**
69
78
* Internal-use only values for `$autoload` that cannot be fixed automatically by the sniff.
70
79
*
71
80
* @since 3.2.0
72
81
*
73
- * @var string[]
82
+ * @var array< string, true>
74
83
*/
75
- private $ internal_values_non_fixable = array ( 'auto ' , 'auto-on ' , 'auto-off ' );
84
+ private $ internal_values_non_fixable = array (
85
+ 'auto ' => true ,
86
+ 'auto-on ' => true ,
87
+ 'auto-off ' => true ,
88
+ );
76
89
77
90
/**
78
91
* Internal-use only values for `$autoload` that can be fixed automatically by the sniff.
79
92
*
80
93
* @since 3.2.0
81
94
*
82
- * @var string[]
95
+ * @var array< string, true>
83
96
*/
84
- private $ internal_values_fixable = array ( 'on ' , 'off ' );
97
+ private $ internal_values_fixable = array (
98
+ 'on ' => true ,
99
+ 'off ' => true ,
100
+ );
85
101
86
102
/**
87
- * List of replacements for fixable values.
103
+ * Replacements for fixable values.
88
104
*
89
105
* @since 3.2.0
90
106
*
@@ -98,13 +114,16 @@ final class OptionAutoloadSniff extends AbstractFunctionParameterSniff {
98
114
);
99
115
100
116
/**
101
- * List of functions for which the `$autoload` parameter is optional.
117
+ * Functions for which the `$autoload` parameter is optional.
102
118
*
103
119
* @since 3.2.0
104
120
*
105
- * @var string[]
121
+ * @var array< string, true>
106
122
*/
107
- private $ autoload_is_optional = array ( 'add_option ' , 'update_option ' );
123
+ private $ autoload_is_optional = array (
124
+ 'add_option ' => true ,
125
+ 'update_option ' => true ,
126
+ );
108
127
109
128
/**
110
129
* The group name for this group of functions.
@@ -277,7 +296,7 @@ private function maybe_display_missing_autoload_warning( $stackPtr, $function_na
277
296
$ this ->phpcsFile ->recordMetric ( $ stackPtr , self ::METRIC_NAME , 'param missing ' );
278
297
279
298
// Only display a warning for the functions in which the `$autoload` parameter is optional.
280
- if ( in_array ( $ function_name , $ this ->autoload_is_optional , true ) ) {
299
+ if ( isset ( $ this ->autoload_is_optional [ $ function_name ] ) ) {
281
300
$ this ->phpcsFile ->addWarning (
282
301
'It is recommended to always pass the `$autoload` parameter when using %s() function. ' ,
283
302
$ stackPtr ,
@@ -315,24 +334,25 @@ private function check_autoload_value( array $autoload_info, $function_name ) {
315
334
316
335
$ normalized_value = strtolower ( $ autoload_info ['clean ' ] );
317
336
318
- if ( \T_NS_SEPARATOR === $ this ->tokens [ $ param_first_token ]['code ' ]
319
- && $ param_second_token
320
- && in_array ( strtolower ( $ this ->tokens [ $ param_second_token ]['content ' ] ), $ this ->valid_values_add_and_update , true )
321
- ) {
322
- // Ensure the sniff handles correctly `true`, `false` and `null` when they are
323
- // namespaced (preceded by a backslash).
324
- $ param_first_token = $ param_second_token ;
325
- $ param_second_token = false ;
326
- $ normalized_value = substr ( $ normalized_value , 1 );
337
+ if ( \T_NS_SEPARATOR === $ this ->tokens [ $ param_first_token ]['code ' ] && $ param_second_token ) {
338
+ $ token_content_lowercase = strtolower ( $ this ->tokens [ $ param_second_token ]['content ' ] );
339
+
340
+ if ( isset ( $ this ->valid_values_add_and_update [ $ token_content_lowercase ] ) ) {
341
+ // Ensure the sniff handles correctly `true`, `false` and `null` when they are
342
+ // namespaced (preceded by a backslash).
343
+ $ param_first_token = $ param_second_token ;
344
+ $ param_second_token = false ;
345
+ $ normalized_value = substr ( $ normalized_value , 1 );
346
+ }
327
347
}
328
348
329
- if ( in_array ( $ function_name , $ this ->autoload_is_optional , true ) ) {
349
+ if ( isset ( $ this ->autoload_is_optional [ $ function_name ] ) ) {
330
350
$ valid_values = $ this ->valid_values_add_and_update ;
331
351
} else {
332
352
$ valid_values = $ this ->valid_values_other_functions ;
333
353
}
334
354
335
- if ( in_array ( $ normalized_value , $ valid_values , true ) ) {
355
+ if ( isset ( $ valid_values [ $ normalized_value ] ) ) {
336
356
$ this ->phpcsFile ->recordMetric ( $ param_first_token , self ::METRIC_NAME , $ normalized_value );
337
357
return ;
338
358
}
@@ -360,23 +380,23 @@ private function check_autoload_value( array $autoload_info, $function_name ) {
360
380
361
381
$ known_discouraged_values = array_merge ( $ this ->deprecated_values , $ this ->internal_values_non_fixable , $ this ->internal_values_fixable );
362
382
363
- if ( in_array ( $ autoload_value , $ known_discouraged_values , true ) ) {
383
+ if ( isset ( $ known_discouraged_values [ $ autoload_value ] ) ) {
364
384
$ metric_value = $ autoload_value ;
365
385
} else {
366
386
$ metric_value = 'other value ' ;
367
387
}
368
388
369
389
$ this ->phpcsFile ->recordMetric ( $ param_first_token , self ::METRIC_NAME , $ metric_value );
370
390
371
- if ( in_array ( $ autoload_value , $ this ->deprecated_values , true ) ) {
391
+ if ( isset ( $ this ->deprecated_values [ $ autoload_value ] ) ) {
372
392
$ message = 'The use of `%s` as the value of the `$autoload` parameter is deprecated. Use `%s` instead. ' ;
373
393
$ error_code = 'Deprecated ' ;
374
394
$ data = array ( $ autoload_info ['clean ' ], $ this ->fixable_values [ $ autoload_value ] );
375
- } elseif ( in_array ( $ autoload_value , $ this ->internal_values_fixable , true ) ) {
395
+ } elseif ( isset ( $ this ->internal_values_fixable [ $ autoload_value ] ) ) {
376
396
$ message = 'The use of `%s` as the value of the `$autoload` parameter is discouraged. Use `%s` instead. ' ;
377
397
$ error_code = 'InternalUseOnly ' ;
378
398
$ data = array ( $ autoload_info ['clean ' ], $ this ->fixable_values [ $ autoload_value ] );
379
- } elseif ( in_array ( $ autoload_value , $ this ->internal_values_non_fixable , true ) ) {
399
+ } elseif ( isset ( $ this ->internal_values_non_fixable [ $ autoload_value ] ) ) {
380
400
$ message = 'The use of `%s` as the value of the `$autoload` parameter is discouraged. ' ;
381
401
$ error_code = 'InternalUseOnly ' ;
382
402
$ data = array ( $ autoload_info ['clean ' ] );
@@ -394,7 +414,7 @@ function ( $value ) {
394
414
$ data = array ( $ autoload_info ['clean ' ], $ valid_values_string );
395
415
}
396
416
397
- if ( in_array ( $ autoload_value , array_keys ( $ this ->fixable_values ), true ) ) {
417
+ if ( isset ( $ this ->fixable_values [ $ autoload_value ] ) ) {
398
418
$ fix = $ this ->phpcsFile ->addFixableWarning (
399
419
$ message ,
400
420
$ param_first_token ,
0 commit comments