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 | abstract class Background_Process extends \Wordlift_Plugin_WP_Background_Process { |
||
| 6 | /** |
||
| 7 | * @var \Wordlift_Log_Service |
||
| 8 | */ |
||
| 9 | private $log; |
||
| 10 | /** |
||
| 11 | * @var Data_Source |
||
| 12 | */ |
||
| 13 | protected $data_source; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Background_Process constructor. |
||
| 17 | * |
||
| 18 | * @param $data_source Data_Source |
||
| 19 | */ |
||
| 20 | public function __construct( $data_source ) { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The key which is used to store the Sync_State class for the current process |
||
| 30 | * @return string |
||
| 31 | */ |
||
| 32 | protected abstract function get_state_storage_key(); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The key which is used as prefix to store the options. |
||
| 36 | * @return string |
||
| 37 | */ |
||
| 38 | protected abstract function get_action_key(); |
||
| 39 | |||
| 40 | |||
| 41 | /** |
||
| 42 | * Check whether the process must cancel or not. |
||
| 43 | * |
||
| 44 | * @return bool Whether to cancel or not the process. |
||
| 45 | */ |
||
| 46 | private function must_cancel() { |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Check whether the provided state is `started` or not. |
||
| 53 | * |
||
| 54 | * @param Sync_State $state The {@link Sync_State}. |
||
| 55 | * |
||
| 56 | * @return bool True if the state is started. |
||
| 57 | */ |
||
| 58 | private function is_started( $state ) { |
||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * Start the background processing. |
||
| 65 | * |
||
| 66 | * @return bool True if the process has been started, otherwise false. |
||
| 67 | */ |
||
| 68 | public function start() { |
||
| 92 | |||
| 93 | |||
| 94 | /** |
||
| 95 | * Cancels the current process. |
||
| 96 | */ |
||
| 97 | public function cancel() { |
||
| 114 | |||
| 115 | |||
| 116 | /** |
||
| 117 | * Get the sync state. |
||
| 118 | * |
||
| 119 | * @return Sync_State The {@link Sync_State}. |
||
| 120 | */ |
||
| 121 | public function get_state() { |
||
| 130 | |||
| 131 | |||
| 132 | /** |
||
| 133 | * This function is called: |
||
| 134 | * - To start a new Synchronization, by passing a {@link Sync_Start_Message} instance. |
||
| 135 | * - To process a item, by passing a numeric ID. |
||
| 136 | * |
||
| 137 | * This function returns the parameter for the next call or NULL if there are no more items to process. |
||
| 138 | * |
||
| 139 | * @param int[] $items An array of item IDs. |
||
| 140 | * |
||
| 141 | * @return int[]|false The next IDs or false if there are no more. |
||
| 142 | */ |
||
| 143 | protected function task( $items ) { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Process all the items in the current batch. |
||
| 172 | * |
||
| 173 | * @param $items |
||
| 174 | * |
||
| 175 | * @return bool If all items are successfully processed. |
||
| 176 | */ |
||
| 177 | abstract protected function process_items( $items ); |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Return next batch of items after processing. |
||
| 181 | * @return int[] or false |
||
| 182 | */ |
||
| 183 | protected function get_next_batch() { |
||
| 189 | |||
| 190 | |||
| 191 | private function update_batch_index() { |
||
| 209 | |||
| 210 | |||
| 211 | } |
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.