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 |
||
53 | class EmbeddedForm |
||
54 | { |
||
55 | /** |
||
56 | * @var Form|WidgetForm |
||
57 | */ |
||
58 | protected $parent = null; |
||
59 | |||
60 | /** |
||
61 | * Fields in form. |
||
62 | * |
||
63 | * @var Collection |
||
64 | */ |
||
65 | protected $fields; |
||
66 | |||
67 | /** |
||
68 | * Original data for this field. |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $original = []; |
||
73 | |||
74 | /** |
||
75 | * Column name for this form. |
||
76 | * |
||
77 | * @var string |
||
78 | */ |
||
79 | protected $column; |
||
80 | |||
81 | /** |
||
82 | * EmbeddedForm constructor. |
||
83 | * |
||
84 | * @param string $column |
||
85 | */ |
||
86 | public function __construct($column) |
||
92 | |||
93 | /** |
||
94 | * Get all fields in current form. |
||
95 | * |
||
96 | * @return Collection |
||
97 | */ |
||
98 | public function fields() |
||
102 | |||
103 | /** |
||
104 | * Set parent form for this form. |
||
105 | * |
||
106 | * @param Form $parent |
||
107 | * |
||
108 | * @return $this |
||
109 | */ |
||
110 | public function setParent(Form $parent) |
||
116 | |||
117 | /** |
||
118 | * Set parent form for this form. |
||
119 | * |
||
120 | * @param WidgetForm $parent |
||
121 | * |
||
122 | * @return $this |
||
123 | */ |
||
124 | public function setParentWidgetForm(WidgetForm $parent) |
||
130 | |||
131 | /** |
||
132 | * Set original values for fields. |
||
133 | * |
||
134 | * @param array $data |
||
135 | * |
||
136 | * @return $this |
||
137 | */ |
||
138 | public function setOriginal($data) |
||
152 | |||
153 | /** |
||
154 | * Prepare for insert or update. |
||
155 | * |
||
156 | * @param array $input |
||
157 | * |
||
158 | * @return mixed |
||
159 | */ |
||
160 | public function prepare($input) |
||
169 | |||
170 | /** |
||
171 | * Do prepare work for each field. |
||
172 | * |
||
173 | * @param string $key |
||
174 | * @param string $record |
||
175 | * |
||
176 | * @return mixed |
||
177 | */ |
||
178 | protected function prepareValue($key, $record) |
||
190 | |||
191 | /** |
||
192 | * Set original data for each field. |
||
193 | * |
||
194 | * @param string $key |
||
195 | * |
||
196 | * @return void |
||
197 | */ |
||
198 | View Code Duplication | protected function setFieldOriginalValue($key) |
|
208 | |||
209 | /** |
||
210 | * Fill data to all fields in form. |
||
211 | * |
||
212 | * @param array $data |
||
213 | * |
||
214 | * @return $this |
||
215 | */ |
||
216 | public function fill(array $data) |
||
224 | |||
225 | /** |
||
226 | * Format form, set `element name` `error key` and `element class`. |
||
227 | * |
||
228 | * @param Field $field |
||
229 | * |
||
230 | * @return Field |
||
231 | */ |
||
232 | protected function formatField(Field $field) |
||
256 | |||
257 | /** |
||
258 | * Add a field to form. |
||
259 | * |
||
260 | * @param Field $field |
||
261 | * |
||
262 | * @return $this |
||
263 | */ |
||
264 | public function pushField(Field $field) |
||
272 | |||
273 | /** |
||
274 | * Add nested-form fields dynamically. |
||
275 | * |
||
276 | * @param string $method |
||
277 | * @param array $arguments |
||
278 | * |
||
279 | * @return Field|$this |
||
280 | */ |
||
281 | View Code Duplication | public function __call($method, $arguments) |
|
302 | } |
||
303 |
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.