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 |
||
11 | class FormerHelper implements Contract |
||
12 | { |
||
13 | /** |
||
14 | * @var View |
||
15 | */ |
||
16 | protected $view; |
||
17 | |||
18 | /** |
||
19 | * @var Translator |
||
20 | */ |
||
21 | protected $translator; |
||
22 | |||
23 | /** |
||
24 | * @var Config |
||
25 | */ |
||
26 | protected $config; |
||
27 | |||
28 | /** |
||
29 | * @var Url |
||
30 | */ |
||
31 | protected $url; |
||
32 | |||
33 | public function __construct(View $view, Translator $translator, Config $config, Url $url) |
||
40 | |||
41 | /** |
||
42 | * Get view path. |
||
43 | * |
||
44 | * @param string|null $view |
||
45 | * @param null $theme |
||
46 | * @return string |
||
47 | */ |
||
48 | public function getViewPath($view = null, $theme = null): string |
||
60 | |||
61 | /** |
||
62 | * Get config value by given key. |
||
63 | * |
||
64 | * @param string $key |
||
65 | * @return mixed |
||
66 | */ |
||
67 | public function config($key) |
||
73 | |||
74 | /** |
||
75 | * Get field class from rules. |
||
76 | * |
||
77 | * @param array $rules |
||
78 | * @return string |
||
79 | */ |
||
80 | View Code Duplication | public function getFieldClassFromRules(array $rules): string |
|
92 | |||
93 | /** |
||
94 | * Get field class from type. |
||
95 | * |
||
96 | * @param string $type |
||
97 | * @return string |
||
98 | */ |
||
99 | View Code Duplication | public function getFieldClassFromType($type): string |
|
109 | |||
110 | /** |
||
111 | * Get given theme or default theme. |
||
112 | * |
||
113 | * @param null|string $theme |
||
114 | * @return string |
||
115 | */ |
||
116 | public function getThemeOrDefault($theme = null): string |
||
120 | |||
121 | /** |
||
122 | * Get url by route name. |
||
123 | * |
||
124 | * @param string $route |
||
125 | * @return string |
||
126 | */ |
||
127 | public function getUrlByRoute($route): string |
||
131 | |||
132 | /** |
||
133 | * Translate a given key. |
||
134 | * |
||
135 | * @param string $key |
||
136 | * @return mixed|null |
||
137 | */ |
||
138 | public function trans($key) |
||
144 | } |
||
145 |
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.