Skip to content

Commit 8595f9f

Browse files
committed
PHPCS: Update ruleset to exclude classes from PrefixAllGlobals.NonPrefixedClassFound sniff
Fix CS in Term command
1 parent 1f09a46 commit 8595f9f

File tree

2 files changed

+92
-74
lines changed

2 files changed

+92
-74
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/Term(_Meta)?_Command\.php$</exclude-pattern>
6162
<exclude-pattern>*/src/User(_Meta|_Session|_Term)?_Command\.php$</exclude-pattern>
6263
</rule>
6364

src/Term_Command.php

Lines changed: 91 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@
3838
*/
3939
class Term_Command extends WP_CLI_Command {
4040

41-
private $fields = array(
41+
private $fields = [
4242
'term_id',
4343
'term_taxonomy_id',
4444
'name',
4545
'slug',
4646
'description',
4747
'parent',
4848
'count',
49-
);
49+
];
5050

5151
/**
5252
* Lists terms in a taxonomy.
@@ -126,40 +126,43 @@ public function list_( $args, $assoc_args ) {
126126

127127
$formatter = $this->get_formatter( $assoc_args );
128128

129-
$defaults = array(
129+
$defaults = [
130130
'hide_empty' => false,
131-
);
131+
];
132132
$assoc_args = array_merge( $defaults, $assoc_args );
133133

134134
if ( ! empty( $assoc_args['term_id'] ) ) {
135-
$term = get_term_by( 'id', $assoc_args['term_id'], $args[0] );
136-
$terms = array( $term );
137-
} else if ( ! empty( $assoc_args['include'] )
135+
$term = get_term_by( 'id', $assoc_args['term_id'], $args[0] );
136+
$terms = [ $term ];
137+
} elseif ( ! empty( $assoc_args['include'] )
138138
&& ! empty( $assoc_args['orderby'] )
139139
&& 'include' === $assoc_args['orderby']
140140
&& Utils\wp_version_compare( '4.7', '<' ) ) {
141-
$terms = array();
141+
$terms = [];
142142
$term_ids = explode( ',', $assoc_args['include'] );
143-
foreach( $term_ids as $term_id ) {
143+
foreach ( $term_ids as $term_id ) {
144144
$term = get_term_by( 'id', $term_id, $args[0] );
145145
if ( $term && ! is_wp_error( $term ) ) {
146146
$terms[] = $term;
147147
} else {
148-
WP_CLI::warning( sprintf( "Invalid term %s.", $term_id ) );
148+
WP_CLI::warning( sprintf( 'Invalid term %s.', $term_id ) );
149149
}
150150
}
151151
} else {
152152
$terms = get_terms( $args, $assoc_args );
153153
}
154154

155-
$terms = array_map( function( $term ){
156-
$term->count = (int)$term->count;
157-
$term->parent = (int)$term->parent;
158-
$term->url = get_term_link( $term );
159-
return $term;
160-
}, $terms );
155+
$terms = array_map(
156+
function( $term ) {
157+
$term->count = (int) $term->count;
158+
$term->parent = (int) $term->parent;
159+
$term->url = get_term_link( $term );
160+
return $term;
161+
},
162+
$terms
163+
);
161164

162-
if ( 'ids' == $formatter->format ) {
165+
if ( 'ids' === $formatter->format ) {
163166
$terms = wp_list_pluck( $terms, 'term_id' );
164167
echo implode( ' ', $terms );
165168
} else {
@@ -200,14 +203,14 @@ public function create( $args, $assoc_args ) {
200203

201204
list( $taxonomy, $term ) = $args;
202205

203-
$defaults = array(
206+
$defaults = [
204207
'slug' => sanitize_title( $term ),
205208
'description' => '',
206209
'parent' => 0,
207-
);
210+
];
208211
$assoc_args = wp_parse_args( $assoc_args, $defaults );
209212

210-
$porcelain = \WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' );
213+
$porcelain = Utils\get_flag_value( $assoc_args, 'porcelain' );
211214
unset( $assoc_args['porcelain'] );
212215

213216
// Compatibility for < WP 4.0
@@ -216,16 +219,17 @@ public function create( $args, $assoc_args ) {
216219
}
217220

218221
$assoc_args = wp_slash( $assoc_args );
219-
$term = wp_slash( $term );
220-
$ret = wp_insert_term( $term, $taxonomy, $assoc_args );
222+
$term = wp_slash( $term );
223+
$ret = wp_insert_term( $term, $taxonomy, $assoc_args );
221224

222225
if ( is_wp_error( $ret ) ) {
223226
WP_CLI::error( $ret->get_error_message() );
224227
} else {
225-
if ( $porcelain )
228+
if ( $porcelain ) {
226229
WP_CLI::line( $ret['term_id'] );
227-
else
228-
WP_CLI::success( sprintf( "Created %s %d.", $taxonomy, $ret['term_id'] ) );
230+
} else {
231+
WP_CLI::success( sprintf( 'Created %s %d.', $taxonomy, $ret['term_id'] ) );
232+
}
229233
}
230234
}
231235

@@ -287,11 +291,11 @@ public function get( $args, $assoc_args ) {
287291
}
288292

289293
if ( empty( $assoc_args['fields'] ) ) {
290-
$term_array = get_object_vars( $term );
294+
$term_array = get_object_vars( $term );
291295
$assoc_args['fields'] = array_keys( $term_array );
292296
}
293297

294-
$term->count = (int) $term->count;
298+
$term->count = (int) $term->count;
295299
$term->parent = (int) $term->parent;
296300

297301
$formatter = $this->get_formatter( $assoc_args );
@@ -344,17 +348,18 @@ public function update( $args, $assoc_args ) {
344348

345349
list( $taxonomy, $term ) = $args;
346350

347-
$defaults = array(
351+
$defaults = [
348352
'name' => null,
349353
'slug' => null,
350354
'description' => null,
351355
'parent' => null,
352-
);
356+
];
353357
$assoc_args = wp_parse_args( $assoc_args, $defaults );
354358

355-
foreach( $assoc_args as $key => $value ) {
356-
if ( is_null( $value ) )
357-
unset( $assoc_args[$key] );
359+
foreach ( $assoc_args as $key => $value ) {
360+
if ( is_null( $value ) ) {
361+
unset( $assoc_args[ $key ] );
362+
}
358363
}
359364

360365
$assoc_args = wp_slash( $assoc_args );
@@ -370,10 +375,11 @@ public function update( $args, $assoc_args ) {
370375
// Update term.
371376
$ret = wp_update_term( $term->term_id, $taxonomy, $assoc_args );
372377

373-
if ( is_wp_error( $ret ) )
378+
if ( is_wp_error( $ret ) ) {
374379
WP_CLI::error( $ret->get_error_message() );
375-
else
376-
WP_CLI::success( "Term updated." );
380+
} else {
381+
WP_CLI::success( 'Term updated.' );
382+
}
377383
}
378384

379385
/**
@@ -417,16 +423,18 @@ public function update( $args, $assoc_args ) {
417423
* Deleted post_tag 161.
418424
* Success: Deleted 3 of 3 terms.
419425
*/
420-
public function delete( $args, $assoc_args ) {
426+
public function delete( $args, $assoc_args ) {
421427
$taxonomy = array_shift( $args );
422428

423-
$successes = $errors = 0;
429+
$successes = 0;
430+
$errors = 0;
424431
foreach ( $args as $term_id ) {
425432

426433
$term = $term_id;
427434

428435
// Get term by specified argument otherwise get term by id.
429-
if ( 'slug' === ( $field = Utils\get_flag_value( $assoc_args, 'by' ) ) ) {
436+
$field = Utils\get_flag_value( $assoc_args, 'by' );
437+
if ( 'slug' === $field ) {
430438

431439
// Get term by slug.
432440
$term = get_term_by( $field, $term, $taxonomy );
@@ -447,8 +455,8 @@ public function delete( $args, $assoc_args ) {
447455
if ( is_wp_error( $ret ) ) {
448456
WP_CLI::warning( $ret );
449457
$errors++;
450-
} else if ( $ret ) {
451-
WP_CLI::log( sprintf( "Deleted %s %d.", $taxonomy, $term_id ) );
458+
} elseif ( $ret ) {
459+
WP_CLI::log( sprintf( 'Deleted %s %d.', $taxonomy, $term_id ) );
452460
$successes++;
453461
} else {
454462
WP_CLI::warning( sprintf( "%s %s doesn't exist.", $taxonomy, $term_id ) );
@@ -505,37 +513,39 @@ public function generate( $args, $assoc_args ) {
505513

506514
list ( $taxonomy ) = $args;
507515

508-
$defaults = array(
509-
'count' => 100,
516+
$defaults = [
517+
'count' => 100,
510518
'max_depth' => 1,
511-
);
519+
];
512520

513-
extract( array_merge( $defaults, $assoc_args ), EXTR_SKIP );
521+
$final_args = array_merge( $defaults, $assoc_args );
522+
$count = $final_args['count'];
523+
$max_depth = $final_args['max_depth'];
514524

515-
if ( !taxonomy_exists( $taxonomy ) ) {
525+
if ( ! taxonomy_exists( $taxonomy ) ) {
516526
WP_CLI::error( sprintf( "'%s' is not a registered taxonomy.", $taxonomy ) );
517527
}
518528

519529
$label = get_taxonomy( $taxonomy )->labels->singular_name;
520-
$slug = sanitize_title_with_dashes( $label );
530+
$slug = sanitize_title_with_dashes( $label );
521531

522532
$hierarchical = get_taxonomy( $taxonomy )->hierarchical;
523533

524-
$format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'progress' );
534+
$format = Utils\get_flag_value( $assoc_args, 'format', 'progress' );
525535

526536
$notify = false;
527537
if ( 'progress' === $format ) {
528-
$notify = \WP_CLI\Utils\make_progress_bar( 'Generating terms', $count );
538+
$notify = Utils\make_progress_bar( 'Generating terms', $count );
529539
}
530540

531541
$previous_term_id = 0;
532-
$current_parent = 0;
533-
$current_depth = 1;
542+
$current_parent = 0;
543+
$current_depth = 1;
534544

535-
$max_id = (int) $wpdb->get_var( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy ORDER BY term_taxonomy_id DESC LIMIT 1" );
545+
$max_id = (int) $wpdb->get_var( "SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} ORDER BY term_taxonomy_id DESC LIMIT 1" );
536546

537547
$suspend_cache_invalidation = wp_suspend_cache_invalidation( true );
538-
$created = array();
548+
$created = [];
539549

540550
for ( $i = $max_id + 1; $i <= $max_id + $count; $i++ ) {
541551

@@ -546,26 +556,25 @@ public function generate( $args, $assoc_args ) {
546556
$current_parent = $previous_term_id;
547557
$current_depth++;
548558

549-
} else if ( $this->maybe_reset_depth() ) {
559+
} elseif ( $this->maybe_reset_depth() ) {
550560

551561
$current_parent = 0;
552-
$current_depth = 1;
562+
$current_depth = 1;
553563

554564
}
555-
556565
}
557566

558-
$args = array(
567+
$args = [
559568
'parent' => $current_parent,
560-
'slug' => $slug . "-$i",
561-
);
569+
'slug' => $slug . "-$i",
570+
];
562571

563572
$name = "$label $i";
564573
$term = wp_insert_term( $name, $taxonomy, $args );
565574
if ( is_wp_error( $term ) ) {
566575
WP_CLI::warning( $term );
567576
} else {
568-
$created[] = $term['term_id'];
577+
$created[] = $term['term_id'];
569578
$previous_term_id = $term['term_id'];
570579
if ( 'ids' === $format ) {
571580
echo $term['term_id'];
@@ -619,20 +628,19 @@ public function generate( $args, $assoc_args ) {
619628
* Success: Updated post_format term count.
620629
*/
621630
public function recount( $args ) {
622-
foreach( $args as $taxonomy ) {
631+
foreach ( $args as $taxonomy ) {
623632

624633
if ( ! taxonomy_exists( $taxonomy ) ) {
625-
WP_CLI::warning( sprintf( "Taxonomy %s does not exist.", $taxonomy ) );
634+
WP_CLI::warning( sprintf( 'Taxonomy %s does not exist.', $taxonomy ) );
626635
} else {
627636

628-
$terms = get_terms( $taxonomy, array( 'hide_empty' => false ) );
637+
$terms = get_terms( $taxonomy, [ 'hide_empty' => false ] );
629638
$term_taxonomy_ids = wp_list_pluck( $terms, 'term_taxonomy_id' );
630639

631640
wp_update_term_count( $term_taxonomy_ids, $taxonomy );
632641

633-
WP_CLI::success( sprintf( "Updated %s term count.", $taxonomy ) );
642+
WP_CLI::success( sprintf( 'Updated %s term count.', $taxonomy ) );
634643
}
635-
636644
}
637645
}
638646

@@ -668,9 +676,10 @@ public function recount( $args ) {
668676
* Success: Migrated the term '9190' from taxonomy 'category' to taxonomy 'post_tag' for 1 posts
669677
*/
670678
public function migrate( $args, $assoc_args ) {
671-
$clean_term_cache = $values = array();
672-
$term_reference = $args[0];
673-
$original_taxonomy = Utils\get_flag_value( $assoc_args, 'from' );
679+
$clean_term_cache = [];
680+
$values = [];
681+
$term_reference = $args[0];
682+
$original_taxonomy = Utils\get_flag_value( $assoc_args, 'from' );
674683
$destination_taxonomy = Utils\get_flag_value( $assoc_args, 'to' );
675684

676685
$term = get_term_by( Utils\get_flag_value( $assoc_args, 'by' ), $term_reference, $original_taxonomy );
@@ -681,7 +690,15 @@ public function migrate( $args, $assoc_args ) {
681690

682691
$original_taxonomy = get_taxonomy( $original_taxonomy );
683692

684-
$id = wp_insert_term( $term->name, $destination_taxonomy, array( 'slug' => $term->slug, 'parent' => 0, 'description' => $term->description ) );
693+
$id = wp_insert_term(
694+
$term->name,
695+
$destination_taxonomy,
696+
[
697+
'slug' => $term->slug,
698+
'parent' => 0,
699+
'description' => $term->description,
700+
]
701+
);
685702

686703
if ( is_wp_error( $id ) ) {
687704
WP_CLI::error( $id->get_error_message() );
@@ -691,7 +708,7 @@ public function migrate( $args, $assoc_args ) {
691708

692709
foreach ( $post_ids as $post_id ) {
693710
$type = get_post_type( $post_id );
694-
if ( in_array( $type, $original_taxonomy->object_type ) ) {
711+
if ( in_array( $type, $original_taxonomy->object_type, true ) ) {
695712
$term_taxonomy_id = wp_set_object_terms( $post_id, $id['term_id'], $destination_taxonomy, true );
696713

697714
if ( is_wp_error( $term_taxonomy_id ) ) {
@@ -716,18 +733,18 @@ public function migrate( $args, $assoc_args ) {
716733

717734
WP_CLI::log( "Old instance of term '{$term->slug}' removed from its original taxonomy." );
718735
$post_count = count( $post_ids );
719-
$post_plural = WP_CLI\Utils\pluralize( 'post', $post_count );
736+
$post_plural = Utils\pluralize( 'post', $post_count );
720737
WP_CLI::success( "Migrated the term '{$term->slug}' from taxonomy '{$original_taxonomy->name}' to taxonomy '{$destination_taxonomy}' for {$post_count} {$post_plural}." );
721738
}
722739

723740
private function maybe_make_child() {
724-
// 50% chance of making child term
725-
return ( mt_rand(1, 2) == 1 );
741+
// 50% chance of making child term.
742+
return ( wp_rand( 1, 2 ) === 1 );
726743
}
727744

728745
private function maybe_reset_depth() {
729-
// 10% chance of reseting to root depth
730-
return ( mt_rand(1, 10) == 7 );
746+
// 10% chance of reseting to root depth.
747+
return ( wp_rand( 1, 10 ) === 7 );
731748
}
732749

733750
private function get_formatter( &$assoc_args ) {

0 commit comments

Comments
 (0)