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