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 |
||
10 | class TasksService extends BaseService |
||
11 | { |
||
12 | /** |
||
13 | * Constructor of the class. |
||
14 | * |
||
15 | * @param object $database |
||
16 | */ |
||
17 | public function __construct(\PDO $database) |
||
21 | |||
22 | /** |
||
23 | * Check if the task exists. |
||
24 | * |
||
25 | * @param int $taskId |
||
26 | * @return object $task |
||
27 | * @throws \Exception |
||
28 | */ |
||
29 | public function checkTask($taskId) |
||
43 | |||
44 | /** |
||
45 | * Get all tasks. |
||
46 | * |
||
47 | * @return array |
||
48 | */ |
||
49 | public function getTasks() |
||
58 | |||
59 | /** |
||
60 | * Get one task by id. |
||
61 | * |
||
62 | * @param int $taskId |
||
63 | * @return array |
||
64 | */ |
||
65 | public function getTask($taskId) |
||
71 | |||
72 | /** |
||
73 | * Search tasks by name. |
||
74 | * |
||
75 | * @param string $tasksName |
||
76 | * @return array |
||
77 | * @throws \Exception |
||
78 | */ |
||
79 | View Code Duplication | public function searchTasks($tasksName) |
|
94 | |||
95 | /** |
||
96 | * Create a task. |
||
97 | * |
||
98 | * @param array $input |
||
99 | * @return array |
||
100 | * @throws \Exception |
||
101 | */ |
||
102 | View Code Duplication | public function createTask($input) |
|
115 | |||
116 | /** |
||
117 | * Update a task. |
||
118 | * |
||
119 | * @param array $input |
||
120 | * @param int $taskId |
||
121 | * @return array |
||
122 | * @throws \Exception |
||
123 | */ |
||
124 | View Code Duplication | public function updateTask($input, $taskId) |
|
138 | |||
139 | /** |
||
140 | * Delete a task. |
||
141 | * |
||
142 | * @param int $taskId |
||
143 | * @return array |
||
144 | */ |
||
145 | public function deleteTask($taskId) |
||
156 | } |
||
157 |