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 Timer 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 Timer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Timer |
||
| 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 $timerId |
||
| 38 | * @return string |
||
| 39 | */ |
||
| 40 | public function delete(array $timerData = []) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param $service |
||
| 60 | * @param $timerId |
||
| 61 | * @return boolean |
||
| 62 | */ |
||
| 63 | public function deleteServiceTimer($service, $timerId) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @return mixed |
||
| 78 | */ |
||
| 79 | public function getDescription() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return mixed |
||
| 86 | */ |
||
| 87 | public function getPrimaryService() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param $projectId |
||
| 94 | * @return mixed |
||
| 95 | */ |
||
| 96 | public function getProjectName($projectId) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @return mixed |
||
| 114 | */ |
||
| 115 | public function getProjects() |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return mixed |
||
| 122 | */ |
||
| 123 | View Code Duplication | public function getRecentTimers() |
|
| 135 | |||
| 136 | /** |
||
| 137 | * @param $service |
||
| 138 | * @return mixed |
||
| 139 | */ |
||
| 140 | public function getServiceDataCache($service) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @return mixed |
||
| 153 | */ |
||
| 154 | public function getTags() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @return mixed |
||
| 161 | */ |
||
| 162 | public function isRunning() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param $service |
||
| 169 | * @param null $action |
||
| 170 | * @param null $success |
||
| 171 | */ |
||
| 172 | public function setNotificationForService($service = null, $action = null, $success = null) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param $description |
||
| 183 | * @param $projectsData |
||
| 184 | * @param $tagData |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | public function start($description = '', array $projectData = [], array $tagData = [], $specificService = null) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | public function stop() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | View Code Duplication | public function syncOnlineDataToLocalCache() |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | public function undo() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param $name |
||
| 279 | * @param $value |
||
| 280 | */ |
||
| 281 | public function updateProperty($name, $value) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param $needle |
||
| 288 | * @return mixed |
||
| 289 | */ |
||
| 290 | private function getItems($needle) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param $name |
||
| 312 | * @return mixed |
||
| 313 | */ |
||
| 314 | private function getProperty($name) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @return mixed |
||
| 321 | */ |
||
| 322 | private function getRecentServiceTimers($service) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param $data |
||
| 329 | * @param string $service |
||
| 330 | */ |
||
| 331 | private function saveServiceDataCache($service, $data) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param string $service |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | private function syncServiceOnlineDataToLocalCache($service) |
||
| 353 | } |
||
| 354 |
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.