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 TrendsController 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 TrendsController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class TrendsController extends Controller |
||
18 | { |
||
19 | use ControllerTrait; |
||
20 | |||
21 | /** |
||
22 | * Show a graph of job trends. |
||
23 | * |
||
24 | * @Route("/trends", name="dtc_queue_trends") |
||
25 | * @Template("@DtcQueue/Queue/trends.html.twig") |
||
26 | */ |
||
27 | 1 | public function trendsAction() |
|
28 | { |
||
29 | 1 | $recordTimings = $this->container->getParameter('dtc_queue.timings.record'); |
|
30 | 1 | $params = ['record_timings' => $recordTimings, 'states' => JobTiming::getStates()]; |
|
31 | 1 | $this->addCssJs($params); |
|
32 | |||
33 | 1 | return $params; |
|
34 | } |
||
35 | |||
36 | /** |
||
37 | * @Route("/timings", name="dtc_queue_timings") |
||
38 | */ |
||
39 | 1 | public function getTimingsAction(Request $request) |
|
55 | |||
56 | /** |
||
57 | * @param \DateTime|null $beginDate |
||
58 | * @param \DateTime $endDate |
||
59 | */ |
||
60 | 1 | protected function calculateTimings($type, $beginDate, $endDate) |
|
104 | |||
105 | /** |
||
106 | * Timings offset by timezone if necessary. |
||
107 | * |
||
108 | * @param array $timingsDates |
||
109 | * @param string $format |
||
110 | * |
||
111 | * @return array |
||
112 | */ |
||
113 | 1 | protected function getTimingsDatesAdjusted(array $timingsDates, $format) |
|
131 | |||
132 | 1 | protected function setTimingsData(array $timingStates, array $timings, array $timingsDates, array &$params) |
|
145 | |||
146 | 1 | protected function getJobTimingsOdm($type, \DateTime $end, \DateTime $begin = null) |
|
186 | |||
187 | 1 | protected function getDateFormat($type) |
|
188 | { |
||
189 | switch ($type) { |
||
190 | 1 | case 'YEAR': |
|
191 | 1 | return 'Y'; |
|
192 | 1 | case 'MONTH': |
|
193 | 1 | return 'Y-m'; |
|
194 | 1 | case 'DAY': |
|
195 | 1 | return 'Y-m-d'; |
|
196 | 1 | case 'HOUR': |
|
197 | 1 | return 'Y-m-d H'; |
|
198 | 1 | case 'MINUTE': |
|
199 | 1 | return 'Y-m-d H:i'; |
|
200 | default: |
||
201 | throw new \InvalidArgumentException("Invalid date format type '$type''"); |
||
202 | } |
||
203 | } |
||
204 | |||
205 | 1 | protected function getRegexDate($type) |
|
206 | { |
||
207 | switch ($type) { |
||
208 | 1 | View Code Duplication | case 'YEAR': |
|
|||
209 | 1 | return ['replace' => ['regex' => '(\d+)\-(\d+)\-(\d+)T(\d+):(\d+):(\d+).+$', 'replacement' => '$1'], |
|
210 | 1 | 'interval' => new \DateInterval('P10Y'), ]; |
|
211 | 1 | View Code Duplication | case 'MONTH': |
212 | 1 | return ['replace' => ['regex' => '(\d+)\-(\d+)\-(\d+)T(\d+):(\d+):(\d+).+$', 'replacement' => '$1-$2'], |
|
213 | 1 | 'interval' => new \DateInterval('P12M'), ]; |
|
214 | 1 | View Code Duplication | case 'DAY': |
215 | 1 | return ['replace' => ['regex' => '(\d+)\-(\d+)\-(\d+)T(\d+):(\d+):(\d+).+$', 'replacement' => '$1-$2-$3'], |
|
216 | 1 | 'interval' => new \DateInterval('P31D'), ]; |
|
217 | 1 | View Code Duplication | case 'HOUR': |
218 | 1 | return ['replace' => ['regex' => '(\d+)\-(\d+)\-(\d+)T(\d+):(\d+):(\d+).+$', 'replacement' => '$1-$2-$3 $4'], |
|
219 | 1 | 'interval' => new \DateInterval('PT24H'), ]; |
|
220 | 1 | View Code Duplication | case 'MINUTE': |
221 | 1 | return ['replace' => ['regex' => '(\d+)\-(\d+)\-(\d+)T(\d+):(\d+):(\d+).+$', 'replacement' => '$1-$2-$3 $4:$5'], |
|
222 | 1 | 'interval' => new \DateInterval('PT3600S'), ]; |
|
223 | } |
||
224 | throw new \InvalidArgumentException("Invalid type $type"); |
||
225 | } |
||
226 | |||
227 | 1 | protected function getOrmGroupBy($type) |
|
228 | { |
||
229 | switch ($type) { |
||
230 | 1 | case 'YEAR': |
|
231 | 1 | return ['groupby' => 'YEAR(j.finishedAt)', |
|
232 | 1 | 'interval' => new \DateInterval('P10Y'), ]; |
|
233 | 1 | case 'MONTH': |
|
234 | 1 | return ['groupby' => 'CONCAT(YEAR(j.finishedAt),\'-\',MONTH(j.finishedAt))', |
|
235 | 1 | 'interval' => new \DateInterval('P12M'), ]; |
|
236 | 1 | case 'DAY': |
|
237 | 1 | return ['groupby' => 'CONCAT(YEAR(j.finishedAt),\'-\',MONTH(j.finishedAt),\'-\',DAY(j.finishedAt))', |
|
238 | 1 | 'interval' => new \DateInterval('P31D'), ]; |
|
239 | 1 | case 'HOUR': |
|
240 | 1 | return ['groupby' => 'CONCAT(YEAR(j.finishedAt),\'-\',MONTH(j.finishedAt),\'-\',DAY(j.finishedAt),\' \',HOUR(j.finishedAt))', |
|
241 | 1 | 'interval' => new \DateInterval('PT24H'), ]; |
|
242 | 1 | case 'MINUTE': |
|
243 | 1 | return ['groupby' => 'CONCAT(YEAR(j.finishedAt),\'-\',MONTH(j.finishedAt),\'-\',DAY(j.finishedAt),\' \',HOUR(j.finishedAt),\':\',MINUTE(j.finishedAt))', |
|
244 | 1 | 'interval' => new \DateInterval('PT3600S'), ]; |
|
245 | } |
||
246 | throw new \InvalidArgumentException("Invalid type $type"); |
||
247 | } |
||
248 | |||
249 | 1 | protected function getJobTimingsOrm($type, \DateTime $end, \DateTime $begin = null) |
|
284 | |||
285 | 1 | protected function strPadLeft($str) |
|
289 | |||
290 | 1 | protected function formatOrmDateTime($type, $str) |
|
291 | { |
||
292 | switch ($type) { |
||
316 | } |
||
317 |
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.