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:
Complex classes like Form often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Form, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
54 | class Form implements Renderable |
||
55 | { |
||
56 | |||
57 | public static $VALID = "valid"; |
||
58 | |||
59 | /** |
||
60 | * @var Field[] |
||
61 | */ |
||
62 | protected $fields = []; |
||
63 | |||
64 | /** |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $data = []; |
||
68 | |||
69 | /** |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $attributes = []; |
||
73 | |||
74 | /** |
||
75 | * Form constructor. |
||
76 | * |
||
77 | * @param array $data |
||
78 | */ |
||
79 | public function __construct($data = []) |
||
91 | |||
92 | /** |
||
93 | * Initialize the form attributes. |
||
94 | */ |
||
95 | protected function initFormAttributes() |
||
107 | |||
108 | /** |
||
109 | * Action uri of the form. |
||
110 | * |
||
111 | * @param string $action |
||
112 | * |
||
113 | * @return $this |
||
114 | */ |
||
115 | public function action($action) |
||
119 | |||
120 | /** |
||
121 | * Method of the form. |
||
122 | * |
||
123 | * @param string $method |
||
124 | * |
||
125 | * @return $this |
||
126 | */ |
||
127 | public function method($method = 'POST') |
||
131 | |||
132 | /** |
||
133 | * Add form attributes. |
||
134 | * |
||
135 | * @param string|array $attr |
||
136 | * @param string $value |
||
137 | * |
||
138 | * @return $this |
||
139 | */ |
||
140 | public function attribute($attr, $value = '') |
||
152 | |||
153 | /** |
||
154 | * Disable Pjax. |
||
155 | * |
||
156 | * @return $this |
||
157 | */ |
||
158 | public function disablePjax() |
||
164 | |||
165 | /** |
||
166 | * Set field and label width in current form. |
||
167 | * |
||
168 | * @param int $fieldWidth |
||
169 | * @param int $labelWidth |
||
170 | * |
||
171 | * @return $this |
||
172 | */ |
||
173 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
184 | |||
185 | /** |
||
186 | * Find field class with given name. |
||
187 | * |
||
188 | * @param string $method |
||
189 | * |
||
190 | * @return bool|string |
||
191 | */ |
||
192 | View Code Duplication | public static function findFieldClass($method) |
|
202 | |||
203 | /** |
||
204 | * Add a form field to form. |
||
205 | * |
||
206 | * @param Field $field |
||
207 | * |
||
208 | * @return $this |
||
209 | */ |
||
210 | protected function pushField(Field &$field) |
||
216 | |||
217 | /** |
||
218 | * Get variables for render form. |
||
219 | * |
||
220 | * @return array |
||
221 | */ |
||
222 | protected function getVariables() |
||
238 | |||
239 | /** |
||
240 | * Format form attributes form array to html. |
||
241 | * |
||
242 | * @param array $attributes |
||
243 | * |
||
244 | * @return string |
||
245 | */ |
||
246 | public function formatAttribute($attributes = []) |
||
261 | |||
262 | /** |
||
263 | * Determine if form fields has files. |
||
264 | * |
||
265 | * @return bool |
||
266 | */ |
||
267 | public function hasFile() |
||
277 | |||
278 | /** |
||
279 | * Get validation messages. |
||
280 | * |
||
281 | * @param array $input |
||
282 | * |
||
283 | * @return MessageBag|bool |
||
284 | */ |
||
285 | View Code Duplication | protected function validationMessages($input) |
|
303 | |||
304 | /** |
||
305 | * Merge validation messages from input validators. |
||
306 | * |
||
307 | * @param \Illuminate\Validation\Validator[] $validators |
||
308 | * |
||
309 | * @return MessageBag |
||
310 | */ |
||
311 | protected function mergeValidationMessages($validators) |
||
321 | |||
322 | /** |
||
323 | * @return bool|Form|\Illuminate\Http\RedirectResponse |
||
324 | */ |
||
325 | public function validate() |
||
335 | /** |
||
336 | * Collect rules of all fields. |
||
337 | * |
||
338 | * @return array |
||
339 | */ |
||
340 | View Code Duplication | public function getRules() |
|
351 | |||
352 | /** |
||
353 | * Collect validationMessages of all fields. |
||
354 | * |
||
355 | * @return array |
||
356 | */ |
||
357 | View Code Duplication | public function getRuleMessages() |
|
368 | |||
369 | /** |
||
370 | * Generate a Field object and add to form builder if Field exists. |
||
371 | * |
||
372 | * @param string $method |
||
373 | * @param array $arguments |
||
374 | * |
||
375 | * @return Field|null |
||
376 | */ |
||
377 | public function __call($method, $arguments) |
||
389 | |||
390 | /** |
||
391 | * Render the form. |
||
392 | * |
||
393 | * @return string |
||
394 | */ |
||
395 | public function render() |
||
399 | |||
400 | /** |
||
401 | * Output as string. |
||
402 | * |
||
403 | * @return string |
||
404 | */ |
||
405 | public function __toString() |
||
409 | } |
||
410 |
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.