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 ForecastOfOpportunities 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 ForecastOfOpportunities, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class ForecastOfOpportunities |
||
21 | { |
||
22 | /** @var RegistryInterface */ |
||
23 | protected $doctrine; |
||
24 | |||
25 | /** @var NumberFormatter */ |
||
26 | protected $numberFormatter; |
||
27 | |||
28 | /** @var DateTimeFormatter */ |
||
29 | protected $dateTimeFormatter; |
||
30 | |||
31 | /** @var AclHelper */ |
||
32 | protected $aclHelper; |
||
33 | |||
34 | /** @var TranslatorInterface */ |
||
35 | protected $translator; |
||
36 | |||
37 | /** @var array */ |
||
38 | protected $ownersValues; |
||
39 | |||
40 | /** @var SecurityFacade */ |
||
41 | protected $securityFacade; |
||
42 | |||
43 | /** @var User */ |
||
44 | protected $currentUser; |
||
45 | |||
46 | /** |
||
47 | * @param RegistryInterface $doctrine |
||
48 | * @param NumberFormatter $numberFormatter |
||
49 | * @param DateTimeFormatter $dateTimeFormatter |
||
50 | * @param AclHelper $aclHelper |
||
51 | * @param TranslatorInterface $translator |
||
52 | * @param SecurityFacade $securityFacade |
||
53 | */ |
||
54 | public function __construct( |
||
69 | |||
70 | /** |
||
71 | * @param WidgetOptionBag $widgetOptions |
||
72 | * @param string $getterName |
||
73 | * @param string $dataType |
||
74 | * @param bool $lessIsBetter |
||
75 | * @return array |
||
76 | */ |
||
77 | public function getForecastOfOpportunitiesValues( |
||
109 | |||
110 | /** |
||
111 | * Prepare Default Options for User and business Units |
||
112 | * |
||
113 | * @param WidgetOptionBag $widgetOptions |
||
114 | * |
||
115 | * @return bool |
||
116 | */ |
||
117 | protected function prepareDefaultOptions($widgetOptions) |
||
139 | |||
140 | /** |
||
141 | * @param array $ownerIds |
||
142 | * @param null $compareToDate |
||
143 | * @return int |
||
144 | */ |
||
145 | protected function getInProgressValues($ownerIds, $compareToDate = null) |
||
151 | |||
152 | /** |
||
153 | * @param array $ownerIds |
||
154 | * @param null $compareToDate |
||
155 | * @return int |
||
156 | */ |
||
157 | protected function getTotalForecastValues($ownerIds, $compareToDate = null) |
||
163 | |||
164 | /** |
||
165 | * @param array $ownerIds |
||
166 | * @param null $compareToDate |
||
167 | * @return int |
||
168 | */ |
||
169 | protected function getWeightedForecastValues($ownerIds, $compareToDate = null) |
||
175 | |||
176 | /** |
||
177 | * @param array $ownerIds |
||
178 | * @param \DateTime|string|int $date |
||
179 | * @return mixed |
||
180 | */ |
||
181 | protected function getOwnersValues(array $ownerIds, $date) |
||
192 | |||
193 | /** |
||
194 | * @param mixed $value |
||
195 | * @param string $type |
||
196 | * @param bool $isDeviant |
||
197 | * |
||
198 | * @return string |
||
199 | */ |
||
200 | protected function formatValue($value, $type = '', $isDeviant = false) |
||
227 | |||
228 | /** |
||
229 | * @param $dataType |
||
230 | * @param $lessIsBetter |
||
231 | * @param $pastResult |
||
232 | * @param $deviation |
||
233 | * @param $result |
||
234 | * |
||
235 | * @return array |
||
236 | */ |
||
237 | protected function prepareData($dataType, $lessIsBetter, $pastResult, $deviation, $result) |
||
266 | |||
267 | /** |
||
268 | * @param WidgetOptionBag $widgetOptions |
||
269 | * |
||
270 | * @return array |
||
271 | */ |
||
272 | protected function getOwnerIds(WidgetOptionBag $widgetOptions) |
||
304 | |||
305 | /** |
||
306 | * @param WidgetOptionBag $widgetOptions |
||
307 | * |
||
308 | * @return array |
||
309 | */ |
||
310 | protected function getBusinessUnitsIds(WidgetOptionBag $widgetOptions) |
||
326 | |||
327 | /** |
||
328 | * @return UserInterface |
||
329 | */ |
||
330 | protected function getCurrentUser() |
||
341 | } |
||
342 |
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.