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 |
||
7 | class WorkflowHandler |
||
8 | { |
||
9 | /** |
||
10 | * @var mixed |
||
11 | */ |
||
12 | private $config; |
||
13 | |||
14 | /** |
||
15 | * @var mixed |
||
16 | */ |
||
17 | private $harvest; |
||
18 | |||
19 | /** |
||
20 | * @var mixed |
||
21 | */ |
||
22 | private $toggl; |
||
23 | |||
24 | /** |
||
25 | * @param Config $config |
||
26 | */ |
||
27 | View Code Duplication | public function __construct(Config $config = null) |
|
36 | |||
37 | /** |
||
38 | * @param array $results |
||
39 | * @param $action |
||
40 | * @return mixed |
||
41 | */ |
||
42 | public function getNotification(array $results = [], $action = null) |
||
56 | |||
57 | /** |
||
58 | * @param $service |
||
59 | * @param null $action |
||
60 | * @param null $success |
||
61 | */ |
||
62 | public function getNotificationForService($service = null, $action = null, $success = null) |
||
70 | |||
71 | /** |
||
72 | * This method shouldn't be in that class but rather |
||
73 | * in the services classes, but those classes don't have |
||
74 | * access to the cache, so, I'd rather sleep than creating |
||
75 | * a new Cache class and refactoring everything again |
||
76 | * @param $service |
||
77 | * @param $projectId |
||
78 | */ |
||
79 | public function getProjectName($service, $projectId) |
||
83 | |||
84 | /** |
||
85 | * @return mixed |
||
86 | */ |
||
87 | public function getProjects() |
||
91 | |||
92 | /** |
||
93 | * @return mixed |
||
94 | */ |
||
95 | public function getRecentTimers() |
||
105 | |||
106 | /** |
||
107 | * @param $service |
||
108 | * @return mixed |
||
109 | */ |
||
110 | public function getServiceDataCache($service) |
||
120 | |||
121 | /** |
||
122 | * @return mixed |
||
123 | */ |
||
124 | public function getTags() |
||
128 | |||
129 | /** |
||
130 | * @return mixed |
||
131 | */ |
||
132 | public function syncOnlineDataToLocalCache() |
||
142 | |||
143 | /** |
||
144 | * @param $needle |
||
145 | * @return mixed |
||
146 | */ |
||
147 | private function getItems($needle) |
||
167 | |||
168 | /** |
||
169 | * @return mixed |
||
170 | */ |
||
171 | private function getRecentServiceTimers($service) |
||
175 | |||
176 | /** |
||
177 | * @param $data |
||
178 | * @param string $service |
||
179 | */ |
||
180 | private function saveServiceDataCache($service, $data) |
||
185 | |||
186 | /** |
||
187 | * @param $service |
||
188 | * @return mixed |
||
189 | */ |
||
190 | private function syncServiceOnlineDataToLocalCache($service) |
||
202 | } |
||
203 |
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.