Skip to content

Commit c528826

Browse files
committed
PHPCS: Update ruleset to exclude classes from MethodDeclaration.Underscore sniff
Fix CS in src/WP_CLI
1 parent c263125 commit c528826

File tree

4 files changed

+139
-108
lines changed

4 files changed

+139
-108
lines changed

phpcs.xml.dist

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@
5353

5454
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedNamespaceFound">
5555
<exclude-pattern>*/src/WP_CLI/Fetchers/(Comment|Post|Site|User)\.php$</exclude-pattern>
56+
<exclude-pattern>*/src/WP_CLI/CommandWith(DBObject|Meta|Terms)\.php$</exclude-pattern>
57+
</rule>
58+
59+
<!-- Whitelisting to provide backward compatibility to classes possibly extending this class. -->
60+
<rule ref="PSR2.Methods.MethodDeclaration.Underscore">
61+
<exclude-pattern>*/src/WP_CLI/CommandWithDBObject\.php$</exclude-pattern>
5662
</rule>
5763

5864
</ruleset>

src/WP_CLI/CommandWithDBObject.php

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
namespace WP_CLI;
44

5+
use WP_CLI;
6+
use WP_CLI_Command;
7+
use WP_CLI\Formatter;
8+
use WP_CLI\Utils;
9+
510
/**
611
* Base class for WP-CLI commands that deal with database objects.
712
*
813
* @package wp-cli
914
*/
10-
abstract class CommandWithDBObject extends \WP_CLI_Command {
15+
abstract class CommandWithDBObject extends WP_CLI_Command {
1116

1217
/**
1318
* @var string $object_type WordPress' expected name for the object.
@@ -38,13 +43,14 @@ protected function _create( $args, $assoc_args, $callback ) {
3843
$obj_id = $callback( $assoc_args );
3944

4045
if ( is_wp_error( $obj_id ) ) {
41-
\WP_CLI::error( $obj_id );
46+
WP_CLI::error( $obj_id );
4247
}
4348

44-
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' ) )
45-
\WP_CLI::line( $obj_id );
46-
else
47-
\WP_CLI::success( "Created $this->obj_type $obj_id." );
49+
if ( Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
50+
WP_CLI::line( $obj_id );
51+
} else {
52+
WP_CLI::success( "Created $this->obj_type $obj_id." );
53+
}
4854
}
4955

5056
/**
@@ -59,23 +65,25 @@ protected function _update( $args, $assoc_args, $callback ) {
5965
$status = 0;
6066

6167
if ( empty( $assoc_args ) ) {
62-
\WP_CLI::error( "Need some fields to update." );
68+
WP_CLI::error( 'Need some fields to update.' );
6369
}
6470

65-
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'defer-term-counting' ) ) {
71+
if ( Utils\get_flag_value( $assoc_args, 'defer-term-counting' ) ) {
6672
wp_defer_term_counting( true );
6773
}
6874

6975
foreach ( $args as $obj_id ) {
70-
$params = array_merge( $assoc_args, array( $this->obj_id_key => $obj_id ) );
71-
72-
$status = $this->success_or_failure( $this->wp_error_to_resp(
73-
$callback( $params ),
74-
"Updated $this->obj_type $obj_id."
75-
) );
76+
$params = array_merge( $assoc_args, [ $this->obj_id_key => $obj_id ] );
77+
78+
$status = $this->success_or_failure(
79+
$this->wp_error_to_resp(
80+
$callback( $params ),
81+
"Updated $this->obj_type $obj_id."
82+
)
83+
);
7684
}
7785

78-
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'defer-term-counting' ) ) {
86+
if ( Utils\get_flag_value( $assoc_args, 'defer-term-counting' ) ) {
7987
wp_defer_term_counting( false );
8088
}
8189

@@ -89,7 +97,7 @@ protected function _update( $args, $assoc_args, $callback ) {
8997
* @return array
9098
*/
9199
protected static function process_csv_arguments_to_arrays( $assoc_args ) {
92-
foreach( $assoc_args as $k => $v ) {
100+
foreach ( $assoc_args as $k => $v ) {
93101
if ( false !== strpos( $k, '__' ) ) {
94102
$assoc_args[ $k ] = explode( ',', $v );
95103
}
@@ -108,16 +116,16 @@ protected static function process_csv_arguments_to_arrays( $assoc_args ) {
108116
protected function _delete( $args, $assoc_args, $callback ) {
109117
$status = 0;
110118

111-
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'defer-term-counting' ) ) {
119+
if ( Utils\get_flag_value( $assoc_args, 'defer-term-counting' ) ) {
112120
wp_defer_term_counting( true );
113121
}
114122

115123
foreach ( $args as $obj_id ) {
116-
$r = $callback( $obj_id, $assoc_args );
124+
$r = $callback( $obj_id, $assoc_args );
117125
$status = $this->success_or_failure( $r );
118126
}
119127

120-
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'defer-term-counting' ) ) {
128+
if ( Utils\get_flag_value( $assoc_args, 'defer-term-counting' ) ) {
121129
wp_defer_term_counting( false );
122130
}
123131

@@ -132,10 +140,11 @@ protected function _delete( $args, $assoc_args, $callback ) {
132140
* @return array
133141
*/
134142
protected function wp_error_to_resp( $r, $success_msg ) {
135-
if ( is_wp_error( $r ) )
136-
return array( 'error', $r->get_error_message() );
137-
else
138-
return array( 'success', $success_msg );
143+
if ( is_wp_error( $r ) ) {
144+
return [ 'error', $r->get_error_message() ];
145+
} else {
146+
return [ 'success', $success_msg ];
147+
}
139148
}
140149

141150
/**
@@ -147,11 +156,11 @@ protected function wp_error_to_resp( $r, $success_msg ) {
147156
protected function success_or_failure( $r ) {
148157
list( $type, $msg ) = $r;
149158

150-
if ( 'success' == $type ) {
151-
\WP_CLI::success( $msg );
159+
if ( 'success' === $type ) {
160+
WP_CLI::success( $msg );
152161
$status = 0;
153162
} else {
154-
\WP_CLI::warning( $msg );
163+
WP_CLI::warning( $msg );
155164
$status = 1;
156165
}
157166

@@ -175,7 +184,7 @@ protected function get_formatter( &$assoc_args ) {
175184
} else {
176185
$fields = $this->obj_fields;
177186
}
178-
return new \WP_CLI\Formatter( $assoc_args, $fields, $this->obj_type );
187+
return new Formatter( $assoc_args, $fields, $this->obj_type );
179188
}
180189

181190
/**
@@ -187,7 +196,7 @@ protected function get_formatter( &$assoc_args ) {
187196
protected function _url( $args, $callback ) {
188197
foreach ( $args as $obj_id ) {
189198
$object = $this->fetcher->get_check( $obj_id );
190-
\WP_CLI::line( $callback( $object->{$this->obj_id_key} ) );
199+
WP_CLI::line( $callback( $object->{$this->obj_id_key} ) );
191200
}
192201
}
193202
}

0 commit comments

Comments
 (0)