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 |
||
14 | class AppExtension extends AbstractExtension |
||
15 | { |
||
16 | /** |
||
17 | * @return TwigFilter[] |
||
18 | */ |
||
19 | public function getFilters() |
||
29 | |||
30 | /** |
||
31 | * @return TwigFunction[] |
||
32 | */ |
||
33 | public function getFunctions() |
||
39 | |||
40 | /** |
||
41 | * calculate and format difficulty value |
||
42 | * |
||
43 | * @param $number |
||
44 | * |
||
45 | * @return string |
||
46 | */ |
||
47 | View Code Duplication | public function oc_Filter_D($number) |
|
57 | |||
58 | /** |
||
59 | * calculate and format terrain value |
||
60 | * |
||
61 | * @param $number |
||
62 | * |
||
63 | * @return string |
||
64 | */ |
||
65 | View Code Duplication | public function oc_Filter_T($number) |
|
75 | |||
76 | /** |
||
77 | * convert string via ROT13 |
||
78 | * |
||
79 | * @param $string |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | public function oc_Filter_rot13($string) |
||
87 | |||
88 | /** |
||
89 | * convert string via ROT13, but ignore characters in [] brackets |
||
90 | * |
||
91 | * @param $string |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | public function oc_Filter_rot13_gc($string) |
||
99 | } |
||
100 |
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.