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 |
||
| 25 | class Task_Single_Instance_Task_Runner { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Define the transient prefix. |
||
| 29 | * |
||
| 30 | * @since 1.0.0 |
||
| 31 | */ |
||
| 32 | const IS_RUNNING_PREFIX = '_wf_task_runner__'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * A {@link Wordlift_Log_Service} instance. |
||
| 36 | * |
||
| 37 | * @since 1.0.0 |
||
| 38 | * @var Wordlift_Log_Service A {@link Wordlift_Log_Service} instance. |
||
| 39 | * @access private |
||
| 40 | */ |
||
| 41 | private $log; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The {@link Task} to execute. |
||
| 45 | * |
||
| 46 | * @since 1.0.0 |
||
| 47 | * @var Task $task The {@link Task} to execute. |
||
| 48 | * @access private |
||
| 49 | */ |
||
| 50 | private $task; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * One or more callbacks to call to update about the task progress. |
||
| 54 | * |
||
| 55 | * @since 1.0.0 |
||
| 56 | * @var Task_Progress[] $callbacks An array of {@link Wordlift_For_Bungalowparkoverzicht_Progress}. |
||
| 57 | * @access private |
||
| 58 | */ |
||
| 59 | private $callbacks; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Whether to force starting a task even if another instance of the task is already running. |
||
| 63 | * |
||
| 64 | * @since 1.0.0 |
||
| 65 | * @var bool $force Whether to force starting a task even if another instance of the task is already running. |
||
| 66 | * @access private |
||
| 67 | */ |
||
| 68 | private $force; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Create a {@link Task_Single_Instance_Task_Runner} instance. |
||
| 72 | * |
||
| 73 | * @param Task $task The {@link Task} instance. |
||
| 74 | * @param bool $force Whether to force starting a task even if another instance of the task is already running, default `false`. |
||
| 75 | * @param array $callbacks An array of {@link Wordlift_For_Bungalowparkoverzicht_Progress}. |
||
| 76 | * |
||
| 77 | * @since 1.0.0 |
||
| 78 | */ |
||
| 79 | View Code Duplication | public function __construct( $task, $force = false, $callbacks = array() ) { |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Get the transient name for running flag. |
||
| 91 | * |
||
| 92 | * @return string The transient name. |
||
| 93 | * @since 1.0.0 |
||
| 94 | */ |
||
| 95 | private function get_running_transient() { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Check whether a task is running. |
||
| 102 | * |
||
| 103 | * @return bool |
||
| 104 | * @since 1.0.0 |
||
| 105 | */ |
||
| 106 | public function is_running() { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Set whether the task is running or not. |
||
| 112 | * |
||
| 113 | * @param bool $value Whether the task is running or not. |
||
| 114 | * |
||
| 115 | * @since 1.0.0 |
||
| 116 | */ |
||
| 117 | public function set_running( $value ) { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Start the task. |
||
| 123 | * |
||
| 124 | * @param int $limit The maximum number of items to process. |
||
| 125 | * @param int $offset The starting offset. |
||
| 126 | * |
||
| 127 | * @throws Task_Another_Instance_Is_Running_Exception if the task is already running. |
||
| 128 | * @since 1.0.0 |
||
| 129 | */ |
||
| 130 | public function start( $limit = 0, $offset = 0 ) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Set the total number of items to process. |
||
| 164 | * |
||
| 165 | * @param int $value The total number of items to process. |
||
| 166 | * |
||
| 167 | * @since 1.0.0 |
||
| 168 | */ |
||
| 169 | private function set_count( $value ) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Set the task progress. |
||
| 183 | * |
||
| 184 | * @param int $index The current item index. |
||
| 185 | * @param mixed $item The current item. |
||
| 186 | * |
||
| 187 | * @since 1.0.0 |
||
| 188 | */ |
||
| 189 | private function set_progress( $index, $item ) { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Inform the callbacks that the task completed. |
||
| 203 | * |
||
| 204 | * @since 1.0.0 |
||
| 205 | */ |
||
| 206 | private function finish() { |
||
| 217 | |||
| 218 | } |
||
| 219 |
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.