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 |
||
| 6 | class Task extends MY_Controller { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Constructor |
||
| 10 | */ |
||
| 11 | public function __construct() |
||
| 19 | |||
| 20 | // -------------------------------------------------------------------------- |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Redirect to task list |
||
| 24 | */ |
||
| 25 | public function index() |
||
| 29 | |||
| 30 | // -------------------------------------------------------------------------- |
||
| 31 | |||
| 32 | /** |
||
| 33 | * List shared tasks |
||
| 34 | */ |
||
| 35 | View Code Duplication | public function shared() |
|
| 46 | |||
| 47 | // -------------------------------------------------------------------------- |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Get the main task view |
||
| 51 | */ |
||
| 52 | View Code Duplication | public function list_tasks() |
|
| 63 | |||
| 64 | // -------------------------------------------------------------------------- |
||
| 65 | |||
| 66 | /** |
||
| 67 | * List archived tasks |
||
| 68 | */ |
||
| 69 | public function archive($page = 1) |
||
| 98 | |||
| 99 | // -------------------------------------------------------------------------- |
||
| 100 | |||
| 101 | /** |
||
| 102 | * List overdue tasks |
||
| 103 | */ |
||
| 104 | View Code Duplication | public function overdue() |
|
| 115 | |||
| 116 | // -------------------------------------------------------------------------- |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Add a task |
||
| 120 | */ |
||
| 121 | public function add() |
||
| 122 | { |
||
| 123 | $data = array(); |
||
| 124 | $data['err'] = ''; |
||
| 125 | $data['cat_list'] = $this->todo->get_category_select(); |
||
| 126 | $data['pri_list'] = $this->todo->get_priority_select(); |
||
| 127 | $data['group_perms'] = ''; |
||
| 128 | $data['groups'] = $this->todo->get_group_list($this->session->userdata('uid')); |
||
| 129 | $data['task_title'] = ''; |
||
| 130 | $data['description'] = ''; |
||
| 131 | $data['due'] = mktime(12, 00, 00); |
||
| 132 | $data['title'] = ''; |
||
| 133 | $data['rem_hours'] = 0; |
||
| 134 | $data['rem_minutes'] = 30; |
||
| 135 | $data['reminder'] = FALSE; |
||
| 136 | $data['friends'] = $this->todo->get_friend_list(); |
||
| 137 | |||
| 138 | |||
| 139 | if ($this->input->post('add_sub') == 'Add Task') |
||
| 140 | { |
||
| 141 | $val = $this->task_model->validate_task(); |
||
| 142 | |||
| 143 | if($val === TRUE) |
||
| 144 | { |
||
| 145 | $done = $this->task_model->add_task(); |
||
| 146 | |||
| 147 | if ($done === TRUE) |
||
| 148 | { |
||
| 149 | //Redirect to task list |
||
| 150 | $this->todo->redirect_303('task/list'); |
||
| 151 | } |
||
| 152 | else |
||
| 153 | { |
||
| 154 | $data['err'][] = "Database Error, Please try again later."; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | else |
||
| 158 | { |
||
| 159 | //Get form values |
||
| 160 | $data = array_merge($data, $this->task_model->form_vals); |
||
| 161 | $data['err'] = $val; |
||
| 162 | |||
| 163 | } |
||
| 164 | |||
| 165 | } |
||
| 166 | |||
| 167 | $this->page->set_title("Add Task"); |
||
| 168 | $this->page->build('task/add', $data); |
||
| 169 | } |
||
| 170 | |||
| 171 | // -------------------------------------------------------------------------- |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Edit a task |
||
| 175 | * |
||
| 176 | * @param int $task_id |
||
| 177 | */ |
||
| 178 | public function edit(int $task_id) |
||
| 217 | |||
| 218 | // -------------------------------------------------------------------------- |
||
| 219 | |||
| 220 | /** |
||
| 221 | * View an individual task |
||
| 222 | * |
||
| 223 | * @param int $task_id |
||
| 224 | */ |
||
| 225 | public function view(int $task_id = NULL) |
||
| 246 | |||
| 247 | // -------------------------------------------------------------------------- |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Delete a task |
||
| 251 | */ |
||
| 252 | public function delete(int $task_id) |
||
| 256 | |||
| 257 | // -------------------------------------------------------------------------- |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Add a task comment |
||
| 261 | */ |
||
| 262 | public function add_task_comment() |
||
| 267 | |||
| 268 | // -------------------------------------------------------------------------- |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Get a list of comments for the task |
||
| 272 | */ |
||
| 273 | public function get_task_comments() |
||
| 281 | |||
| 282 | // -------------------------------------------------------------------------- |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Delete a task comment |
||
| 286 | */ |
||
| 287 | public function del_task_comment() |
||
| 292 | |||
| 293 | // -------------------------------------------------------------------------- |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Update the status of a task |
||
| 297 | */ |
||
| 298 | public function update_status() |
||
| 303 | |||
| 304 | // -------------------------------------------------------------------------- |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Update the category that the task belongs to. |
||
| 308 | */ |
||
| 309 | public function update_category() |
||
| 314 | |||
| 315 | // -------------------------------------------------------------------------- |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Add a checklist item to the task |
||
| 319 | */ |
||
| 320 | public function add_checklist_item() |
||
| 337 | |||
| 338 | // -------------------------------------------------------------------------- |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Update a task checklist item |
||
| 342 | */ |
||
| 343 | public function update_checklist_item() |
||
| 350 | } |
||
| 351 | // End of controllers/task.php |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.