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 |
||
| 12 | class TaskService extends BaseService |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Constructor of the class. |
||
| 16 | * |
||
| 17 | * @param object $database |
||
| 18 | */ |
||
| 19 | public function __construct(\PDO $database) |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Check if the task exists. |
||
| 26 | * |
||
| 27 | * @param int $taskId |
||
| 28 | * @return object $task |
||
| 29 | * @throws \Exception |
||
| 30 | */ |
||
| 31 | public function checkTask($taskId) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Get all tasks. |
||
| 48 | * |
||
| 49 | * @return array |
||
| 50 | */ |
||
| 51 | public function getTasks() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get one task by id. |
||
| 63 | * |
||
| 64 | * @param int $taskId |
||
| 65 | * @return array |
||
| 66 | */ |
||
| 67 | public function getTask($taskId) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Search tasks by name. |
||
| 76 | * |
||
| 77 | * @param string $tasksName |
||
| 78 | * @return array |
||
| 79 | * @throws \Exception |
||
| 80 | */ |
||
| 81 | View Code Duplication | public function searchTasks($tasksName) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Create a task. |
||
| 99 | * |
||
| 100 | * @param array $input |
||
| 101 | * @return array |
||
| 102 | * @throws \Exception |
||
| 103 | */ |
||
| 104 | View Code Duplication | public function createTask($input) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Update a task. |
||
| 120 | * |
||
| 121 | * @param array $input |
||
| 122 | * @param int $taskId |
||
| 123 | * @return array |
||
| 124 | * @throws \Exception |
||
| 125 | */ |
||
| 126 | View Code Duplication | public function updateTask($input, $taskId) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Delete a task. |
||
| 143 | * |
||
| 144 | * @param int $taskId |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | View Code Duplication | public function deleteTask($taskId) |
|
| 158 | } |
||
| 159 |
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.