Skip to content

Commit cd264e6

Browse files
committed
Improve sniff performance by changing the format of some properties
This allows to use isset() instead of in_array()
1 parent 4dbe72a commit cd264e6

File tree

1 file changed

+50
-29
lines changed

1 file changed

+50
-29
lines changed

WordPress/Sniffs/WP/OptionAutoloadSniff.php

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,46 +42,63 @@ final class OptionAutoloadSniff extends AbstractFunctionParameterSniff {
4242
*
4343
* @since 3.2.0
4444
*
45-
* @var string[]
45+
* @var array<string, string>
4646
*/
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+
);
4852

4953
/**
5054
* List of valid values for the `$autoload` parameter in the wp_set_options_autoload(),
5155
* wp_set_option_autoload(), and wp_set_option_autoload_values() functions.
5256
*
5357
* @since 3.2.0
5458
*
55-
* @var string[]
59+
* @var array<string, string>
5660
*/
57-
private $valid_values_other_functions = array( 'true', 'false' );
61+
private $valid_values_other_functions = array(
62+
'true' => 'true',
63+
'false' => 'false',
64+
);
5865

5966
/**
6067
* List of deprecated values for the `$autoload` parameter.
6168
*
6269
* @since 3.2.0
6370
*
64-
* @var string[]
71+
* @var array<string, true>
6572
*/
66-
private $deprecated_values = array( 'yes', 'no' );
73+
private $deprecated_values = array(
74+
'yes' => true,
75+
'no' => true,
76+
);
6777

6878
/**
6979
* Internal-use only values for `$autoload` that cannot be fixed automatically by the sniff.
7080
*
7181
* @since 3.2.0
7282
*
73-
* @var string[]
83+
* @var array<string, true>
7484
*/
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+
);
7690

7791
/**
7892
* Internal-use only values for `$autoload` that can be fixed automatically by the sniff.
7993
*
8094
* @since 3.2.0
8195
*
82-
* @var string[]
96+
* @var array<string, true>
8397
*/
84-
private $internal_values_fixable = array( 'on', 'off' );
98+
private $internal_values_fixable = array(
99+
'on' => true,
100+
'off' => true,
101+
);
85102

86103
/**
87104
* List of replacements for fixable values.
@@ -102,9 +119,12 @@ final class OptionAutoloadSniff extends AbstractFunctionParameterSniff {
102119
*
103120
* @since 3.2.0
104121
*
105-
* @var string[]
122+
* @var array<string, true>
106123
*/
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+
);
108128

109129
/**
110130
* The group name for this group of functions.
@@ -277,7 +297,7 @@ private function maybe_display_missing_autoload_warning( $stackPtr, $function_na
277297
$this->phpcsFile->recordMetric( $stackPtr, self::METRIC_NAME, 'param missing' );
278298

279299
// 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 ] ) ) {
281301
$this->phpcsFile->addWarning(
282302
'It is recommended to always pass the `$autoload` parameter when using %s() function.',
283303
$stackPtr,
@@ -315,24 +335,25 @@ private function check_autoload_value( array $autoload_info, $function_name ) {
315335

316336
$normalized_value = strtolower( $autoload_info['clean'] );
317337

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+
}
327348
}
328349

329-
if ( in_array( $function_name, $this->autoload_is_optional, true ) ) {
350+
if ( isset( $this->autoload_is_optional[ $function_name ] ) ) {
330351
$valid_values = $this->valid_values_add_and_update;
331352
} else {
332353
$valid_values = $this->valid_values_other_functions;
333354
}
334355

335-
if ( in_array( $normalized_value, $valid_values, true ) ) {
356+
if ( isset( $valid_values[ $normalized_value ] ) ) {
336357
$this->phpcsFile->recordMetric( $param_first_token, self::METRIC_NAME, $normalized_value );
337358
return;
338359
}
@@ -360,23 +381,23 @@ private function check_autoload_value( array $autoload_info, $function_name ) {
360381

361382
$known_discouraged_values = array_merge( $this->deprecated_values, $this->internal_values_non_fixable, $this->internal_values_fixable );
362383

363-
if ( in_array( $autoload_value, $known_discouraged_values, true ) ) {
384+
if ( isset( $known_discouraged_values[ $autoload_value ] ) ) {
364385
$metric_value = $autoload_value;
365386
} else {
366387
$metric_value = 'other value';
367388
}
368389

369390
$this->phpcsFile->recordMetric( $param_first_token, self::METRIC_NAME, $metric_value );
370391

371-
if ( in_array( $autoload_value, $this->deprecated_values, true ) ) {
392+
if ( isset( $this->deprecated_values[ $autoload_value ] ) ) {
372393
$message = 'The use of `%s` as the value of the `$autoload` parameter is deprecated. Use `%s` instead.';
373394
$error_code = 'Deprecated';
374395
$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 ] ) ) {
376397
$message = 'The use of `%s` as the value of the `$autoload` parameter is discouraged. Use `%s` instead.';
377398
$error_code = 'InternalUseOnly';
378399
$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 ] ) ) {
380401
$message = 'The use of `%s` as the value of the `$autoload` parameter is discouraged.';
381402
$error_code = 'InternalUseOnly';
382403
$data = array( $autoload_info['clean'] );
@@ -394,7 +415,7 @@ function ( $value ) {
394415
$data = array( $autoload_info['clean'], $valid_values_string );
395416
}
396417

397-
if ( in_array( $autoload_value, array_keys( $this->fixable_values ), true ) ) {
418+
if ( isset( $this->fixable_values[ $autoload_value ] ) ) {
398419
$fix = $this->phpcsFile->addFixableWarning(
399420
$message,
400421
$param_first_token,

0 commit comments

Comments
 (0)