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 |
||
11 | { |
||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | private $message = ''; |
||
16 | |||
17 | /** |
||
18 | * @var mixed |
||
19 | */ |
||
20 | private $serviceApiCall = null; |
||
21 | |||
22 | /** |
||
23 | * @param $apiToken |
||
24 | */ |
||
25 | View Code Duplication | public function __construct($apiToken = null) |
|
36 | |||
37 | /** |
||
38 | * @param $timerId |
||
39 | * @return mixed |
||
40 | */ |
||
41 | View Code Duplication | public function deleteTimer($timerId = null) |
|
53 | |||
54 | /** |
||
55 | * @return string |
||
56 | */ |
||
57 | public function getLastMessage() |
||
61 | |||
62 | /** |
||
63 | * @return mixed |
||
64 | */ |
||
65 | View Code Duplication | public function getOnlineData() |
|
77 | |||
78 | public function getProjects() |
||
82 | |||
83 | public function getRecentTimers() |
||
87 | |||
88 | /** |
||
89 | * @param $description |
||
90 | * @param $projectId |
||
91 | * @param $tagNames |
||
92 | * @return mixed |
||
93 | */ |
||
94 | public function startTimer($description, $projectId, $tagNames) |
||
118 | |||
119 | /** |
||
120 | * @param $timerId |
||
121 | * @return mixed |
||
122 | */ |
||
123 | View Code Duplication | public function stopTimer($timerId = null) |
|
135 | |||
136 | /** |
||
137 | * @param string $message |
||
138 | */ |
||
139 | private function setMessage($message = null) |
||
143 | |||
144 | /** |
||
145 | * @param string $action |
||
146 | * @param string $apiUri |
||
147 | * @return mixed |
||
148 | */ |
||
149 | private function timerAction($action, $apiUri, array $options = []) |
||
175 | } |
||
176 |
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.