Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack_CLI often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack_CLI, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Jetpack_CLI extends WP_CLI_Command { |
||
9 | |||
10 | // Aesthetics |
||
11 | public $green_open = "\033[32m"; |
||
12 | public $red_open = "\033[31m"; |
||
13 | public $yellow_open = "\033[33m"; |
||
14 | public $color_close = "\033[0m"; |
||
15 | |||
16 | /** |
||
17 | * Get Jetpack Details |
||
18 | * |
||
19 | * ## OPTIONS |
||
20 | * |
||
21 | * empty: Leave it empty for basic stats |
||
22 | * |
||
23 | * full: View full stats. It's the data from the heartbeat |
||
24 | * |
||
25 | * ## EXAMPLES |
||
26 | * |
||
27 | * wp jetpack status |
||
28 | * wp jetpack status full |
||
29 | * |
||
30 | */ |
||
31 | public function status( $args, $assoc_args ) { |
||
32 | if ( ! Jetpack::is_active() ) { |
||
33 | WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) ); |
||
34 | } |
||
35 | |||
36 | View Code Duplication | if ( isset( $args[0] ) && 'full' !== $args[0] ) { |
|
37 | /* translators: %s is a command like "prompt" */ |
||
38 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $args[0] ) ); |
||
39 | } |
||
40 | |||
41 | /* |
||
42 | * Are they asking for all data? |
||
43 | * |
||
44 | * Loop through heartbeat data and organize by priority. |
||
45 | */ |
||
46 | $all_data = ( isset( $args[0] ) && 'full' == $args[0] ) ? 'full' : false; |
||
47 | if ( $all_data ) { |
||
|
|||
48 | WP_CLI::success( __( 'Jetpack is currently connected to WordPress.com', 'jetpack' ) ); |
||
49 | WP_CLI::line( sprintf( __( "The Jetpack Version is %s", 'jetpack' ), JETPACK__VERSION ) ); |
||
50 | WP_CLI::line( sprintf( __( "The WordPress.com blog_id is %d", 'jetpack' ), Jetpack_Options::get_option( 'id' ) ) ); |
||
51 | |||
52 | // Heartbeat data |
||
53 | WP_CLI::line( "\n" . __( 'Additional data: ', 'jetpack' ) ); |
||
54 | |||
55 | // Get the filtered heartbeat data. |
||
56 | // Filtered so we can color/list by severity |
||
57 | $stats = Jetpack::jetpack_check_heartbeat_data(); |
||
58 | |||
59 | // Display red flags first |
||
60 | foreach ( $stats['bad'] as $stat => $value ) { |
||
61 | printf( "$this->red_open%-'.16s %s $this->color_close\n", $stat, $value ); |
||
62 | } |
||
63 | |||
64 | // Display caution warnings next |
||
65 | foreach ( $stats['caution'] as $stat => $value ) { |
||
66 | printf( "$this->yellow_open%-'.16s %s $this->color_close\n", $stat, $value ); |
||
67 | } |
||
68 | |||
69 | // The rest of the results are good! |
||
70 | foreach ( $stats['good'] as $stat => $value ) { |
||
71 | |||
72 | // Modules should get special spacing for aestetics |
||
73 | if ( strpos( $stat, 'odule-' ) ) { |
||
74 | printf( "%-'.30s %s\n", $stat, $value ); |
||
75 | usleep( 4000 ); // For dramatic effect lolz |
||
76 | continue; |
||
77 | } |
||
78 | printf( "%-'.16s %s\n", $stat, $value ); |
||
79 | usleep( 4000 ); // For dramatic effect lolz |
||
80 | } |
||
81 | } else { |
||
82 | // Just the basics |
||
83 | WP_CLI::success( __( 'Jetpack is currently connected to WordPress.com', 'jetpack' ) ); |
||
84 | WP_CLI::line( sprintf( __( 'The Jetpack Version is %s', 'jetpack' ), JETPACK__VERSION ) ); |
||
85 | WP_CLI::line( sprintf( __( 'The WordPress.com blog_id is %d', 'jetpack' ), Jetpack_Options::get_option( 'id' ) ) ); |
||
86 | WP_CLI::line( "\n" . _x( "View full status with 'wp jetpack status full'", '"wp jetpack status full" is a command - do not translate', 'jetpack' ) ); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Disconnect Jetpack Blogs or Users |
||
92 | * |
||
93 | * ## OPTIONS |
||
94 | * |
||
95 | * blog: Disconnect the entire blog. |
||
96 | * |
||
97 | * user <user_identifier>: Disconnect a specific user from WordPress.com. |
||
98 | * |
||
99 | * Please note, the primary account that the blog is connected |
||
100 | * to WordPress.com with cannot be disconnected without |
||
101 | * disconnecting the entire blog. |
||
102 | * |
||
103 | * ## EXAMPLES |
||
104 | * |
||
105 | * wp jetpack disconnect blog |
||
106 | * wp jetpack disconnect user 13 |
||
107 | * wp jetpack disconnect user username |
||
108 | * wp jetpack disconnect user [email protected] |
||
109 | * |
||
110 | * @synopsis <blog|user> [<user_identifier>] |
||
111 | */ |
||
112 | public function disconnect( $args, $assoc_args ) { |
||
164 | |||
165 | /** |
||
166 | * Reset Jetpack options and settings to default |
||
167 | * |
||
168 | * ## OPTIONS |
||
169 | * |
||
170 | * modules: Resets modules to default state ( get_default_modules() ) |
||
171 | * |
||
172 | * options: Resets all Jetpack options except: |
||
173 | * - All private options (Blog token, user token, etc...) |
||
174 | * - id (The Client ID/WP.com Blog ID of this site) |
||
175 | * - master_user |
||
176 | * - version |
||
177 | * - activated |
||
178 | * |
||
179 | * ## EXAMPLES |
||
180 | * |
||
181 | * wp jetpack reset options |
||
182 | * wp jetpack reset modules |
||
183 | * |
||
184 | * @synopsis <modules|options> |
||
185 | */ |
||
186 | public function reset( $args, $assoc_args ) { |
||
241 | |||
242 | /** |
||
243 | * Manage Jetpack Modules |
||
244 | * |
||
245 | * ## OPTIONS |
||
246 | * |
||
247 | * list : View all available modules, and their status. |
||
248 | * activate all : Activate all modules |
||
249 | * deactivate all: Deactivate all modules |
||
250 | * |
||
251 | * activate <module_slug> : Activate a module. |
||
252 | * deactivate <module_slug> : Deactivate a module. |
||
253 | * toggle <module_slug> : Toggle a module on or off. |
||
254 | * |
||
255 | * ## EXAMPLES |
||
256 | * |
||
257 | * wp jetpack module list |
||
258 | * wp jetpack module activate stats |
||
259 | * wp jetpack module deactivate stats |
||
260 | * wp jetpack module toggle stats |
||
261 | * |
||
262 | * wp jetpack module activate all |
||
263 | * wp jetpack module deactivate all |
||
264 | * |
||
265 | * @synopsis <list|activate|deactivate|toggle> [<module_name>] |
||
266 | */ |
||
267 | public function module( $args, $assoc_args ) { |
||
334 | |||
335 | /** |
||
336 | * Manage Protect Settings |
||
337 | * |
||
338 | * ## OPTIONS |
||
339 | * |
||
340 | * whitelist: Whitelist an IP address. You can also read or clear the whitelist. |
||
341 | * |
||
342 | * |
||
343 | * ## EXAMPLES |
||
344 | * |
||
345 | * wp jetpack protect whitelist <ip address> |
||
346 | * wp jetpack protect whitelist list |
||
347 | * wp jetpack protect whitelist clear |
||
348 | * |
||
349 | * @synopsis <whitelist> [<ip|ip_low-ip_high|list|clear>] |
||
350 | */ |
||
351 | public function protect( $args, $assoc_args ) { |
||
455 | |||
456 | /** |
||
457 | * Manage Jetpack Options |
||
458 | * |
||
459 | * ## OPTIONS |
||
460 | * |
||
461 | * list : List all jetpack options and their values |
||
462 | * delete : Delete an option |
||
463 | * - can only delete options that are white listed. |
||
464 | * update : update an option |
||
465 | * - can only update option strings |
||
466 | * get : get the value of an option |
||
467 | * |
||
468 | * ## EXAMPLES |
||
469 | * |
||
470 | * wp jetpack options list |
||
471 | * wp jetpack options get <option_name> |
||
472 | * wp jetpack options delete <option_name> |
||
473 | * wp jetpack options update <option_name> [<option_value>] |
||
474 | * |
||
475 | * @synopsis <list|get|delete|update> [<option_name>] [<option_value>] |
||
476 | */ |
||
477 | public function options( $args, $assoc_args ) { |
||
478 | $action = isset( $args[0] ) ? $args[0] : 'list'; |
||
479 | $safe_to_modify = Jetpack::get_jetpack_options_for_reset(); |
||
480 | |||
481 | // Jumpstart is special |
||
482 | array_push( $safe_to_modify, 'jumpstart' ); |
||
483 | |||
484 | // Is the option flagged as unsafe? |
||
485 | $flagged = ! in_array( $args[1], $safe_to_modify ); |
||
486 | |||
487 | View Code Duplication | if ( ! in_array( $action, array( 'list', 'get', 'delete', 'update' ) ) ) { |
|
488 | /* translators: %s is a command like "prompt" */ |
||
489 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); |
||
490 | } |
||
491 | |||
492 | if ( isset( $args[0] ) ) { |
||
493 | if ( 'get' == $args[0] && isset( $args[1] ) ) { |
||
494 | $action = 'get'; |
||
495 | } else if ( 'delete' == $args[0] && isset( $args[1] ) ) { |
||
496 | $action = 'delete'; |
||
497 | } else if ( 'update' == $args[0] && isset( $args[1] ) ) { |
||
498 | $action = 'update'; |
||
499 | } else { |
||
500 | $action = 'list'; |
||
501 | } |
||
502 | } |
||
503 | |||
504 | // Bail if the option isn't found |
||
505 | $option = isset( $args[1] ) ? Jetpack_Options::get_option( $args[1] ) : false; |
||
506 | View Code Duplication | if ( isset( $args[1] ) && ! $option && 'update' !== $args[0] ) { |
|
507 | WP_CLI::error( __( 'Option not found or is empty. Use "list" to list option names', 'jetpack' ) ); |
||
508 | } |
||
509 | |||
510 | // Let's print_r the option if it's an array |
||
511 | // Used in the 'get' and 'list' actions |
||
512 | $option = is_array( $option ) ? print_r( $option ) : $option; |
||
513 | |||
514 | switch ( $action ) { |
||
515 | case 'get': |
||
516 | WP_CLI::success( "\t" . $option ); |
||
517 | break; |
||
518 | case 'delete': |
||
519 | jetpack_cli_are_you_sure( $flagged ); |
||
520 | |||
521 | Jetpack_Options::delete_option( $args[1] ); |
||
522 | WP_CLI::success( sprintf( __( 'Deleted option: %s', 'jetpack' ), $args[1] ) ); |
||
523 | break; |
||
524 | case 'update': |
||
525 | jetpack_cli_are_you_sure( $flagged ); |
||
526 | |||
527 | // Updating arrays would get pretty tricky... |
||
528 | $value = Jetpack_Options::get_option( $args[1] ); |
||
529 | if ( $value && is_array( $value ) ) { |
||
530 | WP_CLI::error( __( 'Sorry, no updating arrays at this time', 'jetpack' ) ); |
||
531 | } |
||
532 | |||
533 | Jetpack_Options::update_option( $args[1], $args[2] ); |
||
534 | WP_CLI::success( sprintf( _x( 'Updated option: %s to "%s"', 'Updating an option from "this" to "that".', 'jetpack' ), $args[1], $args[2] ) ); |
||
535 | break; |
||
536 | case 'list': |
||
537 | $options_compact = Jetpack_Options::get_option_names(); |
||
538 | $options_non_compact = Jetpack_Options::get_option_names( 'non_compact' ); |
||
539 | $options_private = Jetpack_Options::get_option_names( 'private' ); |
||
540 | $options = array_merge( $options_compact, $options_non_compact, $options_private ); |
||
541 | |||
542 | // Table headers |
||
543 | WP_CLI::line( "\t" . str_pad( __( 'Option', 'jetpack' ), 30 ) . __( 'Value', 'jetpack' ) ); |
||
544 | |||
545 | // List out the options and their values |
||
546 | // Tell them if the value is empty or not |
||
547 | // Tell them if it's an array |
||
548 | foreach ( $options as $option ) { |
||
549 | $value = Jetpack_Options::get_option( $option ); |
||
550 | if ( ! $value ) { |
||
551 | WP_CLI::line( "\t" . str_pad( $option, 30 ) . 'Empty' ); |
||
552 | continue; |
||
553 | } |
||
554 | |||
555 | if ( ! is_array( $value ) ) { |
||
556 | WP_CLI::line( "\t" . str_pad( $option, 30 ) . $value ); |
||
557 | } else if ( is_array( $value ) ) { |
||
558 | WP_CLI::line( "\t" . str_pad( $option, 30 ) . 'Array - Use "get <option>" to read option array.' ); |
||
559 | } |
||
560 | } |
||
561 | $option_text = '{' . _x( 'option', 'a variable command that a user can write, provided in the printed instructions', 'jetpack' ) . '}'; |
||
562 | $value_text = '{' . _x( 'value', 'the value that they want to update the option to', 'jetpack' ) . '}'; |
||
563 | |||
564 | WP_CLI::success( |
||
565 | _x( "Above are your options. You may 'get', 'delete', and 'update' them.", "'get', 'delete', and 'update' are commands - do not translate.", 'jetpack' ) . "\n" . |
||
566 | str_pad( 'wp jetpack options get', 26 ) . $option_text . "\n" . |
||
567 | str_pad( 'wp jetpack options delete', 26 ) . $option_text . "\n" . |
||
568 | str_pad( 'wp jetpack options update', 26 ) . "$option_text $value_text" . "\n" . |
||
569 | _x( "Type 'wp jetpack options' for more info.", "'wp jetpack options' is a command - do not translate.", 'jetpack' ) . "\n" |
||
570 | ); |
||
571 | break; |
||
572 | } |
||
573 | } |
||
574 | |||
575 | /** |
||
576 | * Get the status of or start a new Jetpack sync. |
||
577 | * |
||
578 | * ## OPTIONS |
||
579 | * |
||
580 | * status : Print the current sync status |
||
581 | * start : Start a full sync from this site to WordPress.com |
||
582 | * |
||
583 | * ## EXAMPLES |
||
584 | * |
||
585 | * wp jetpack sync status |
||
586 | * wp jetpack sync start --modules=functions --sync_wait_time=5 |
||
587 | * |
||
588 | * @synopsis <status|start> [--<field>=<value>] |
||
589 | */ |
||
590 | public function sync( $args, $assoc_args ) { |
||
693 | |||
694 | /** |
||
695 | * List the contents of a specific Jetpack sync queue. |
||
696 | * |
||
697 | * ## OPTIONS |
||
698 | * |
||
699 | * peek : List the 100 front-most items on the queue. |
||
700 | * |
||
701 | * ## EXAMPLES |
||
702 | * |
||
703 | * wp jetpack sync_queue full_sync peek |
||
704 | * |
||
705 | * @synopsis <incremental|full_sync> <peek> |
||
706 | */ |
||
707 | public function sync_queue( $args, $assoc_args ) { |
||
758 | } |
||
759 | |||
760 | /* |
||
796 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: