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 Wordlift_Batch_Analysis_Service 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 Wordlift_Batch_Analysis_Service, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 52 | class Wordlift_Batch_Analysis_Service { |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The list of states for the Batch Analysis: |
||
| 56 | * - STATE_META_KEY: the batch analysis state meta key, |
||
| 57 | * - STATE_SUBMIT: a post/page has been submitted for analysis, |
||
| 58 | * - STATE_REQUEST: the plugin requested an analysis for the submitted |
||
| 59 | * post/page, |
||
| 60 | * - STATE_SUCCESS: the analysis has completed successfully, |
||
| 61 | * - STATE_ERROR: the analysis returned an error. |
||
| 62 | * |
||
| 63 | * @since 3.14.2 |
||
| 64 | */ |
||
| 65 | const STATE_META_KEY = '_wl_batch_analysis_state'; |
||
| 66 | const STATE_SUBMIT = 0; |
||
| 67 | const STATE_REQUEST = 1; |
||
| 68 | // ### COMPLETE states. |
||
| 69 | const STATE_SUCCESS = 2; |
||
| 70 | const STATE_ERROR = 2; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * The submit timestamp meta key. A post may have more than one timestamp. |
||
| 74 | * |
||
| 75 | * @since 3.14.2 |
||
| 76 | */ |
||
| 77 | const SUBMIT_TIMESTAMP_META_KEY = '_wl_batch_analysis_submit_timestamp'; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * The request timestamp meta key. A post may have more than one timestamp. |
||
| 81 | * |
||
| 82 | * @since 3.14.2 |
||
| 83 | */ |
||
| 84 | const REQUEST_TIMESTAMP_META_KEY = '_wl_batch_analysis_request_timestamp'; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The complete (success or error) timestamp meta key. A post may have more |
||
| 88 | * than one timestamp. |
||
| 89 | * |
||
| 90 | * @since 3.14.2 |
||
| 91 | */ |
||
| 92 | const COMPLETE_TIMESTAMP_META_KEY = '_wl_batch_analysis_complete_timestamp'; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The options setting meta key. A post may have more than one setting. |
||
| 96 | * |
||
| 97 | * @since 3.14.2 |
||
| 98 | */ |
||
| 99 | const BATCH_ANALYSIS_OPTIONS_META_KEY = '_wl_batch_analysis_options'; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * The warning timestamp meta key. A post has only zero/one value. |
||
| 103 | * |
||
| 104 | * @since 3.14.2 |
||
| 105 | */ |
||
| 106 | const WARNING_META_KEY = '_wl_batch_analysis_warning'; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Option name. |
||
| 110 | * |
||
| 111 | * @since 3.14.0 |
||
| 112 | */ |
||
| 113 | const OPTION_NAME = 'wl_analyze_batch'; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Name of waiting to be processed queue array inside the option. |
||
| 117 | * |
||
| 118 | * @since 3.14.0 |
||
| 119 | */ |
||
| 120 | const ANALYZE_QUEUE = 'queue'; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Name of waiting for response queue array inside the option. |
||
| 124 | * |
||
| 125 | * @since 3.14.0 |
||
| 126 | */ |
||
| 127 | const RESPONSE_QUEUE = 'processing'; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Regular expressions that match interpolation errors. |
||
| 131 | * |
||
| 132 | * @since 3.17.0 |
||
| 133 | */ |
||
| 134 | private static $interpolation_patterns = array( |
||
| 135 | // Matches word before the annotation. |
||
| 136 | '~(\w)<[a-z]+ id="urn:[^"]+" class="[^"]+" itemid="[^"]+">(.*?)<\/[a-z]+>~', |
||
| 137 | // Matches word after the annotation. |
||
| 138 | '~<[a-z]+ id="urn:[^"]+" class="[^"]+" itemid="[^"]+">(.*?)<\/[a-z]+>(\w)~', |
||
| 139 | // Matches space in the beginning of annotation name. |
||
| 140 | '~<[a-z]+ id="urn:[^"]+" class="[^"]+" itemid="[^"]+">(\s)(.*?)<\/[a-z]+>~', |
||
| 141 | ); |
||
| 142 | |||
| 143 | /** |
||
| 144 | * The {@link Wordlift} plugin instance. |
||
| 145 | * |
||
| 146 | * @since 3.14.0 |
||
| 147 | * @access private |
||
| 148 | * @var \Wordlift $plugin The {@link Wordlift} plugin instance. |
||
| 149 | */ |
||
| 150 | private $plugin; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * The {@link Wordlift_Configuration_Service} instance. |
||
| 154 | * |
||
| 155 | * @since 3.14.0 |
||
| 156 | * @access private |
||
| 157 | * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
||
| 158 | */ |
||
| 159 | private $configuration_service; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * The {@link Wordlift_Cache_Service} instance. |
||
| 163 | * |
||
| 164 | * @since 3.17.0 |
||
| 165 | * @access protected |
||
| 166 | * @var \Wordlift_Cache_Service $cache_service The {@link Wordlift_Cache_Service} instance. |
||
| 167 | */ |
||
| 168 | private $cache_service; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * A {@link Wordlift_Log_Service} instance. |
||
| 172 | * |
||
| 173 | * @since 3.14.2 |
||
| 174 | * @access private |
||
| 175 | * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
||
| 176 | */ |
||
| 177 | private $log; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * The {@link Class_Wordlift_Batch_Analys_Service} instance. |
||
| 181 | * |
||
| 182 | * @since 3.14.0 |
||
| 183 | * |
||
| 184 | * @param \Wordlift $plugin The {@link Wordlift} plugin instance. |
||
| 185 | * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
||
| 186 | * @param \Wordlift_Cache_Service $cache_service The {@link Wordlift_Cache_Service} instance. |
||
| 187 | */ |
||
| 188 | public function __construct( $plugin, $configuration_service, $cache_service ) { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Submit posts for Batch Analysis. |
||
| 209 | * |
||
| 210 | * @since 3.14.2 |
||
| 211 | * |
||
| 212 | * @param array $args { |
||
| 213 | * A list of options for the Batch Analysis. |
||
| 214 | * |
||
| 215 | * @type string $link Either `default`, `no` or `yes` (`default` is used if not specified): |
||
| 216 | * * `default` doesn't set the link option - entities |
||
| 217 | * will be linked if configured so in WordLift settings. |
||
| 218 | * * `yes` links the entities. |
||
| 219 | * * `no` doesn't link the entities. |
||
| 220 | * This value is forwarded to WLS' Batch Analysis end-point. |
||
| 221 | * @type int $min_occurrences The minimum number of occurrences to select |
||
| 222 | * an entity. Default `1`. |
||
| 223 | * @type bool $include_annotated Whether to include annotated posts in selection. |
||
| 224 | * Default `false`. |
||
| 225 | * @type array|int $include Explicitly include the specified {@link WP_Post}s. |
||
| 226 | * @type array|int $exclude Explicitly exclude the specified {@link WP_Post}s. |
||
| 227 | * @type string|null $from An optional date from filter (used in `post_date_gmt`). |
||
| 228 | * @type string|null $to An optional date from filter (used in `post_date_gmt`). |
||
| 229 | * @type array|string $post_type Specify the post type(s), by default only `post`. |
||
| 230 | * } |
||
| 231 | * |
||
| 232 | * @return int The number of submitted {@link WP_Post}s or false on error. |
||
| 233 | */ |
||
| 234 | public function submit( $args ) { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Submit one or more {@link WP_Posts} for Batch Analysis. |
||
| 270 | * |
||
| 271 | * @param array $args { |
||
| 272 | * An array of arguments. |
||
| 273 | * |
||
| 274 | * @type string $link The link option: `default`, `yes` or |
||
| 275 | * `no`. If not set `default`. |
||
| 276 | * @type int $min_occurrences The minimum number of occurrences. If |
||
| 277 | * not set `1`. |
||
| 278 | * @type array|int $ids An array of {@link WP_Post}s' ids or one |
||
| 279 | * single numeric {@link WP_Post} id. |
||
| 280 | * } |
||
| 281 | * |
||
| 282 | * @return float|int |
||
| 283 | */ |
||
| 284 | public function submit_posts( $args ) { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Add metas to the posts that should be analysed. |
||
| 312 | * |
||
| 313 | * @param array $params The request params. |
||
| 314 | * @param string $query The mysql query. |
||
| 315 | * |
||
| 316 | * @since 3.18.0 |
||
| 317 | * |
||
| 318 | * @return int The number of posts found/submitted. |
||
| 319 | */ |
||
| 320 | public function update_posts_meta( $params, $query ) { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Cancel the Batch Analysis request for the specified {@link WP_Post}s. |
||
| 350 | * |
||
| 351 | * @since 3.14.2 |
||
| 352 | * |
||
| 353 | * @param int|array $post_ids A single {@link WP_Post}'s id or an array of |
||
| 354 | * {@link WP_Post}s' ids. |
||
| 355 | * |
||
| 356 | * @return false|int The number of cancelled {@link WP_Post}s or false on |
||
| 357 | * error. |
||
| 358 | */ |
||
| 359 | public function cancel( $post_ids ) { |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Request the batch analysis for submitted posts. |
||
| 378 | * |
||
| 379 | * @since 3.14.2 |
||
| 380 | */ |
||
| 381 | public function request() { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Get the results for the Batch Analysis. |
||
| 428 | * |
||
| 429 | * @since 3.14.2 |
||
| 430 | */ |
||
| 431 | public function complete() { |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Set a warning flag on the {@link WP_Post} if its content has suspicious |
||
| 523 | * interpolations. |
||
| 524 | * |
||
| 525 | * @since 3.14.2 |
||
| 526 | * |
||
| 527 | * @param string $content The {@link WP_Post}'s content. |
||
| 528 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 529 | */ |
||
| 530 | protected function set_warning_based_on_content( $content, $post_id ) { |
||
| 539 | |||
| 540 | private function has_interpolation_errors( $content ) { |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Fix interpolation errors raised by Batch Analysis |
||
| 552 | * |
||
| 553 | * @param string $content The {@link WP_Post}'s content. |
||
| 554 | * @param int $id The {@link WP_Post}'s id. |
||
| 555 | * |
||
| 556 | * @since 3.17.0 |
||
| 557 | * |
||
| 558 | * @return string Post content without interpolations. |
||
| 559 | */ |
||
| 560 | private function fix_interpolation_errors( $content, $id ) { |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Clear the warning flag for the specified {@link WP_Post}s. |
||
| 577 | * |
||
| 578 | * @since 3.14.2 |
||
| 579 | * |
||
| 580 | * @param int|array $post_ids A single {@link WP_Post}'s id or an array of |
||
| 581 | * {@link WP_Post}s' ids. |
||
| 582 | */ |
||
| 583 | public function clear_warning( $post_ids ) { |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Set the warning flag for the specified {@link WP_Post}. |
||
| 593 | * |
||
| 594 | * @since 3.14.2 |
||
| 595 | * |
||
| 596 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 597 | * @param bool $value The flag's value. |
||
| 598 | * |
||
| 599 | * @return int|bool Meta ID if the key didn't exist, true on successful update, |
||
| 600 | * false on failure. |
||
| 601 | */ |
||
| 602 | private function set_warning( $post_id, $value ) { |
||
| 606 | |||
| 607 | // /** |
||
| 608 | // * Get the post/page batch analysis state. |
||
| 609 | // * |
||
| 610 | // * @since 3.14.2 |
||
| 611 | // * |
||
| 612 | // * @param int $post_id The {@link WP_Post}'s id. |
||
| 613 | // * |
||
| 614 | // * @return int|string The post state or an empty string if not set. |
||
| 615 | // */ |
||
| 616 | // public function get_state( $post_id ) { |
||
| 617 | // |
||
| 618 | // return get_post_meta( $post_id, self::STATE_META_KEY, true ); |
||
| 619 | // } |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Set the post/page batch analysis state. |
||
| 623 | * |
||
| 624 | * @since 3.14.2 |
||
| 625 | * |
||
| 626 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 627 | * @param int $value The new state. |
||
| 628 | * |
||
| 629 | * @return int|bool Meta ID if the key didn't exist, true on successful update, |
||
| 630 | * false on failure. |
||
| 631 | */ |
||
| 632 | private function set_state( $post_id, $value ) { |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Get the options setting for a {@link WP_Post}. |
||
| 658 | * |
||
| 659 | * If there are multiple link settings, only the last one is returned. |
||
| 660 | * |
||
| 661 | * @since 3.14.2 |
||
| 662 | * |
||
| 663 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 664 | * |
||
| 665 | * @return array The link settings. |
||
| 666 | */ |
||
| 667 | private function get_options( $post_id ) { |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Get the array of post IDS waiting in the queue to start processing. |
||
| 679 | * |
||
| 680 | * @since 3.14.0 |
||
| 681 | * |
||
| 682 | * @return array The waiting to be processed post ids queue. |
||
| 683 | */ |
||
| 684 | View Code Duplication | public function waiting_for_analysis() { |
|
| 697 | |||
| 698 | /** |
||
| 699 | * Get the array of post IDS waiting for response. |
||
| 700 | * |
||
| 701 | * @deprecated |
||
| 702 | * @since 3.14.0 |
||
| 703 | * |
||
| 704 | * @return array The waiting for response post ids queue. |
||
| 705 | */ |
||
| 706 | View Code Duplication | public function waiting_for_response() { |
|
| 719 | |||
| 720 | /** |
||
| 721 | * Request the analysis for the specified {@link WP_Post}. |
||
| 722 | * |
||
| 723 | * @since 3.14.2 |
||
| 724 | * |
||
| 725 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 726 | * |
||
| 727 | * @return WP_Error|array The response or WP_Error on failure. |
||
| 728 | */ |
||
| 729 | private function do_request( $post_id ) { |
||
| 781 | |||
| 782 | /** |
||
| 783 | * Get the Batch Analysis results for the specified {@link WP_Post}. |
||
| 784 | * |
||
| 785 | * @since 3.14.2 |
||
| 786 | * |
||
| 787 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 788 | * |
||
| 789 | * @return WP_Error|array The response or WP_Error on failure. |
||
| 790 | */ |
||
| 791 | private function do_complete( $post_id ) { |
||
| 806 | |||
| 807 | /** |
||
| 808 | * Get the {@link WP_Post}s' ids flagged with warnings. |
||
| 809 | * |
||
| 810 | * @since 3.14.2 |
||
| 811 | * |
||
| 812 | * @return array An array of {@link WP_Post}s' ids. |
||
| 813 | */ |
||
| 814 | View Code Duplication | public function get_warnings() { |
|
| 826 | |||
| 827 | /** |
||
| 828 | * Check whether the term has entity type associated and set default term if it hasn't. |
||
| 829 | * |
||
| 830 | * @since 3.17.0 |
||
| 831 | * |
||
| 832 | * @param int $id The {@link WP_Post}'s id. |
||
| 833 | */ |
||
| 834 | private function maybe_set_default_term( $id ) { |
||
| 847 | |||
| 848 | } |
||
| 849 |
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state