Skip to content

Commit fc98b8e

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 fc98b8e

File tree

1 file changed

+55
-35
lines changed

1 file changed

+55
-35
lines changed

WordPress/Sniffs/WP/OptionAutoloadSniff.php

+55-35
Original file line numberDiff line numberDiff line change
@@ -37,54 +37,70 @@ final class OptionAutoloadSniff extends AbstractFunctionParameterSniff {
3737
const METRIC_NAME = 'Value of the `$autoload` parameter in the option functions';
3838

3939
/**
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.
4241
*
4342
* @since 3.2.0
4443
*
45-
* @var string[]
44+
* @var array<string, string>
4645
*/
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+
);
4851

4952
/**
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(),
5154
* wp_set_option_autoload(), and wp_set_option_autoload_values() functions.
5255
*
5356
* @since 3.2.0
5457
*
55-
* @var string[]
58+
* @var array<string, string>
5659
*/
57-
private $valid_values_other_functions = array( 'true', 'false' );
60+
private $valid_values_other_functions = array(
61+
'true' => 'true',
62+
'false' => 'false',
63+
);
5864

5965
/**
60-
* List of deprecated values for the `$autoload` parameter.
66+
* Deprecated values for the `$autoload` parameter.
6167
*
6268
* @since 3.2.0
6369
*
64-
* @var string[]
70+
* @var array<string, true>
6571
*/
66-
private $deprecated_values = array( 'yes', 'no' );
72+
private $deprecated_values = array(
73+
'yes' => true,
74+
'no' => true,
75+
);
6776

6877
/**
6978
* Internal-use only values for `$autoload` that cannot be fixed automatically by the sniff.
7079
*
7180
* @since 3.2.0
7281
*
73-
* @var string[]
82+
* @var array<string, true>
7483
*/
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+
);
7689

7790
/**
7891
* Internal-use only values for `$autoload` that can be fixed automatically by the sniff.
7992
*
8093
* @since 3.2.0
8194
*
82-
* @var string[]
95+
* @var array<string, true>
8396
*/
84-
private $internal_values_fixable = array( 'on', 'off' );
97+
private $internal_values_fixable = array(
98+
'on' => true,
99+
'off' => true,
100+
);
85101

86102
/**
87-
* List of replacements for fixable values.
103+
* Replacements for fixable values.
88104
*
89105
* @since 3.2.0
90106
*
@@ -98,13 +114,16 @@ final class OptionAutoloadSniff extends AbstractFunctionParameterSniff {
98114
);
99115

100116
/**
101-
* List of functions for which the `$autoload` parameter is optional.
117+
* Functions for which the `$autoload` parameter is optional.
102118
*
103119
* @since 3.2.0
104120
*
105-
* @var string[]
121+
* @var array<string, true>
106122
*/
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+
);
108127

109128
/**
110129
* The group name for this group of functions.
@@ -277,7 +296,7 @@ private function maybe_display_missing_autoload_warning( $stackPtr, $function_na
277296
$this->phpcsFile->recordMetric( $stackPtr, self::METRIC_NAME, 'param missing' );
278297

279298
// 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 ] ) ) {
281300
$this->phpcsFile->addWarning(
282301
'It is recommended to always pass the `$autoload` parameter when using %s() function.',
283302
$stackPtr,
@@ -315,24 +334,25 @@ private function check_autoload_value( array $autoload_info, $function_name ) {
315334

316335
$normalized_value = strtolower( $autoload_info['clean'] );
317336

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+
}
327347
}
328348

329-
if ( in_array( $function_name, $this->autoload_is_optional, true ) ) {
349+
if ( isset( $this->autoload_is_optional[ $function_name ] ) ) {
330350
$valid_values = $this->valid_values_add_and_update;
331351
} else {
332352
$valid_values = $this->valid_values_other_functions;
333353
}
334354

335-
if ( in_array( $normalized_value, $valid_values, true ) ) {
355+
if ( isset( $valid_values[ $normalized_value ] ) ) {
336356
$this->phpcsFile->recordMetric( $param_first_token, self::METRIC_NAME, $normalized_value );
337357
return;
338358
}
@@ -360,23 +380,23 @@ private function check_autoload_value( array $autoload_info, $function_name ) {
360380

361381
$known_discouraged_values = array_merge( $this->deprecated_values, $this->internal_values_non_fixable, $this->internal_values_fixable );
362382

363-
if ( in_array( $autoload_value, $known_discouraged_values, true ) ) {
383+
if ( isset( $known_discouraged_values[ $autoload_value ] ) ) {
364384
$metric_value = $autoload_value;
365385
} else {
366386
$metric_value = 'other value';
367387
}
368388

369389
$this->phpcsFile->recordMetric( $param_first_token, self::METRIC_NAME, $metric_value );
370390

371-
if ( in_array( $autoload_value, $this->deprecated_values, true ) ) {
391+
if ( isset( $this->deprecated_values[ $autoload_value ] ) ) {
372392
$message = 'The use of `%s` as the value of the `$autoload` parameter is deprecated. Use `%s` instead.';
373393
$error_code = 'Deprecated';
374394
$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 ] ) ) {
376396
$message = 'The use of `%s` as the value of the `$autoload` parameter is discouraged. Use `%s` instead.';
377397
$error_code = 'InternalUseOnly';
378398
$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 ] ) ) {
380400
$message = 'The use of `%s` as the value of the `$autoload` parameter is discouraged.';
381401
$error_code = 'InternalUseOnly';
382402
$data = array( $autoload_info['clean'] );
@@ -394,7 +414,7 @@ function ( $value ) {
394414
$data = array( $autoload_info['clean'], $valid_values_string );
395415
}
396416

397-
if ( in_array( $autoload_value, array_keys( $this->fixable_values ), true ) ) {
417+
if ( isset( $this->fixable_values[ $autoload_value ] ) ) {
398418
$fix = $this->phpcsFile->addFixableWarning(
399419
$message,
400420
$param_first_token,

0 commit comments

Comments
 (0)