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:
| 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 link setting meta key. A post may have more than one setting. |
||
| 96 | * |
||
| 97 | * @since 3.14.2 |
||
| 98 | */ |
||
| 99 | const LINK_META_KEY = '_wl_batch_analysis_link'; |
||
| 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 | * The {@link Wordlift} plugin instance. |
||
| 131 | * |
||
| 132 | * @since 3.14.0 |
||
| 133 | * @access private |
||
| 134 | * @var \Wordlift $plugin The {@link Wordlift} plugin instance. |
||
| 135 | */ |
||
| 136 | private $plugin; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * The {@link Wordlift_Configuration_Service} instance. |
||
| 140 | * |
||
| 141 | * @since 3.14.0 |
||
| 142 | * @access private |
||
| 143 | * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
||
| 144 | */ |
||
| 145 | private $configuration_service; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * A {@link Wordlift_Log_Service} instance. |
||
| 149 | * |
||
| 150 | * @since 3.14.2 |
||
| 151 | * @access private |
||
| 152 | * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
||
| 153 | */ |
||
| 154 | private $log; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * The {@link Class_Wordlift_Batch_Analys_Service} instance. |
||
| 158 | * |
||
| 159 | * @since 3.14.0 |
||
| 160 | * |
||
| 161 | * @param \Wordlift $plugin The {@link Wordlift} plugin instance. |
||
| 162 | * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
||
| 163 | */ |
||
| 164 | public function __construct( $plugin, $configuration_service ) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get the base SQL statement to submit a post for Batch Analysis. |
||
| 183 | * |
||
| 184 | * Functions may use this base SQL and add their own filters. |
||
| 185 | * |
||
| 186 | * @since 3.14.2 |
||
| 187 | * |
||
| 188 | * @param string $link The link setting ('yes'/'no'). |
||
| 189 | * |
||
| 190 | * @return string The base SQL. |
||
| 191 | */ |
||
| 192 | private function get_sql( $link ) { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Submit for analysis all the posts/pages which do not have annotations |
||
| 231 | * and haven't been analyzed before. |
||
| 232 | * |
||
| 233 | * @since 3.14.2 |
||
| 234 | * |
||
| 235 | * @param string $link The link setting. |
||
| 236 | * |
||
| 237 | * @return false|int The number of submitted {@link WP_Post}s or false on |
||
| 238 | * error. |
||
| 239 | */ |
||
| 240 | public function submit_auto_selected_posts( $link ) { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Submit the provided list of {@link WP_Post}s' ids for Batch Analysis. |
||
| 264 | * |
||
| 265 | * @since 3.14.2 |
||
| 266 | * |
||
| 267 | * @param int|array $post_ids A single {@link WP_Post}'s id or an array of |
||
| 268 | * {@link WP_Post}s' ids. |
||
| 269 | * @param string $link The link setting. |
||
| 270 | * |
||
| 271 | * @return int The number of submitted {@link WP_Post}s or false on error. |
||
| 272 | */ |
||
| 273 | public function submit( $post_ids, $link ) { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Cancel the Batch Analysis request for the specified {@link WP_Post}s. |
||
| 293 | * |
||
| 294 | * @since 3.14.2 |
||
| 295 | * |
||
| 296 | * @param int|array $post_ids A single {@link WP_Post}'s id or an array of |
||
| 297 | * {@link WP_Post}s' ids. |
||
| 298 | * |
||
| 299 | * @return false|int The number of cancelled {@link WP_Post}s or false on |
||
| 300 | * error. |
||
| 301 | */ |
||
| 302 | public function cancel( $post_ids ) { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Request the batch analysis for submitted posts. |
||
| 320 | * |
||
| 321 | * @since 3.14.2 |
||
| 322 | */ |
||
| 323 | public function request() { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Get the results for the Batch Analysis. |
||
| 373 | * |
||
| 374 | * @since 3.14.2 |
||
| 375 | */ |
||
| 376 | public function complete() { |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Set a warning flag on the {@link WP_Post} if its content has suspicious |
||
| 447 | * interpolations. |
||
| 448 | * |
||
| 449 | * @since 3.14.2 |
||
| 450 | * |
||
| 451 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 452 | * @param string $content The {@link WP_Post}'s content. |
||
| 453 | * |
||
| 454 | * @return string The content (for chaining operations). |
||
| 455 | */ |
||
| 456 | private function set_warning_based_on_content( $post_id, $content ) { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Clear the warning flag for the specified {@link WP_Post}s. |
||
| 472 | * |
||
| 473 | * @since 3.14.2 |
||
| 474 | * |
||
| 475 | * @param int|array $post_ids A single {@link WP_Post}'s id or an array of |
||
| 476 | * {@link WP_Post}s' ids. |
||
| 477 | */ |
||
| 478 | public function clear_warning( $post_ids ) { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Set the warning flag for the specified {@link WP_Post}. |
||
| 488 | * |
||
| 489 | * @since 3.14.2 |
||
| 490 | * |
||
| 491 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 492 | * @param bool $value The flag's value. |
||
| 493 | * |
||
| 494 | * @return int|bool Meta ID if the key didn't exist, true on successful update, |
||
| 495 | * false on failure. |
||
| 496 | */ |
||
| 497 | private function set_warning( $post_id, $value ) { |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Get the post/page batch analysis state. |
||
| 504 | * |
||
| 505 | * @since 3.14.2 |
||
| 506 | * |
||
| 507 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 508 | * |
||
| 509 | * @return int|string The post state or an empty string if not set. |
||
| 510 | */ |
||
| 511 | public function get_state( $post_id ) { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Set the post/page batch analysis state. |
||
| 518 | * |
||
| 519 | * @since 3.14.2 |
||
| 520 | * |
||
| 521 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 522 | * @param int $value The new state. |
||
| 523 | * |
||
| 524 | * @return int|bool Meta ID if the key didn't exist, true on successful update, |
||
| 525 | * false on failure. |
||
| 526 | */ |
||
| 527 | public function set_state( $post_id, $value ) { |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Get the link setting for a {@link WP_Post}. |
||
| 553 | * |
||
| 554 | * If there are multiple link settings, only the last one is returned. |
||
| 555 | * |
||
| 556 | * @since 3.14.2 |
||
| 557 | * |
||
| 558 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 559 | * |
||
| 560 | * @return string The link setting or `default` if not set. |
||
| 561 | */ |
||
| 562 | public function get_link( $post_id ) { |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Get the array of post IDS waiting in the queue to start processing. |
||
| 571 | * |
||
| 572 | * @since 3.14.0 |
||
| 573 | * |
||
| 574 | * @return array The waiting to be processed post ids queue. |
||
| 575 | */ |
||
| 576 | View Code Duplication | public function waiting_for_analysis() { |
|
| 587 | |||
| 588 | /** |
||
| 589 | * Get the array of post IDS waiting for response. |
||
| 590 | * |
||
| 591 | * @deprecated |
||
| 592 | * @since 3.14.0 |
||
| 593 | * |
||
| 594 | * @return array The waiting for response post ids queue. |
||
| 595 | */ |
||
| 596 | View Code Duplication | public function waiting_for_response() { |
|
| 607 | |||
| 608 | /** |
||
| 609 | * Request the analysis for the specified {@link WP_Post}. |
||
| 610 | * |
||
| 611 | * @since 3.14.2 |
||
| 612 | * |
||
| 613 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 614 | * |
||
| 615 | * @return WP_Error|array The response or WP_Error on failure. |
||
| 616 | */ |
||
| 617 | private function do_request( $post_id ) { |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Get the Batch Analysis results 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_complete( $post_id ) { |
||
| 690 | |||
| 691 | /** |
||
| 692 | * Get the {@link WP_Post}s' ids flagged with warnings. |
||
| 693 | * |
||
| 694 | * @since 3.14.2 |
||
| 695 | * |
||
| 696 | * @return array An array of {@link WP_Post}s' ids. |
||
| 697 | */ |
||
| 698 | View Code Duplication | public function get_warnings() { |
|
| 708 | |||
| 709 | } |
||
| 710 |
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