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 Toggl extends Service |
||
11 | { |
||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $apiBaseUrl = 'https://www.toggl.com/api/v8/'; |
||
16 | |||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $methods = [ |
||
21 | 'start' => 'post', |
||
22 | 'stop' => 'put', |
||
23 | 'delete' => 'delete', |
||
24 | 'get_recent_timers' => 'get', |
||
25 | 'get_online_data' => 'get', |
||
26 | ]; |
||
27 | |||
28 | /** |
||
29 | * @param $apiToken |
||
30 | */ |
||
31 | public function __construct($apiToken = null) |
||
35 | |||
36 | /** |
||
37 | * @param $timerId |
||
38 | */ |
||
39 | public function apiDeleteUrl($timerId) |
||
43 | |||
44 | public function apiStartUrl() |
||
48 | |||
49 | /** |
||
50 | * @param $timerId |
||
51 | */ |
||
52 | public function apiStopUrl($timerId) |
||
56 | |||
57 | /** |
||
58 | * @param $description |
||
59 | * @param $projectId |
||
60 | * @param $tagData |
||
61 | */ |
||
62 | public function generateTimer($description, $projectId, $tagData) |
||
73 | |||
74 | /** |
||
75 | * @return mixed |
||
76 | */ |
||
77 | public function getOnlineData() |
||
81 | |||
82 | /** |
||
83 | * @param $projectId |
||
84 | * @return mixed |
||
85 | */ |
||
86 | public function getProjectName($projectId, array $data = []) |
||
100 | |||
101 | /** |
||
102 | * @param $data |
||
103 | * @return mixed |
||
104 | */ |
||
105 | public function getProjects($data) |
||
109 | |||
110 | /** |
||
111 | * @return mixed |
||
112 | */ |
||
113 | View Code Duplication | public function getRecentTimers() |
|
130 | |||
131 | /** |
||
132 | * @param $data |
||
133 | * @return mixed |
||
134 | */ |
||
135 | public function getTags($data) |
||
139 | |||
140 | /** |
||
141 | * @param string $action |
||
142 | * @param string $apiUri |
||
143 | * @return mixed |
||
144 | */ |
||
145 | View Code Duplication | protected function timerAction($action, $apiUri, array $options = []) |
|
158 | |||
159 | /** |
||
160 | * @param $needle |
||
161 | * @param array $haystack |
||
162 | * @return mixed |
||
163 | */ |
||
164 | private function getItems($needle, array $haystack = []) |
||
189 | } |
||
190 |
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.