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 |
||
13 | View Code Duplication | class GoalsClient extends ManagementClient |
|
|
|||
14 | { |
||
15 | |||
16 | /** |
||
17 | * Get counter goals |
||
18 | * |
||
19 | * @see http://api.yandex.ru/metrika/doc/beta/management/goals/goals.xml |
||
20 | * |
||
21 | * @param int $counterId |
||
22 | * @return Models\Goals |
||
23 | */ |
||
24 | 1 | public function getGoals($counterId) |
|
31 | |||
32 | |||
33 | /** |
||
34 | * Add goal to counter |
||
35 | * |
||
36 | * @see http://api.yandex.ru/metrika/doc/beta/management/goals/addgoal.xml |
||
37 | * |
||
38 | * @param int $counterId |
||
39 | * @param Models\Goal $goal |
||
40 | * @return Models\Goal |
||
41 | */ |
||
42 | 1 | public function addGoal($counterId, Models\Goal $goal) |
|
43 | { |
||
44 | 1 | $resource = 'counter/' . $counterId . '/goals'; |
|
45 | 1 | $response = $this->sendPostRequest($resource, ["goal" => $goal->toArray()]); |
|
46 | 1 | $goalResponse = new Models\AddGoalResponse($response); |
|
47 | 1 | return $goalResponse->getGoal(); |
|
48 | } |
||
49 | |||
50 | |||
51 | /** |
||
52 | * Get counter goal |
||
53 | * |
||
54 | * @see http://api.yandex.ru/metrika/doc/beta/management/goals/goal.xml |
||
55 | * |
||
56 | * @param int $id |
||
57 | * @param int $counterId |
||
58 | * @return Models\Goal |
||
59 | */ |
||
60 | 1 | public function getGoal($id, $counterId) |
|
67 | |||
68 | |||
69 | /** |
||
70 | * Update counter goal |
||
71 | * |
||
72 | * @see http://api.yandex.ru/metrika/doc/beta/management/goals/editgoal.xml |
||
73 | * |
||
74 | * @param int $id |
||
75 | * @param int $counterId |
||
76 | * @param Models\Goal $goal |
||
77 | * @return Models\Goal |
||
78 | */ |
||
79 | 1 | public function updateGoal($id, $counterId, Models\Goal $goal) |
|
86 | |||
87 | |||
88 | /** |
||
89 | * Delete counter goal |
||
90 | * |
||
91 | * @see http://api.yandex.ru/metrika/doc/ref/reference/del-counter-goal.xml |
||
92 | * |
||
93 | * @param int $id |
||
94 | * @param int $counterId |
||
95 | * @return array |
||
96 | */ |
||
97 | 1 | public function deleteGoal($id, $counterId) |
|
103 | } |
||
104 |
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.