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:
Complex classes like Time often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Time, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Time |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var mixed |
||
| 13 | */ |
||
| 14 | private $config; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var mixed |
||
| 18 | */ |
||
| 19 | private $harvest; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var mixed |
||
| 23 | */ |
||
| 24 | private $toggl; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @param Config $config |
||
| 28 | */ |
||
| 29 | public function __construct(Config $config = null) |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param $service |
||
| 38 | * @param $timerId |
||
| 39 | * @return boolean |
||
| 40 | */ |
||
| 41 | public function deleteServiceTimer($service, $timerId) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param $timerId |
||
| 56 | * @return string |
||
| 57 | */ |
||
| 58 | public function deleteTimer($timerId) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param $projectId |
||
| 85 | * @return mixed |
||
| 86 | */ |
||
| 87 | public function getProjectName($projectId) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @return mixed |
||
| 105 | */ |
||
| 106 | View Code Duplication | public function getProjects() |
|
| 122 | |||
| 123 | /** |
||
| 124 | * @return mixed |
||
| 125 | */ |
||
| 126 | View Code Duplication | public function getRecentTimers() |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @param $service |
||
| 141 | * @return mixed |
||
| 142 | */ |
||
| 143 | public function getServiceDataCache($service) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return mixed |
||
| 157 | */ |
||
| 158 | View Code Duplication | public function getTags() |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @param $service |
||
| 177 | * @param null $action |
||
| 178 | * @param null $success |
||
| 179 | */ |
||
| 180 | public function setNotificationForService($service = null, $action = null, $success = null) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param $description |
||
| 191 | * @param $projectsDefault |
||
| 192 | * @param null $tagsDefault |
||
| 193 | * @param string $startType |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | public function startTimer($description = '', array $projectsDefault = [], array $tagsDefault = [], $startType = 'start') |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param $description |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | public function startTimerWithDefaultOptions($description) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | View Code Duplication | public function stopRunningTimer() |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | View Code Duplication | public function syncOnlineDataToLocalCache() |
|
| 292 | |||
| 293 | /** |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | View Code Duplication | public function undoTimer() |
|
| 313 | |||
| 314 | /** |
||
| 315 | * @return mixed |
||
| 316 | */ |
||
| 317 | private function getRecentServiceTimers($service) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param $data |
||
| 324 | * @param string $service |
||
| 325 | */ |
||
| 326 | private function saveServiceDataCache($service, $data) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param string $service |
||
| 334 | * @return string |
||
| 335 | */ |
||
| 336 | private function syncServiceOnlineDataToLocalCache($service) |
||
| 348 | } |
||
| 349 |
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.