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 array |
||
18 | */ |
||
19 | private $currentImplementation = [ |
||
20 | 'start' => ['toggl'], |
||
21 | 'start_default' => ['toggl', 'harvest'], |
||
22 | 'stop' => ['toggl', 'harvest'], |
||
23 | 'delete' => ['toggl'], |
||
24 | 'get_projects' => ['toggl'], |
||
25 | 'get_tags' => ['toggl'], |
||
26 | 'get_timers' => ['toggl'], |
||
27 | 'sync_data' => ['toggl'], |
||
28 | ]; |
||
29 | |||
30 | /** |
||
31 | * @var mixed |
||
32 | */ |
||
33 | private $harvest; |
||
34 | |||
35 | /** |
||
36 | * @var mixed |
||
37 | */ |
||
38 | private $message; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | private $services = [ |
||
44 | 'toggl', |
||
45 | 'harvest', |
||
46 | ]; |
||
47 | |||
48 | /** |
||
49 | * @var mixed |
||
50 | */ |
||
51 | private $toggl; |
||
52 | |||
53 | View Code Duplication | public function __construct() |
|
|
|||
54 | { |
||
55 | $this->config = new Config(getenv('alfred_workflow_data') . '/config.json'); |
||
56 | |||
57 | $this->harvest = new Harvest($this->config->get('harvest', 'domain'), $this->config->get('harvest', 'api_token')); |
||
58 | $this->toggl = new Toggl($this->config->get('toggl', 'api_token')); |
||
59 | $this->message = ''; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @return mixed |
||
64 | */ |
||
65 | View Code Duplication | public function activatedServices() |
|
66 | { |
||
67 | $activatedServices = []; |
||
68 | |||
69 | foreach ($this->services as $service) { |
||
70 | if ($this->isServiceActive($service) === true) { |
||
71 | array_push($activatedServices, $service); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | return $activatedServices; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @param $service |
||
80 | * @param $timerId |
||
81 | * @return boolean |
||
82 | */ |
||
83 | View Code Duplication | public function deleteServiceTimer($service, $timerId) |
|
84 | { |
||
85 | $res = false; |
||
86 | |||
87 | if ($this->$service->deleteTimer($timerId) === true) { |
||
88 | if ($timerId === $this->config->get('workflow', 'timer_' . $service . '_id')) { |
||
89 | $this->config->update('workflow', 'timer_' . $service . '_id', null); |
||
90 | $res = true; |
||
91 | } |
||
92 | } |
||
93 | |||
94 | return $res; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param $timerId |
||
99 | * @return string |
||
100 | */ |
||
101 | public function deleteTimer($timerId) |
||
102 | { |
||
103 | $message = ''; |
||
104 | $atLeastOneTimerDeleted = false; |
||
105 | |||
106 | foreach ($this->implementedServicesForFeature('delete') as $service) { |
||
107 | if ($this->deleteServiceTimer($service, $timerId) === true) { |
||
108 | $atLeastOneTimerDeleted = true; |
||
109 | $message .= '- ' . ucfirst($service) .': deleted'."\r\n"; |
||
110 | } else { |
||
111 | $message .= '- ' . ucfirst($service) .': cannot delete'."\r\n"; |
||
112 | } |
||
113 | } |
||
114 | |||
115 | if ($atLeastOneTimerDeleted === true) { |
||
116 | $this->config->update('workflow', 'is_timer_running', false); |
||
117 | } |
||
118 | |||
119 | return $message; |
||
120 | } |
||
121 | |||
122 | public function generateDefaultConfigurationFile() |
||
123 | { |
||
124 | $this->config->generateDefaultConfigurationFile(); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param $projectId |
||
129 | * @return mixed |
||
130 | */ |
||
131 | View Code Duplication | public function getProjectName($projectId) |
|
132 | { |
||
133 | $projectName = ''; |
||
134 | |||
135 | $projects = $this->getProjects(); |
||
136 | |||
137 | foreach ($projects as $project) { |
||
138 | if ($project['id'] === $projectId) { |
||
139 | $projectName = $project['name']; |
||
140 | break; |
||
141 | } |
||
142 | } |
||
143 | |||
144 | return $projectName; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @return mixed |
||
149 | */ |
||
150 | View Code Duplication | public function getProjects() |
|
151 | { |
||
152 | $projects = []; |
||
153 | |||
154 | /* |
||
155 | * Temporary, only get the projects of Toggl |
||
156 | * Later, we will get Harvest ones too |
||
157 | */ |
||
158 | foreach ($this->implementedServicesForFeature('get_projects') as $service) { |
||
159 | if ($this->isServiceActive($service) === true) { |
||
160 | $projects = $this->getServiceProjects($service); |
||
161 | } |
||
162 | } |
||
163 | |||
164 | return $projects; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @return mixed |
||
169 | */ |
||
170 | View Code Duplication | public function getRecentTimers() |
|
171 | { |
||
172 | $timers = []; |
||
173 | |||
174 | foreach ($this->implementedServicesForFeature('get_timers') as $service) { |
||
175 | if ($this->isServiceActive($service) === true) { |
||
176 | $timers = array_merge($timers, $this->getRecentServiceTimers($service)); |
||
177 | } |
||
178 | } |
||
179 | |||
180 | return $timers; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @param $service |
||
185 | * @return mixed |
||
186 | */ |
||
187 | View Code Duplication | public function getServiceDataCache($service) |
|
188 | { |
||
189 | $data = []; |
||
190 | $cacheFile = getenv('alfred_workflow_data') . '/' . $service . '_cache.json'; |
||
191 | |||
192 | if (file_exists($cacheFile)) { |
||
193 | $data = json_decode(file_get_contents($cacheFile), true); |
||
194 | } |
||
195 | |||
196 | return $data; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @param $service |
||
201 | */ |
||
202 | View Code Duplication | public function getServiceProjects($service) |
|
203 | { |
||
204 | $projects = $this->getServiceDataCache($service); |
||
205 | |||
206 | if (isset($projects['data']['projects']) === true) { |
||
207 | /* |
||
208 | * To only show projects that are currently active |
||
209 | * The Toggl API is slightly weird on that |
||
210 | */ |
||
211 | foreach ($projects['data']['projects'] as $key => $project) { |
||
212 | if (isset($project['server_deleted_at']) === true) { |
||
213 | unset($projects['data']['projects'][$key]); |
||
214 | } |
||
215 | } |
||
216 | |||
217 | $projects = $projects['data']['projects']; |
||
218 | } |
||
219 | |||
220 | return $projects; |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * @return mixed |
||
225 | */ |
||
226 | View Code Duplication | public function getTags() |
|
238 | |||
239 | /** |
||
240 | * @return mixed |
||
241 | */ |
||
242 | public function getTimerDescription() |
||
246 | |||
247 | /** |
||
248 | * @return boolean |
||
249 | */ |
||
250 | public function hasTimerRunning() |
||
254 | |||
255 | /** |
||
256 | * @param string $feature |
||
257 | * @return mixed |
||
258 | */ |
||
259 | View Code Duplication | public function implementedServicesForFeature($feature = null) |
|
260 | { |
||
261 | $services = []; |
||
262 | |||
263 | if (isset($this->currentImplementation[$feature]) === true) { |
||
264 | $services = $this->currentImplementation[$feature]; |
||
265 | } |
||
266 | |||
267 | return $services; |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * @return boolean |
||
272 | */ |
||
273 | public function isConfigured() |
||
277 | |||
278 | /** |
||
279 | * @param $service |
||
280 | * @return mixed |
||
281 | */ |
||
282 | public function isServiceActive($service) |
||
286 | |||
287 | /** |
||
288 | * @return mixed |
||
289 | */ |
||
290 | View Code Duplication | public function servicesToUndo() |
|
291 | { |
||
292 | $services = []; |
||
293 | |||
294 | foreach ($this->activatedServices() as $service) { |
||
295 | if ($this->config->get('workflow', 'timer_' . $service . '_id') !== null) { |
||
296 | array_push($services, $service); |
||
297 | } |
||
302 | |||
303 | /** |
||
304 | * @param $description |
||
305 | * @param $projectsDefault |
||
306 | * @param null $tagsDefault |
||
307 | * @param boolean $startDefault |
||
308 | * @return string |
||
309 | */ |
||
310 | public function startTimer($description = '', $projectsDefault = null, $tagsDefault = null, $startDefault = false) |
||
352 | |||
353 | /** |
||
354 | * @param $description |
||
355 | * @return string |
||
356 | */ |
||
357 | View Code Duplication | public function startTimerWithDefaultOptions($description) |
|
371 | |||
372 | /** |
||
373 | * @return string |
||
374 | */ |
||
375 | public function stopRunningTimer() |
||
397 | |||
398 | /** |
||
399 | * @return string |
||
400 | */ |
||
401 | View Code Duplication | public function syncOnlineDataToLocalCache() |
|
413 | |||
414 | /** |
||
415 | * @return string |
||
416 | */ |
||
417 | public function undoTimer() |
||
442 | |||
443 | /** |
||
444 | * @return mixed |
||
445 | */ |
||
446 | private function getRecentServiceTimers($service) |
||
450 | |||
451 | /** |
||
452 | * @return mixed |
||
453 | */ |
||
454 | View Code Duplication | private function getServiceTags($service) |
|
464 | |||
465 | /** |
||
466 | * @param $data |
||
467 | * @param string $service |
||
468 | */ |
||
469 | private function saveServiceDataCache($service, $data) |
||
474 | |||
475 | /** |
||
476 | * @param string $service |
||
477 | * @return string |
||
478 | */ |
||
479 | private function syncServiceOnlineDataToLocalCache($service) |
||
492 | } |
||
493 |
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.