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 |
||
10 | class FormerHelper implements Contract |
||
11 | { |
||
12 | /** |
||
13 | * @var View |
||
14 | */ |
||
15 | protected $view; |
||
16 | |||
17 | /** |
||
18 | * @var Translator |
||
19 | */ |
||
20 | protected $translator; |
||
21 | |||
22 | /** |
||
23 | * @var Config |
||
24 | */ |
||
25 | protected $config; |
||
26 | |||
27 | public function __construct(View $view, Translator $translator, Config $config) |
||
33 | |||
34 | /** |
||
35 | * Get view path. |
||
36 | * |
||
37 | * @param string|null $view |
||
38 | * @param null $theme |
||
39 | * @return string |
||
40 | */ |
||
41 | public function getViewPath($view = null, $theme = null): string |
||
53 | |||
54 | /** |
||
55 | * Get config value by given key. |
||
56 | * |
||
57 | * @param string $key |
||
58 | * @return mixed |
||
59 | */ |
||
60 | public function config($key) |
||
66 | |||
67 | /** |
||
68 | * Get field class from rules. |
||
69 | * |
||
70 | * @param array $rules |
||
71 | * @return string |
||
72 | */ |
||
73 | public function getFieldClassFromRules(array $rules): string |
||
87 | |||
88 | /** |
||
89 | * Get given theme or default theme. |
||
90 | * |
||
91 | * @param null|string $theme |
||
92 | * @return string |
||
93 | */ |
||
94 | public function getThemeOrDefault($theme = null): string |
||
98 | |||
99 | /** |
||
100 | * Get the label of field by name. |
||
101 | * |
||
102 | * @param string $name |
||
103 | * @return string |
||
104 | */ |
||
105 | public function getLabel($name): string |
||
112 | |||
113 | /** |
||
114 | * Get the placeholder of field by name. |
||
115 | * |
||
116 | * @param string $name |
||
117 | * @return string|null |
||
118 | */ |
||
119 | View Code Duplication | public function getPlaceholder($name) |
|
130 | |||
131 | /** |
||
132 | * Get the text of field by name. |
||
133 | * |
||
134 | * @param string $name |
||
135 | * @return string|null |
||
136 | */ |
||
137 | View Code Duplication | public function getText($name) |
|
147 | } |
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.