Skip to content

Commit e9a2bbd

Browse files
committed
Improve conditional structures
1 parent ef52380 commit e9a2bbd

File tree

5 files changed

+63
-70
lines changed

5 files changed

+63
-70
lines changed

src/Comment_Command.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,9 @@ public function generate( $args, $assoc_args ) {
190190
if ( 'progress' === $format ) {
191191
$notify->tick();
192192
} elseif ( 'ids' === $format ) {
193-
if ( 'ids' === $format ) {
194-
echo $comment_id;
195-
if ( $i < $limit - 1 ) {
196-
echo ' ';
197-
}
193+
echo $comment_id;
194+
if ( $i < $limit - 1 ) {
195+
echo ' ';
198196
}
199197
}
200198
}
@@ -415,15 +413,12 @@ function ( $comment_id, $assoc_args ) {
415413
$status = wp_get_comment_status( $comment_id );
416414
$result = wp_delete_comment( $comment_id, $force );
417415

418-
if ( $result ) {
419-
if ( $force || 'trash' === $status ) {
420-
return [ 'success', "Deleted comment $comment_id." ];
421-
} else {
422-
return [ 'success', "Trashed comment $comment_id." ];
423-
}
424-
} else {
416+
if ( ! $result ) {
425417
return [ 'error', "Failed deleting comment $comment_id." ];
426418
}
419+
420+
$verb = ( $force || 'trash' === $status ) ? 'Deleted' : 'Trashed';
421+
return [ 'success', "$verb comment $comment_id." ];
427422
}
428423
);
429424
}
@@ -433,11 +428,11 @@ private function call( $args, $status, $success, $failure ) {
433428

434429
$func = sprintf( 'wp_%s_comment', $status );
435430

436-
if ( $func( $comment_id ) ) {
437-
WP_CLI::success( "$success comment $comment_id." );
438-
} else {
431+
if ( ! $func( $comment_id ) ) {
439432
WP_CLI::error( "$failure comment $comment_id." );
440433
}
434+
435+
WP_CLI::success( "$success comment $comment_id." );
441436
}
442437

443438
private function set_status( $args, $status, $success ) {
@@ -447,9 +442,9 @@ private function set_status( $args, $status, $success ) {
447442

448443
if ( is_wp_error( $result ) ) {
449444
WP_CLI::error( $result );
450-
} else {
451-
WP_CLI::success( "$success comment $comment->comment_ID." );
452445
}
446+
447+
WP_CLI::success( "$success comment $comment->comment_ID." );
453448
}
454449

455450
/**

src/Site_Command.php

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -775,26 +775,22 @@ public function set_private( $args ) {
775775
}
776776

777777
private function update_site_status( $ids, $pref, $value ) {
778-
if ( 'archived' === $pref && 1 === $value ) {
779-
$action = 'archived';
780-
} elseif ( 'archived' === $pref && 0 === $value ) {
781-
$action = 'unarchived';
782-
} elseif ( 'deleted' === $pref && 1 === $value ) {
783-
$action = 'deactivated';
784-
} elseif ( 'deleted' === $pref && 0 === $value ) {
785-
$action = 'activated';
786-
} elseif ( 'spam' === $pref && 1 === $value ) {
787-
$action = 'marked as spam';
788-
} elseif ( 'spam' === $pref && 0 === $value ) {
789-
$action = 'removed from spam';
790-
} elseif ( 'public' === $pref && 1 === $value ) {
791-
$action = 'marked as public';
792-
} elseif ( 'public' === $pref && 0 === $value ) {
793-
$action = 'marked as private';
794-
} elseif ( 'mature' === $pref && 1 === $value ) {
795-
$action = 'marked as mature';
796-
} elseif ( 'mature' === $pref && 0 === $value ) {
797-
$action = 'marked as unmature';
778+
switch ( $pref ) {
779+
case 'archived':
780+
$action = $value ? 'archived' : 'unarchived';
781+
break;
782+
case 'deleted':
783+
$action = $value ? 'deactivated' : 'activated';
784+
break;
785+
case 'mature':
786+
$action = $value ? 'marked as mature' : 'marked as unmature';
787+
break;
788+
case 'public':
789+
$action = $value ? 'marked as public' : 'marked as private';
790+
break;
791+
case 'spam':
792+
$action = $value ? 'marked as spam' : 'removed from spam';
793+
break;
798794
}
799795

800796
foreach ( $ids as $site_id ) {

src/User_Command.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,11 @@ function ( $user ) use ( $network, $reassign ) {
289289
$message = "Removed user {$user_id} from " . home_url() . '.';
290290
}
291291

292-
if ( $result ) {
293-
return [ 'success', $message ];
294-
} else {
292+
if ( ! $result ) {
295293
return [ 'error', "Failed deleting user {$user_id}." ];
296294
}
295+
296+
return [ 'success', $message ];
297297
}
298298
);
299299
}
@@ -936,13 +936,17 @@ public function import_csv( $args, $assoc_args ) {
936936
foreach ( $file_object as $line ) {
937937
if ( empty( $line[0] ) ) {
938938
continue;
939-
} elseif ( empty( $indexes ) ) {
939+
}
940+
941+
if ( empty( $indexes ) ) {
940942
$indexes = $line;
941943
continue;
942944
}
945+
943946
foreach ( $indexes as $n => $key ) {
944947
$data[ $key ] = $line[ $n ];
945948
}
949+
946950
$csv_data[] = $data;
947951
}
948952
} else {
@@ -993,8 +997,9 @@ public function import_csv( $args, $assoc_args ) {
993997
WP_CLI::log( "{$existing_user->user_login} exists and has been skipped." );
994998
continue;
995999

996-
} elseif ( $existing_user ) {
1000+
}
9971001

1002+
if ( $existing_user ) {
9981003
$new_user['ID'] = $existing_user->ID;
9991004
$user_id = wp_update_user( $new_user );
10001005

@@ -1037,7 +1042,9 @@ public function import_csv( $args, $assoc_args ) {
10371042
WP_CLI::warning( $user_id );
10381043
continue;
10391044

1040-
} elseif ( false === $new_user['role'] ) {
1045+
}
1046+
1047+
if ( false === $new_user['role'] ) {
10411048
delete_user_option( $user_id, 'capabilities' );
10421049
delete_user_option( $user_id, 'user_level' );
10431050
}
@@ -1179,11 +1186,8 @@ private function update_msuser_status( $user_ids, $pref, $value ) {
11791186
}
11801187

11811188
if ( 'spam' === $pref && '1' === $value ) {
1182-
$action = 'marked as spam';
1183-
$verb = 'spam';
1184-
} elseif ( 'spam' === $pref && '0' === $value ) {
1185-
$action = 'removed from spam';
1186-
$verb = 'unspam';
1189+
$action = (int) $value ? 'marked as spam' : 'removed from spam';
1190+
$verb = (int) $value ? 'spam' : 'unspam';
11871191
}
11881192

11891193
$successes = 0;

src/WP_CLI/CommandWithDBObject.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,9 @@ protected function _delete( $args, $assoc_args, $callback ) {
140140
* @return array
141141
*/
142142
protected function wp_error_to_resp( $response, $success_msg ) {
143-
if ( is_wp_error( $response ) ) {
144-
return [ 'error', $response->get_error_message() ];
145-
} else {
146-
return [ 'success', $success_msg ];
147-
}
143+
return is_wp_error( $response )
144+
? [ 'error', $response->get_error_message() ]
145+
: [ 'success', $success_msg ];
148146
}
149147

150148
/**

src/WP_CLI/CommandWithTerms.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -166,37 +166,37 @@ public function remove( $args, $assoc_args ) {
166166
$default_category = ( ! empty( $default_category ) ) ? $default_category : 1;
167167
$default_category = wp_set_object_terms( $object_id, [ $default_category ], $taxonomy, true );
168168

169-
if ( ! is_wp_error( $default_category ) ) {
170-
$message = 'Removed all terms and set default term.';
171-
} else {
169+
if ( is_wp_error( $default_category ) ) {
172170
WP_CLI::error( 'Failed to set default term.' );
173171
}
172+
173+
$message = 'Removed all terms and set default term.';
174174
}
175175

176-
if ( ! is_wp_error( $result ) ) {
177-
WP_CLI::success( $message );
178-
} else {
176+
if ( is_wp_error( $result ) ) {
179177
WP_CLI::error( 'Failed to remove all terms.' );
180178
}
181-
return;
182179

183-
} else {
180+
WP_CLI::success( $message );
184181

185-
// Abort if no terms are specified.
186-
if ( ! $terms ) {
187-
WP_CLI::error( 'Please specify one or more terms, or use --all.' );
188-
}
182+
return;
183+
}
189184

190-
// Remove term from post.
191-
$result = wp_remove_object_terms( $object_id, $terms, $taxonomy );
185+
// Abort if no terms are specified.
186+
if ( ! $terms ) {
187+
WP_CLI::error( 'Please specify one or more terms, or use --all.' );
192188
}
193189

190+
// Remove term from post.
191+
$result = wp_remove_object_terms( $object_id, $terms, $taxonomy );
192+
194193
$label = count( $terms ) > 1 ? 'terms' : 'term';
195-
if ( ! is_wp_error( $result ) ) {
196-
WP_CLI::success( "Removed {$label}." );
197-
} else {
194+
195+
if ( is_wp_error( $result ) ) {
198196
WP_CLI::error( "Failed to remove {$label}." );
199197
}
198+
199+
WP_CLI::success( "Removed {$label}." );
200200
}
201201

202202
/**

0 commit comments

Comments
 (0)