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 |
||
8 | class DateTime extends NamedFormElement |
||
9 | { |
||
10 | use \SleepingOwl\Admin\Traits\DatePicker; |
||
11 | |||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $format = 'Y-m-d H:i:s'; |
||
16 | |||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $timezone; |
||
21 | |||
22 | /** |
||
23 | * @var bool |
||
24 | */ |
||
25 | protected $seconds = false; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $view = 'form.element.datetime'; |
||
31 | |||
32 | /** |
||
33 | * @return string |
||
34 | */ |
||
35 | 1 | public function getFormat() |
|
39 | |||
40 | /** |
||
41 | * @return string |
||
42 | */ |
||
43 | public function getTimezone() |
||
51 | |||
52 | /** |
||
53 | * @param string|null $format |
||
54 | * |
||
55 | * @return $this |
||
56 | */ |
||
57 | 1 | public function setFormat($format) |
|
63 | |||
64 | /** |
||
65 | * @param string $timezone |
||
66 | * |
||
67 | * @return $this |
||
68 | */ |
||
69 | public function setTimezone($timezone) |
||
75 | |||
76 | /** |
||
77 | * @return $this|NamedFormElement|mixed|null|string |
||
78 | */ |
||
79 | public function getValueFromModel() |
||
86 | |||
87 | /** |
||
88 | * @return bool |
||
89 | */ |
||
90 | public function hasSeconds() |
||
94 | |||
95 | /** |
||
96 | * @param bool $seconds |
||
97 | * |
||
98 | * @return $this |
||
99 | */ |
||
100 | public function setSeconds($seconds) |
||
106 | |||
107 | /** |
||
108 | * @param mixed $value |
||
109 | * |
||
110 | * @return void |
||
111 | */ |
||
112 | View Code Duplication | public function setModelAttribute($value) |
|
121 | |||
122 | /** |
||
123 | * @return array |
||
124 | */ |
||
125 | public function toArray() |
||
142 | |||
143 | /** |
||
144 | * @return string |
||
145 | */ |
||
146 | 1 | public function getPickerFormat() |
|
150 | |||
151 | /** |
||
152 | * @return $this |
||
153 | * |
||
154 | * SMELLS This function does more than it says. |
||
155 | */ |
||
156 | public function setCurrentDate() |
||
162 | |||
163 | /** |
||
164 | * @param string $value |
||
165 | * |
||
166 | * @return string|void |
||
167 | */ |
||
168 | View Code Duplication | protected function parseValue($value) |
|
194 | } |
||
195 |
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.