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 ) { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Disconnect Jetpack Blogs or Users |
||
| 91 | * |
||
| 92 | * ## OPTIONS |
||
| 93 | * |
||
| 94 | * blog: Disconnect the entire blog. |
||
| 95 | * |
||
| 96 | * user <user_identifier>: Disconnect a specific user from WordPress.com. |
||
| 97 | * |
||
| 98 | * Please note, the primary account that the blog is connected |
||
| 99 | * to WordPress.com with cannot be disconnected without |
||
| 100 | * disconnecting the entire blog. |
||
| 101 | * |
||
| 102 | * ## EXAMPLES |
||
| 103 | * |
||
| 104 | * wp jetpack disconnect blog |
||
| 105 | * wp jetpack disconnect user 13 |
||
| 106 | * wp jetpack disconnect user username |
||
| 107 | * wp jetpack disconnect user [email protected] |
||
| 108 | * |
||
| 109 | * @synopsis <blog|user> [<user_identifier>] |
||
| 110 | */ |
||
| 111 | public function disconnect( $args, $assoc_args ) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Reset Jetpack options and settings to default |
||
| 164 | * |
||
| 165 | * ## OPTIONS |
||
| 166 | * |
||
| 167 | * modules: Resets modules to default state ( get_default_modules() ) |
||
| 168 | * |
||
| 169 | * options: Resets all Jetpack options except: |
||
| 170 | * - All private options (Blog token, user token, etc...) |
||
| 171 | * - id (The Client ID/WP.com Blog ID of this site) |
||
| 172 | * - master_user |
||
| 173 | * - version |
||
| 174 | * - activated |
||
| 175 | * |
||
| 176 | * ## EXAMPLES |
||
| 177 | * |
||
| 178 | * wp jetpack reset options |
||
| 179 | * wp jetpack reset modules |
||
| 180 | * |
||
| 181 | * @synopsis <modules|options> |
||
| 182 | */ |
||
| 183 | public function reset( $args, $assoc_args ) { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Manage Jetpack Modules |
||
| 238 | * |
||
| 239 | * ## OPTIONS |
||
| 240 | * |
||
| 241 | * list : View all available modules, and their status. |
||
| 242 | * activate all : Activate all modules |
||
| 243 | * deactivate all: Deactivate all modules |
||
| 244 | * |
||
| 245 | * activate <module_slug> : Activate a module. |
||
| 246 | * deactivate <module_slug> : Deactivate a module. |
||
| 247 | * toggle <module_slug> : Toggle a module on or off. |
||
| 248 | * |
||
| 249 | * ## EXAMPLES |
||
| 250 | * |
||
| 251 | * wp jetpack module list |
||
| 252 | * wp jetpack module activate stats |
||
| 253 | * wp jetpack module deactivate stats |
||
| 254 | * wp jetpack module toggle stats |
||
| 255 | * |
||
| 256 | * wp jetpack module activate all |
||
| 257 | * wp jetpack module deactivate all |
||
| 258 | * |
||
| 259 | * @synopsis <list|activate|deactivate|toggle> [<module_name>] |
||
| 260 | */ |
||
| 261 | public function module( $args, $assoc_args ) { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Manage Protect Settings |
||
| 330 | * |
||
| 331 | * ## OPTIONS |
||
| 332 | * |
||
| 333 | * whitelist: Whitelist an IP address. You can also read or clear the whitelist. |
||
| 334 | * |
||
| 335 | * |
||
| 336 | * ## EXAMPLES |
||
| 337 | * |
||
| 338 | * wp jetpack protect whitelist <ip address> |
||
| 339 | * wp jetpack protect whitelist list |
||
| 340 | * wp jetpack protect whitelist clear |
||
| 341 | * |
||
| 342 | * @synopsis <whitelist> [<ip|ip_low-ip_high|list|clear>] |
||
| 343 | */ |
||
| 344 | public function protect( $args, $assoc_args ) { |
||
| 345 | $action = isset( $args[0] ) ? $args[0] : 'prompt'; |
||
| 346 | if ( ! in_array( $action, array( 'whitelist' ) ) ) { |
||
| 347 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); |
||
| 348 | } |
||
| 349 | // Check if module is active |
||
| 350 | if ( ! Jetpack::is_module_active( __FUNCTION__ ) ) { |
||
| 351 | WP_CLI::error( sprintf( _x( '%s is not active. You can activate it with "wp jetpack module activate %s"', '"wp jetpack module activate" is a command - do not translate', 'jetpack' ), __FUNCTION__, __FUNCTION__ ) ); |
||
| 352 | } |
||
| 353 | if ( in_array( $action, array( 'whitelist' ) ) ) { |
||
| 354 | if ( isset( $args[1] ) ) { |
||
| 355 | $action = 'whitelist'; |
||
| 356 | } else { |
||
| 357 | $action = 'prompt'; |
||
| 358 | } |
||
| 359 | } |
||
| 360 | switch ( $action ) { |
||
| 361 | case 'whitelist': |
||
| 362 | $whitelist = array(); |
||
| 363 | $new_ip = $args[1]; |
||
| 364 | $current_whitelist = get_site_option( 'jetpack_protect_whitelist' ); |
||
| 365 | |||
| 366 | // Build array of IPs that are already whitelisted. |
||
| 367 | // Re-build manually instead of using jetpack_protect_format_whitelist() so we can easily get |
||
| 368 | // low & high range params for jetpack_protect_ip_address_is_in_range(); |
||
| 369 | foreach( $current_whitelist as $whitelisted ) { |
||
| 370 | |||
| 371 | // IP ranges |
||
| 372 | if ( $whitelisted->range ) { |
||
| 373 | |||
| 374 | // Is it already whitelisted? |
||
| 375 | if ( jetpack_protect_ip_address_is_in_range( $new_ip, $whitelisted->range_low, $whitelisted->range_high ) ) { |
||
| 376 | WP_CLI::error( sprintf( __( "%s has already been whitelisted", 'jetpack' ), $new_ip ) ); |
||
| 377 | break; |
||
| 378 | } |
||
| 379 | $whitelist[] = $whitelisted->range_low . " - " . $whitelisted->range_high; |
||
| 380 | |||
| 381 | } else { // Individual IPs |
||
| 382 | |||
| 383 | // Check if the IP is already whitelisted (single IP only) |
||
| 384 | if ( $new_ip == $whitelisted->ip_address ) { |
||
| 385 | WP_CLI::error( sprintf( __( "%s has already been whitelisted", 'jetpack' ), $new_ip ) ); |
||
| 386 | break; |
||
| 387 | } |
||
| 388 | $whitelist[] = $whitelisted->ip_address; |
||
| 389 | |||
| 390 | } |
||
| 391 | } |
||
| 392 | |||
| 393 | /* |
||
| 394 | * List the whitelist |
||
| 395 | * Done here because it's easier to read the $whitelist array after it's been rebuilt |
||
| 396 | */ |
||
| 397 | if ( isset( $args[1] ) && 'list' == $args[1] ) { |
||
| 398 | if ( ! empty( $whitelist ) ) { |
||
| 399 | WP_CLI::success( __( 'Here are your whitelisted IPs:', 'jetpack' ) ); |
||
| 400 | foreach ( $whitelist as $ip ) { |
||
| 401 | WP_CLI::line( "\t" . str_pad( $ip, 24 ) ) ; |
||
| 402 | } |
||
| 403 | } else { |
||
| 404 | WP_CLI::line( __( 'Whitelist is empty.', "jetpack" ) ) ; |
||
| 405 | } |
||
| 406 | break; |
||
| 407 | } |
||
| 408 | |||
| 409 | /* |
||
| 410 | * Clear the whitelist |
||
| 411 | */ |
||
| 412 | if ( isset( $args[1] ) && 'clear' == $args[1] ) { |
||
| 413 | View Code Duplication | if ( ! empty( $whitelist ) ) { |
|
| 414 | $whitelist = array(); |
||
| 415 | jetpack_protect_save_whitelist( $whitelist ); |
||
| 416 | WP_CLI::success( __( 'Cleared all whitelisted IPs', 'jetpack' ) ); |
||
| 417 | } else { |
||
| 418 | WP_CLI::line( __( 'Whitelist is empty.', "jetpack" ) ) ; |
||
| 419 | } |
||
| 420 | break; |
||
| 421 | } |
||
| 422 | |||
| 423 | // Append new IP to whitelist array |
||
| 424 | array_push( $whitelist, $new_ip ); |
||
| 425 | |||
| 426 | // Save whitelist if there are no errors |
||
| 427 | $result = jetpack_protect_save_whitelist( $whitelist ); |
||
| 428 | if ( is_wp_error( $result ) ) { |
||
| 429 | WP_CLI::error( __( $result, 'jetpack' ) ); |
||
| 430 | } |
||
| 431 | |||
| 432 | WP_CLI::success( sprintf( __( '%s has been whitelisted.', 'jetpack' ), $new_ip ) ); |
||
| 433 | break; |
||
| 434 | case 'prompt': |
||
| 435 | WP_CLI::error( |
||
| 436 | __( 'No command found.', 'jetpack' ) . "\n" . |
||
| 437 | __( 'Please enter the IP address you want to whitelist.', 'jetpack' ) . "\n" . |
||
| 438 | _x( 'You can save a range of IPs {low_range}-{high_range}. No spaces allowed. (example: 1.1.1.1-2.2.2.2)', 'Instructions on how to whitelist IP ranges - low_range/high_range should be translated.', 'jetpack' ) . "\n" . |
||
| 439 | _x( "You can also 'list' or 'clear' the whitelist.", "'list' and 'clear' are commands and should not be translated", 'jetpack' ) . "\n" |
||
| 440 | ); |
||
| 441 | break; |
||
| 442 | } |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Manage Jetpack Options |
||
| 447 | * |
||
| 448 | * ## OPTIONS |
||
| 449 | * |
||
| 450 | * list : List all jetpack options and their values |
||
| 451 | * delete : Delete an option |
||
| 452 | * - can only delete options that are white listed. |
||
| 453 | * update : update an option |
||
| 454 | * - can only update option strings |
||
| 455 | * get : get the value of an option |
||
| 456 | * |
||
| 457 | * ## EXAMPLES |
||
| 458 | * |
||
| 459 | * wp jetpack options list |
||
| 460 | * wp jetpack options get <option_name> |
||
| 461 | * wp jetpack options delete <option_name> |
||
| 462 | * wp jetpack options update <option_name> [<option_value>] |
||
| 463 | * |
||
| 464 | * @synopsis <list|get|delete|update> [<option_name>] [<option_value>] |
||
| 465 | */ |
||
| 466 | public function options( $args, $assoc_args ) { |
||
| 562 | } |
||
| 563 | |||
| 599 | } |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: