Skip to content

Commit f4d37e9

Browse files
committed
PHPCS: Update ruleset to exclude classes from PrefixAllGlobals.NonPrefixedClassFound sniff
Fix CS in Option command
1 parent a877b0f commit f4d37e9

File tree

2 files changed

+47
-34
lines changed

2 files changed

+47
-34
lines changed

phpcs.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
</rule>
5959

6060
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound">
61+
<exclude-pattern>*/src/Option_Command\.php$</exclude-pattern>
6162
<exclude-pattern>*/src/Post(_Meta|_Term|_Type)?_Command\.php$</exclude-pattern>
6263
<exclude-pattern>*/src/Site(_Meta|_Option)?_Command\.php$</exclude-pattern>
6364
<exclude-pattern>*/src/Term(_Meta)?_Command\.php$</exclude-pattern>

src/Option_Command.php

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public function add( $args, $assoc_args ) {
131131
$autoload = 'yes';
132132
}
133133

134-
if ( !add_option( $key, $value, '', $autoload ) ) {
134+
if ( ! add_option( $key, $value, '', $autoload ) ) {
135135
WP_CLI::error( "Could not add option '{$key}'. Does it already exist?" );
136136
} else {
137137
WP_CLI::success( "Added '{$key}' option." );
@@ -242,12 +242,12 @@ public function add( $args, $assoc_args ) {
242242
public function list_( $args, $assoc_args ) {
243243

244244
global $wpdb;
245-
$pattern = '%';
246-
$exclude = '';
247-
$fields = array( 'option_name', 'option_value' );
248-
$size_query = ",LENGTH(option_value) AS `size_bytes`";
245+
$pattern = '%';
246+
$exclude = '';
247+
$fields = array( 'option_name', 'option_value' );
248+
$size_query = ',LENGTH(option_value) AS `size_bytes`';
249249
$autoload_query = '';
250-
$sort = Utils\get_flag_value( $assoc_args, 'order' );
250+
$sort = Utils\get_flag_value( $assoc_args, 'order' );
251251

252252
if ( isset( $assoc_args['search'] ) ) {
253253
$pattern = self::esc_like( $assoc_args['search'] );
@@ -267,8 +267,8 @@ public function list_( $args, $assoc_args ) {
267267
}
268268

269269
if ( Utils\get_flag_value( $assoc_args, 'format' ) === 'total_bytes' ) {
270-
$fields = array( 'size_bytes' );
271-
$size_query = ",SUM(LENGTH(option_value)) AS `size_bytes`";
270+
$fields = array( 'size_bytes' );
271+
$size_query = ',SUM(LENGTH(option_value)) AS `size_bytes`';
272272
}
273273

274274
if ( isset( $assoc_args['autoload'] ) ) {
@@ -295,28 +295,33 @@ public function list_( $args, $assoc_args ) {
295295

296296
$where = '';
297297
if ( $pattern ) {
298-
$where .= $wpdb->prepare( "WHERE `option_name` LIKE %s", $pattern );
298+
$where .= $wpdb->prepare( 'WHERE `option_name` LIKE %s', $pattern );
299299
}
300300

301301
if ( $exclude ) {
302-
$where .= $wpdb->prepare( " AND `option_name` NOT LIKE %s", $exclude );
302+
$where .= $wpdb->prepare( ' AND `option_name` NOT LIKE %s', $exclude );
303303
}
304304
$where .= $autoload_query . $transients_query;
305305

306-
$results = $wpdb->get_results( "SELECT `option_name`,`option_value`,`autoload`" . $size_query
307-
. " FROM `$wpdb->options` {$where}" );
306+
$results = $wpdb->get_results(
307+
'SELECT `option_name`,`option_value`,`autoload`' . $size_query
308+
. " FROM `$wpdb->options` {$where}"
309+
);
308310

309311
$orderby = Utils\get_flag_value( $assoc_args, 'orderby' );
310312
$order = Utils\get_flag_value( $assoc_args, 'order' );
311313

312314
// Sort result.
313315
if ( 'option_id' !== $orderby ) {
314-
usort( $results, function ( $a, $b ) use ( $orderby, $order ) {
315-
// Sort array.
316-
return 'asc' === $order
316+
usort(
317+
$results,
318+
function ( $a, $b ) use ( $orderby, $order ) {
319+
// Sort array.
320+
return 'asc' === $order
317321
? $a->$orderby > $b->$orderby
318322
: $a->$orderby < $b->$orderby;
319-
});
323+
}
324+
);
320325
} elseif ( 'option_id' === $orderby && 'desc' === $order ) { // Sort by default descending.
321326
krsort( $results );
322327
}
@@ -408,13 +413,13 @@ public function update( $args, $assoc_args ) {
408413
$value = WP_CLI::read_value( $value, $assoc_args );
409414

410415
$autoload = Utils\get_flag_value( $assoc_args, 'autoload' );
411-
if ( ! in_array( $autoload, array( 'yes', 'no' ) ) ) {
416+
if ( ! in_array( $autoload, [ 'yes', 'no' ], true ) ) {
412417
$autoload = null;
413418
}
414419

415420
$value = sanitize_option( $key, $value );
416421
// Sanitization WordPress normally performs when getting an option
417-
if ( in_array( $key, array('siteurl', 'home', 'category_base', 'tag_base') ) ) {
422+
if ( in_array( $key, [ 'siteurl', 'home', 'category_base', 'tag_base' ], true ) ) {
418423
$value = untrailingslashit( $value );
419424
}
420425
$old_value = sanitize_option( $key, get_option( $key ) );
@@ -490,12 +495,15 @@ public function pluck( $args, $assoc_args ) {
490495
WP_CLI::halt( 1 );
491496
}
492497

493-
$key_path = array_map( function( $key ) {
494-
if ( is_numeric( $key ) && ( $key === (string) intval( $key ) ) ) {
495-
return (int) $key;
496-
}
497-
return $key;
498-
}, array_slice( $args, 1 ) );
498+
$key_path = array_map(
499+
function( $key ) {
500+
if ( is_numeric( $key ) && ( (string) intval( $key ) === $key ) ) {
501+
return (int) $key;
502+
}
503+
return $key;
504+
},
505+
array_slice( $args, 1 )
506+
);
499507

500508
$traverser = new RecursiveDataStructureTraverser( $value );
501509

@@ -542,14 +550,17 @@ public function pluck( $args, $assoc_args ) {
542550
*/
543551
public function patch( $args, $assoc_args ) {
544552
list( $action, $key ) = $args;
545-
$key_path = array_map( function( $key ) {
546-
if ( is_numeric( $key ) && ( $key === (string) intval( $key ) ) ) {
547-
return (int) $key;
548-
}
549-
return $key;
550-
}, array_slice( $args, 2 ) );
553+
$key_path = array_map(
554+
function( $key ) {
555+
if ( is_numeric( $key ) && ( (string) intval( $key ) === $key ) ) {
556+
return (int) $key;
557+
}
558+
return $key;
559+
},
560+
array_slice( $args, 2 )
561+
);
551562

552-
if ( 'delete' == $action ) {
563+
if ( 'delete' === $action ) {
553564
$patch_value = null;
554565
} else {
555566
$stdin_value = EntityUtils::has_stdin()
@@ -561,7 +572,8 @@ public function patch( $args, $assoc_args ) {
561572
}
562573

563574
/* Need to make a copy of $current_value here as it is modified by reference */
564-
$old_value = $current_value = sanitize_option( $key, get_option( $key ) );
575+
$old_value = sanitize_option( $key, get_option( $key ) );
576+
$current_value = $old_value;
565577
if ( is_object( $current_value ) ) {
566578
$old_value = clone $current_value;
567579
}
@@ -591,11 +603,11 @@ private static function esc_like( $old ) {
591603
global $wpdb;
592604

593605
// Remove notices in 4.0 and support backwards compatibility
594-
if( method_exists( $wpdb, 'esc_like' ) ) {
606+
if ( method_exists( $wpdb, 'esc_like' ) ) {
595607
// 4.0
596608
$old = $wpdb->esc_like( $old );
597609
} else {
598-
// 3.9 or less
610+
// phpcs:ignore WordPress.WP.DeprecatedFunctions.like_escapeFound -- called in WordPress 3.9 or less.
599611
$old = like_escape( esc_sql( $old ) );
600612
}
601613

0 commit comments

Comments
 (0)