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) |
|
| 58 | |||
| 59 | /** |
||
| 60 | * @return mixed |
||
| 61 | */ |
||
| 62 | public function getLastMessage() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @return mixed |
||
| 69 | */ |
||
| 70 | public function getOnlineData() |
||
| 87 | |||
| 88 | public function getRecentTimers() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param $description |
||
| 105 | * @param $projectId |
||
| 106 | * @param $tagNames |
||
| 107 | * @return mixed |
||
| 108 | */ |
||
| 109 | public function startTimer($description, $projectId, $tagNames) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param $timerId |
||
| 138 | * @return mixed |
||
| 139 | */ |
||
| 140 | View Code Duplication | public function stopTimer($timerId = null) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * @param $message |
||
| 160 | */ |
||
| 161 | private function setMessage($message = null) |
||
| 165 | |||
| 166 | public function getProjects() |
||
| 170 | } |
||
| 171 |
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.