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 |
||
| 5 | View Code Duplication | class Videoobject_Background_Process extends \Wordlift_Plugin_WP_Background_Process { |
|
|
|
|||
| 6 | |||
| 7 | const WL_CMKG_ANALYSIS_BACKGROUND_PROCESS = '_wl_cmkg_analysis_background_process'; |
||
| 8 | |||
| 9 | |||
| 10 | protected $action = 'wl_cmkg_analysis_background__analysis'; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @var Analysis_Background_Service |
||
| 14 | */ |
||
| 15 | private $analysis_background_service; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var \Wordlift_Log_Service |
||
| 19 | */ |
||
| 20 | private $log; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Analysis_Background_Process constructor. |
||
| 24 | * |
||
| 25 | * @param $analysis_background_service Analysis_Background_Service A {@link Analysis_Background_Service} instance providing the supporting functions to this background process. |
||
| 26 | */ |
||
| 27 | public function __construct( $analysis_background_service ) { |
||
| 36 | |||
| 37 | /** |
||
| 38 | * This function is called: |
||
| 39 | * - To start a new Synchronization, by passing a {@link Sync_Start_Message} instance. |
||
| 40 | * - To synchronize a post, by passing a numeric ID. |
||
| 41 | * |
||
| 42 | * This function returns the parameter for the next call or NULL if there are no more posts to process. |
||
| 43 | * |
||
| 44 | * @param int[] $term_ids An array of term IDs. |
||
| 45 | * |
||
| 46 | * @return int[]|false The next term IDs or false if there are no more. |
||
| 47 | */ |
||
| 48 | protected function task( $term_ids ) { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Start the background processing. |
||
| 66 | * |
||
| 67 | * @return bool True if the process has been started, otherwise false. |
||
| 68 | */ |
||
| 69 | public function start() { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Set the transient to cancel the process. The next time the process runs, it'll check whether this transient is |
||
| 94 | * set and will stop processing. |
||
| 95 | */ |
||
| 96 | public function request_cancel() { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Get the sync state. |
||
| 104 | * |
||
| 105 | * @return Sync_State The {@link Sync_State}. |
||
| 106 | */ |
||
| 107 | public static function get_state() { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Check whether the provided state is `started` or not. |
||
| 119 | * |
||
| 120 | * @param Sync_State $state The {@link Sync_State}. |
||
| 121 | * |
||
| 122 | * @return bool True if the state is started. |
||
| 123 | */ |
||
| 124 | private function is_started( $state ) { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Check whether the process must cancel or not. |
||
| 130 | * |
||
| 131 | * @return bool Whether to cancel or not the process. |
||
| 132 | */ |
||
| 133 | private function must_cancel() { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Cancels the current process. |
||
| 140 | */ |
||
| 141 | public function cancel() { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Push the post with the provided ID to the remote platform. |
||
| 160 | * |
||
| 161 | * @param int[] $term_ids The term IDs. |
||
| 162 | * |
||
| 163 | * @return int[]|false The next term ID to process or false if processing is complete. |
||
| 164 | */ |
||
| 165 | private function sync_items( $term_ids ) { |
||
| 210 | |||
| 211 | } |
||
| 212 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.