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 |
||
| 59 | class Form implements Renderable |
||
| 60 | { |
||
| 61 | /** |
||
| 62 | * @var Field[] |
||
| 63 | */ |
||
| 64 | protected $fields = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | protected $data = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $attributes = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Available buttons. |
||
| 78 | * |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | protected $buttons = ['reset', 'submit']; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Width for label and submit field. |
||
| 85 | * |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | protected $width = [ |
||
| 89 | 'label' => 2, |
||
| 90 | 'field' => 8, |
||
| 91 | ]; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Form constructor. |
||
| 95 | * |
||
| 96 | * @param array $data |
||
| 97 | */ |
||
| 98 | public function __construct($data = []) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @return array |
||
| 107 | */ |
||
| 108 | public function data() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Fill data to form fields. |
||
| 115 | * |
||
| 116 | * @param array $data |
||
| 117 | * @return $this |
||
| 118 | */ |
||
| 119 | public function fill($data = []) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @return $this |
||
| 134 | */ |
||
| 135 | public function sanitize() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Initialize the form attributes. |
||
| 146 | */ |
||
| 147 | protected function initFormAttributes() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Add form attributes. |
||
| 160 | * |
||
| 161 | * @param string|array $attr |
||
| 162 | * @param string $value |
||
| 163 | * |
||
| 164 | * @return $this |
||
| 165 | */ |
||
| 166 | public function attribute($attr, $value = '') |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Format form attributes form array to html. |
||
| 181 | * |
||
| 182 | * @param array $attributes |
||
| 183 | * |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | public function formatAttribute($attributes = []) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Action uri of the form. |
||
| 204 | * |
||
| 205 | * @param string $action |
||
| 206 | * |
||
| 207 | * @return $this |
||
| 208 | */ |
||
| 209 | public function action($action) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Method of the form. |
||
| 216 | * |
||
| 217 | * @param string $method |
||
| 218 | * |
||
| 219 | * @return $this |
||
| 220 | */ |
||
| 221 | public function method($method = 'POST') |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Disable Pjax. |
||
| 234 | * |
||
| 235 | * @return $this |
||
| 236 | */ |
||
| 237 | public function disablePjax() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Disable reset button. |
||
| 246 | * |
||
| 247 | * @return $this |
||
| 248 | */ |
||
| 249 | public function disableReset() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Disable submit button. |
||
| 258 | * |
||
| 259 | * @return $this |
||
| 260 | */ |
||
| 261 | public function disableSubmit() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Set field and label width in current form. |
||
| 270 | * |
||
| 271 | * @param int $fieldWidth |
||
| 272 | * @param int $labelWidth |
||
| 273 | * |
||
| 274 | * @return $this |
||
| 275 | */ |
||
| 276 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Determine if the form has field type. |
||
| 294 | * |
||
| 295 | * @param string $name |
||
| 296 | * |
||
| 297 | * @return bool |
||
| 298 | */ |
||
| 299 | public function hasField($name) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Add a form field to form. |
||
| 306 | * |
||
| 307 | * @param Field $field |
||
| 308 | * |
||
| 309 | * @return $this |
||
| 310 | */ |
||
| 311 | public function pushField(Field &$field) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Get all fields of form. |
||
| 320 | * |
||
| 321 | * @return Field[] |
||
| 322 | */ |
||
| 323 | public function fields() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get variables for render form. |
||
| 330 | * |
||
| 331 | * @return array |
||
| 332 | */ |
||
| 333 | protected function getVariables() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Determine if form fields has files. |
||
| 350 | * |
||
| 351 | * @return bool |
||
| 352 | */ |
||
| 353 | public function hasFile() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Validate this form fields. |
||
| 366 | * |
||
| 367 | * @param Request $request |
||
| 368 | * |
||
| 369 | * @return bool|MessageBag |
||
| 370 | */ |
||
| 371 | public function validate(Request $request) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Merge validation messages from input validators. |
||
| 397 | * |
||
| 398 | * @param \Illuminate\Validation\Validator[] $validators |
||
| 399 | * |
||
| 400 | * @return MessageBag |
||
| 401 | */ |
||
| 402 | protected function mergeValidationMessages($validators) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Render the form. |
||
| 415 | * |
||
| 416 | * @return string |
||
| 417 | */ |
||
| 418 | public function render() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Generate a Field object and add to form builder if Field exists. |
||
| 441 | * |
||
| 442 | * @param string $method |
||
| 443 | * @param array $arguments |
||
| 444 | * |
||
| 445 | * @return Field|$this |
||
| 446 | */ |
||
| 447 | public function __call($method, $arguments) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Output as string. |
||
| 462 | * |
||
| 463 | * @return string |
||
| 464 | */ |
||
| 465 | public function __toString() |
||
| 469 | } |
||
| 470 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: