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 |
||
| 62 | class Form implements Renderable |
||
| 63 | { |
||
| 64 | /** |
||
| 65 | * The title of form. |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | public $title; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var Field[] |
||
| 73 | */ |
||
| 74 | protected $fields = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected $data = []; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | protected $attributes = []; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Available buttons. |
||
| 88 | * |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | protected $buttons = ['reset', 'submit']; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Width for label and submit field. |
||
| 95 | * |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | protected $width = [ |
||
| 99 | 'label' => 2, |
||
| 100 | 'field' => 8, |
||
| 101 | ]; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var bool |
||
| 105 | */ |
||
| 106 | public $inbox = true; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var string |
||
| 110 | */ |
||
| 111 | public $confirm = ''; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Form constructor. |
||
| 115 | * |
||
| 116 | * @param array $data |
||
| 117 | */ |
||
| 118 | public function __construct($data = []) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get form title. |
||
| 127 | * |
||
| 128 | * @return mixed |
||
| 129 | */ |
||
| 130 | public function title() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @return array |
||
| 137 | */ |
||
| 138 | public function data() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | public function confirm($message) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Fill data to form fields. |
||
| 155 | * |
||
| 156 | * @param array $data |
||
| 157 | * |
||
| 158 | * @return $this |
||
| 159 | */ |
||
| 160 | public function fill($data = []) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @return $this |
||
| 175 | */ |
||
| 176 | public function sanitize() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Initialize the form attributes. |
||
| 187 | */ |
||
| 188 | protected function initFormAttributes() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Add form attributes. |
||
| 202 | * |
||
| 203 | * @param string|array $attr |
||
| 204 | * @param string $value |
||
| 205 | * |
||
| 206 | * @return $this |
||
| 207 | */ |
||
| 208 | public function attribute($attr, $value = '') |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Format form attributes form array to html. |
||
| 223 | * |
||
| 224 | * @param array $attributes |
||
| 225 | * |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | public function formatAttribute($attributes = []) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Action uri of the form. |
||
| 246 | * |
||
| 247 | * @param string $action |
||
| 248 | * |
||
| 249 | * @return $this |
||
| 250 | */ |
||
| 251 | public function action($action) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Method of the form. |
||
| 258 | * |
||
| 259 | * @param string $method |
||
| 260 | * |
||
| 261 | * @return $this |
||
| 262 | */ |
||
| 263 | public function method($method = 'POST') |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Disable Pjax. |
||
| 276 | * |
||
| 277 | * @return $this |
||
| 278 | */ |
||
| 279 | public function disablePjax() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Disable reset button. |
||
| 288 | * |
||
| 289 | * @return $this |
||
| 290 | */ |
||
| 291 | public function disableReset() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Disable submit button. |
||
| 300 | * |
||
| 301 | * @return $this |
||
| 302 | */ |
||
| 303 | public function disableSubmit() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Set field and label width in current form. |
||
| 312 | * |
||
| 313 | * @param int $fieldWidth |
||
| 314 | * @param int $labelWidth |
||
| 315 | * |
||
| 316 | * @return $this |
||
| 317 | */ |
||
| 318 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Determine if the form has field type. |
||
| 336 | * |
||
| 337 | * @param string $name |
||
| 338 | * |
||
| 339 | * @return bool |
||
| 340 | */ |
||
| 341 | public function hasField($name) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Add a form field to form. |
||
| 348 | * |
||
| 349 | * @param Field $field |
||
| 350 | * |
||
| 351 | * @return $this |
||
| 352 | */ |
||
| 353 | public function pushField(Field &$field) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Get all fields of form. |
||
| 362 | * |
||
| 363 | * @return Field[] |
||
| 364 | */ |
||
| 365 | public function fields() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Get variables for render form. |
||
| 372 | * |
||
| 373 | * @return array |
||
| 374 | */ |
||
| 375 | protected function getVariables() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Determine if form fields has files. |
||
| 390 | * |
||
| 391 | * @return bool |
||
| 392 | */ |
||
| 393 | public function hasFile() |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Validate this form fields. |
||
| 406 | * |
||
| 407 | * @param Request $request |
||
| 408 | * |
||
| 409 | * @return bool|MessageBag |
||
| 410 | */ |
||
| 411 | public function validate(Request $request) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Merge validation messages from input validators. |
||
| 437 | * |
||
| 438 | * @param \Illuminate\Validation\Validator[] $validators |
||
| 439 | * |
||
| 440 | * @return MessageBag |
||
| 441 | */ |
||
| 442 | protected function mergeValidationMessages($validators) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Add a fieldset to form. |
||
| 455 | * |
||
| 456 | * @param string $title |
||
| 457 | * @param Closure $setCallback |
||
| 458 | * |
||
| 459 | * @return Field\Fieldset |
||
| 460 | */ |
||
| 461 | View Code Duplication | public function fieldset(string $title, Closure $setCallback) |
|
| 473 | |||
| 474 | /** |
||
| 475 | * @return $this |
||
| 476 | */ |
||
| 477 | public function unbox() |
||
| 483 | |||
| 484 | protected function addConfirmScript() |
||
| 520 | |||
| 521 | protected function prepareForm() |
||
| 531 | |||
| 532 | protected function prepareHandle() |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Render the form. |
||
| 543 | * |
||
| 544 | * @return string |
||
| 545 | */ |
||
| 546 | public function render() |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Generate a Field object and add to form builder if Field exists. |
||
| 563 | * |
||
| 564 | * @param string $method |
||
| 565 | * @param array $arguments |
||
| 566 | * |
||
| 567 | * @return Field|$this |
||
| 568 | */ |
||
| 569 | public function __call($method, $arguments) |
||
| 583 | } |
||
| 584 |
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: