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 | const 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 | * A {@link Wordlift_Log_Service} instance. |
||
| 163 | * |
||
| 164 | * @since 3.14.2 |
||
| 165 | * @access private |
||
| 166 | * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
||
| 167 | */ |
||
| 168 | private $log; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * The {@link Class_Wordlift_Batch_Analys_Service} instance. |
||
| 172 | * |
||
| 173 | * @since 3.14.0 |
||
| 174 | * |
||
| 175 | * @param \Wordlift $plugin The {@link Wordlift} plugin instance. |
||
| 176 | * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
||
| 177 | */ |
||
| 178 | public function __construct( $plugin, $configuration_service ) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Submit posts for Batch Analysis. |
||
| 197 | * |
||
| 198 | * @since 3.14.2 |
||
| 199 | * |
||
| 200 | * @param array $args { |
||
| 201 | * A list of options for the Batch Analysis. |
||
| 202 | * |
||
| 203 | * @type string $link Either `default`, `no` or `yes` (`default` is used if not specified): |
||
| 204 | * * `default` doesn't set the link option - entities |
||
| 205 | * will be linked if configured so in WordLift settings. |
||
| 206 | * * `yes` links the entities. |
||
| 207 | * * `no` doesn't link the entities. |
||
| 208 | * This value is forwarded to WLS' Batch Analysis end-point. |
||
| 209 | * @type int $min_occurrences The minimum number of occurrences to select |
||
| 210 | * an entity. Default `1`. |
||
| 211 | * @type bool $include_annotated Whether to include annotated posts in selection. |
||
| 212 | * Default `false`. |
||
| 213 | * @type array|int $include Explicitly include the specified {@link WP_Post}s. |
||
| 214 | * @type array|int $exclude Explicitly exclude the specified {@link WP_Post}s. |
||
| 215 | * @type string|null $from An optional date from filter (used in `post_date_gmt`). |
||
| 216 | * @type string|null $to An optional date from filter (used in `post_date_gmt`). |
||
| 217 | * @type array|string $post_type Specify the post type(s), by default only `post`. |
||
| 218 | * } |
||
| 219 | * |
||
| 220 | * @return int The number of submitted {@link WP_Post}s or false on error. |
||
| 221 | */ |
||
| 222 | public function submit( $args ) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Submit one or more {@link WP_Posts} for Batch Analysis. |
||
| 259 | * |
||
| 260 | * @param array $args { |
||
| 261 | * An array of arguments. |
||
| 262 | * |
||
| 263 | * @type string $link The link option: `default`, `yes` or |
||
| 264 | * `no`. If not set `default`. |
||
| 265 | * @type int $min_occurrences The minimum number of occurrences. If |
||
| 266 | * not set `1`. |
||
| 267 | * @type array|int $ids An array of {@link WP_Post}s' ids or one |
||
| 268 | * single numeric {@link WP_Post} id. |
||
| 269 | * } |
||
| 270 | * |
||
| 271 | * @return float|int |
||
| 272 | */ |
||
| 273 | public function submit_posts( $args ) { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Cancel the Batch Analysis request for the specified {@link WP_Post}s. |
||
| 302 | * |
||
| 303 | * @since 3.14.2 |
||
| 304 | * |
||
| 305 | * @param int|array $post_ids A single {@link WP_Post}'s id or an array of |
||
| 306 | * {@link WP_Post}s' ids. |
||
| 307 | * |
||
| 308 | * @return false|int The number of cancelled {@link WP_Post}s or false on |
||
| 309 | * error. |
||
| 310 | */ |
||
| 311 | public function cancel( $post_ids ) { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Request the batch analysis for submitted posts. |
||
| 330 | * |
||
| 331 | * @since 3.14.2 |
||
| 332 | */ |
||
| 333 | public function request() { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Get the results for the Batch Analysis. |
||
| 380 | * |
||
| 381 | * @since 3.14.2 |
||
| 382 | */ |
||
| 383 | public function complete() { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Set a warning flag on the {@link WP_Post} if its content has suspicious |
||
| 472 | * interpolations. |
||
| 473 | * |
||
| 474 | * @since 3.14.2 |
||
| 475 | * |
||
| 476 | * @param string $content The {@link WP_Post}'s content. |
||
| 477 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 478 | * |
||
| 479 | * @return string The content (for chaining operations). |
||
| 480 | */ |
||
| 481 | protected function set_warning_based_on_content( $content, $post_id ) { |
||
| 490 | |||
| 491 | private function has_interpolation_errors( $content ) { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Fix interpolation errors raised by Batch Analysis |
||
| 500 | * |
||
| 501 | * @param string $content The {@link WP_Post}'s content. |
||
| 502 | * @param int $id The {@link WP_Post}'s id. |
||
| 503 | * |
||
| 504 | * @since 3.17.0 |
||
| 505 | * |
||
| 506 | * @return string Post content without interpolations. |
||
| 507 | */ |
||
| 508 | private function fix_interpolation_errors( $content, $id ) { |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Clear the warning flag for the specified {@link WP_Post}s. |
||
| 523 | * |
||
| 524 | * @since 3.14.2 |
||
| 525 | * |
||
| 526 | * @param int|array $post_ids A single {@link WP_Post}'s id or an array of |
||
| 527 | * {@link WP_Post}s' ids. |
||
| 528 | */ |
||
| 529 | public function clear_warning( $post_ids ) { |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Set the warning flag for the specified {@link WP_Post}. |
||
| 539 | * |
||
| 540 | * @since 3.14.2 |
||
| 541 | * |
||
| 542 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 543 | * @param bool $value The flag's value. |
||
| 544 | * |
||
| 545 | * @return int|bool Meta ID if the key didn't exist, true on successful update, |
||
| 546 | * false on failure. |
||
| 547 | */ |
||
| 548 | private function set_warning( $post_id, $value ) { |
||
| 552 | |||
| 553 | // /** |
||
| 554 | // * Get the post/page batch analysis state. |
||
| 555 | // * |
||
| 556 | // * @since 3.14.2 |
||
| 557 | // * |
||
| 558 | // * @param int $post_id The {@link WP_Post}'s id. |
||
| 559 | // * |
||
| 560 | // * @return int|string The post state or an empty string if not set. |
||
| 561 | // */ |
||
| 562 | // public function get_state( $post_id ) { |
||
| 563 | // |
||
| 564 | // return get_post_meta( $post_id, self::STATE_META_KEY, true ); |
||
| 565 | // } |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Set the post/page batch analysis state. |
||
| 569 | * |
||
| 570 | * @since 3.14.2 |
||
| 571 | * |
||
| 572 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 573 | * @param int $value The new state. |
||
| 574 | * |
||
| 575 | * @return int|bool Meta ID if the key didn't exist, true on successful update, |
||
| 576 | * false on failure. |
||
| 577 | */ |
||
| 578 | private function set_state( $post_id, $value ) { |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Get the options setting for a {@link WP_Post}. |
||
| 604 | * |
||
| 605 | * If there are multiple link settings, only the last one is returned. |
||
| 606 | * |
||
| 607 | * @since 3.14.2 |
||
| 608 | * |
||
| 609 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 610 | * |
||
| 611 | * @return array The link settings. |
||
| 612 | */ |
||
| 613 | private function get_options( $post_id ) { |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Get the array of post IDS waiting in the queue to start processing. |
||
| 625 | * |
||
| 626 | * @since 3.14.0 |
||
| 627 | * |
||
| 628 | * @return array The waiting to be processed post ids queue. |
||
| 629 | */ |
||
| 630 | View Code Duplication | public function waiting_for_analysis() { |
|
| 643 | |||
| 644 | /** |
||
| 645 | * Get the array of post IDS waiting for response. |
||
| 646 | * |
||
| 647 | * @deprecated |
||
| 648 | * @since 3.14.0 |
||
| 649 | * |
||
| 650 | * @return array The waiting for response post ids queue. |
||
| 651 | */ |
||
| 652 | View Code Duplication | public function waiting_for_response() { |
|
| 665 | |||
| 666 | /** |
||
| 667 | * Request the analysis for the specified {@link WP_Post}. |
||
| 668 | * |
||
| 669 | * @since 3.14.2 |
||
| 670 | * |
||
| 671 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 672 | * |
||
| 673 | * @return WP_Error|array The response or WP_Error on failure. |
||
| 674 | */ |
||
| 675 | private function do_request( $post_id ) { |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Get the Batch Analysis results for the specified {@link WP_Post}. |
||
| 730 | * |
||
| 731 | * @since 3.14.2 |
||
| 732 | * |
||
| 733 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 734 | * |
||
| 735 | * @return WP_Error|array The response or WP_Error on failure. |
||
| 736 | */ |
||
| 737 | private function do_complete( $post_id ) { |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Get the {@link WP_Post}s' ids flagged with warnings. |
||
| 755 | * |
||
| 756 | * @since 3.14.2 |
||
| 757 | * |
||
| 758 | * @return array An array of {@link WP_Post}s' ids. |
||
| 759 | */ |
||
| 760 | View Code Duplication | public function get_warnings() { |
|
| 772 | |||
| 773 | /** |
||
| 774 | * Check whether the term has entity type associated and set default term if it hasn't. |
||
| 775 | * |
||
| 776 | * @since 3.17.0 |
||
| 777 | * |
||
| 778 | * @param int $id The {@link WP_Post}'s id. |
||
| 779 | */ |
||
| 780 | private function maybe_set_default_term( $id ) { |
||
| 793 | |||
| 794 | } |
||
| 795 |
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