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 Harvest extends Service |
||
11 | { |
||
12 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | protected $methods = [ |
||
16 | 'start' => 'post', |
||
17 | 'stop' => 'get', |
||
18 | 'delete' => 'delete', |
||
19 | 'timer_running' => 'get', |
||
20 | 'get_projects' => 'get', |
||
21 | 'get_tags' => 'get', |
||
22 | 'get_recent_timers' => 'get', |
||
23 | ]; |
||
24 | |||
25 | /** |
||
26 | * @param $domain |
||
27 | * @param null $apiToken |
||
28 | */ |
||
29 | public function __construct($domain = null, $apiToken = null) |
||
33 | |||
34 | /** |
||
35 | * @param $timerId |
||
36 | */ |
||
37 | public function apiDeleteUrl($timerId) |
||
41 | |||
42 | public function apiStartUrl() |
||
46 | |||
47 | /** |
||
48 | * @param $timerId |
||
49 | */ |
||
50 | public function apiStopUrl($timerId) |
||
54 | |||
55 | /** |
||
56 | * @param $description |
||
57 | * @param $projectId |
||
58 | * @param $taskId |
||
59 | * @return mixed |
||
60 | */ |
||
61 | public function generateTimer($description, $projectId, $taskId) |
||
69 | |||
70 | /** |
||
71 | * @return mixed |
||
72 | */ |
||
73 | public function getOnlineData() |
||
81 | |||
82 | /** |
||
83 | * @param $data |
||
84 | * @return mixed |
||
85 | */ |
||
86 | public function getProjects($data) |
||
90 | |||
91 | View Code Duplication | public function getRecentTimers() |
|
108 | |||
109 | /** |
||
110 | * @param $data |
||
111 | * @return mixed |
||
112 | */ |
||
113 | public function getTags($data) |
||
117 | |||
118 | /** |
||
119 | * @param string $action |
||
120 | * @param string $apiUri |
||
121 | * @return mixed |
||
122 | */ |
||
123 | View Code Duplication | protected function timerAction($action, $apiUri, array $options = []) |
|
142 | |||
143 | /** |
||
144 | * @param $needle |
||
145 | * @param array $haystack |
||
146 | * @return mixed |
||
147 | */ |
||
148 | private function getItems($needle, array $haystack = []) |
||
165 | |||
166 | /** |
||
167 | * @param $timerId |
||
168 | * @return boolean |
||
169 | */ |
||
170 | private function isTimerRunning($timerId) |
||
176 | } |
||
177 |
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.