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 |
||
| 67 | class Form implements Renderable |
||
| 68 | { |
||
| 69 | use BaseForm\Concerns\HandleCascadeFields; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The title of form. |
||
| 73 | * |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | public $title; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * The description of form. |
||
| 80 | * |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | public $description; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var Field[] |
||
| 87 | */ |
||
| 88 | protected $fields = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | protected $data = []; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | protected $attributes = []; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Available buttons. |
||
| 102 | * |
||
| 103 | * @var array |
||
| 104 | */ |
||
| 105 | protected $buttons = ['reset', 'submit']; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Width for label and submit field. |
||
| 109 | * |
||
| 110 | * @var array |
||
| 111 | */ |
||
| 112 | protected $width = [ |
||
| 113 | 'label' => 2, |
||
| 114 | 'field' => 8, |
||
| 115 | ]; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var bool |
||
| 119 | */ |
||
| 120 | public $inbox = true; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var string |
||
| 124 | */ |
||
| 125 | public $confirm = ''; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var Form |
||
| 129 | */ |
||
| 130 | protected $form; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Form constructor. |
||
| 134 | * |
||
| 135 | * @param array $data |
||
| 136 | */ |
||
| 137 | public function __construct($data = []) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get form title. |
||
| 146 | * |
||
| 147 | * @return mixed |
||
| 148 | */ |
||
| 149 | public function title() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Get form description. |
||
| 156 | * |
||
| 157 | * @return mixed |
||
| 158 | */ |
||
| 159 | public function description() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @return array |
||
| 166 | */ |
||
| 167 | public function data() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @return array |
||
| 174 | */ |
||
| 175 | public function confirm($message) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Fill data to form fields. |
||
| 184 | * |
||
| 185 | * @param array $data |
||
| 186 | * |
||
| 187 | * @return $this |
||
| 188 | */ |
||
| 189 | public function fill($data = []) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @return $this |
||
| 204 | */ |
||
| 205 | public function sanitize() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Initialize the form attributes. |
||
| 216 | */ |
||
| 217 | protected function initFormAttributes() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Add form attributes. |
||
| 231 | * |
||
| 232 | * @param string|array $attr |
||
| 233 | * @param string $value |
||
| 234 | * |
||
| 235 | * @return $this |
||
| 236 | */ |
||
| 237 | public function attribute($attr, $value = '') |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Format form attributes form array to html. |
||
| 252 | * |
||
| 253 | * @param array $attributes |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | public function formatAttribute($attributes = []) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Action uri of the form. |
||
| 275 | * |
||
| 276 | * @param string $action |
||
| 277 | * |
||
| 278 | * @return $this |
||
| 279 | */ |
||
| 280 | public function action($action) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Method of the form. |
||
| 287 | * |
||
| 288 | * @param string $method |
||
| 289 | * |
||
| 290 | * @return $this |
||
| 291 | */ |
||
| 292 | public function method($method = 'POST') |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Disable Pjax. |
||
| 305 | * |
||
| 306 | * @return $this |
||
| 307 | */ |
||
| 308 | public function disablePjax() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Disable reset button. |
||
| 317 | * |
||
| 318 | * @return $this |
||
| 319 | */ |
||
| 320 | public function disableReset() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Disable submit button. |
||
| 329 | * |
||
| 330 | * @return $this |
||
| 331 | */ |
||
| 332 | public function disableSubmit() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Set field and label width in current form. |
||
| 341 | * |
||
| 342 | * @param int $fieldWidth |
||
| 343 | * @param int $labelWidth |
||
| 344 | * |
||
| 345 | * @return $this |
||
| 346 | */ |
||
| 347 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Determine if the form has field type. |
||
| 365 | * |
||
| 366 | * @param string $name |
||
| 367 | * |
||
| 368 | * @return bool |
||
| 369 | */ |
||
| 370 | public function hasField($name) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Add a form field to form. |
||
| 377 | * |
||
| 378 | * @param Field $field |
||
| 379 | * |
||
| 380 | * @return $this |
||
| 381 | */ |
||
| 382 | public function pushField(Field $field) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Get all fields of form. |
||
| 393 | * |
||
| 394 | * @return Field[] |
||
| 395 | */ |
||
| 396 | public function fields() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Get variables for render form. |
||
| 403 | * |
||
| 404 | * @return array |
||
| 405 | */ |
||
| 406 | protected function getVariables() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Determine if form fields has files. |
||
| 421 | * |
||
| 422 | * @return bool |
||
| 423 | */ |
||
| 424 | public function hasFile() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Validate this form fields. |
||
| 437 | * |
||
| 438 | * @param Request $request |
||
| 439 | * |
||
| 440 | * @return bool|MessageBag |
||
| 441 | */ |
||
| 442 | public function validate(Request $request) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Merge validation messages from input validators. |
||
| 468 | * |
||
| 469 | * @param \Illuminate\Validation\Validator[] $validators |
||
| 470 | * |
||
| 471 | * @return MessageBag |
||
| 472 | */ |
||
| 473 | protected function mergeValidationMessages($validators) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Add a fieldset to form. |
||
| 486 | * |
||
| 487 | * @param string $title |
||
| 488 | * @param Closure $setCallback |
||
| 489 | * |
||
| 490 | * @return Field\Fieldset |
||
| 491 | */ |
||
| 492 | View Code Duplication | public function fieldset(string $title, Closure $setCallback) |
|
| 504 | |||
| 505 | /** |
||
| 506 | * @return $this |
||
| 507 | */ |
||
| 508 | public function unbox() |
||
| 514 | |||
| 515 | protected function addConfirmScript() |
||
| 551 | |||
| 552 | protected function addCascadeScript() |
||
| 567 | |||
| 568 | protected function prepareForm() |
||
| 580 | |||
| 581 | protected function prepareHandle() |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Render the form. |
||
| 592 | * |
||
| 593 | * @return string |
||
| 594 | */ |
||
| 595 | public function render() |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Generate a Field object and add to form builder if Field exists. |
||
| 612 | * |
||
| 613 | * @param string $method |
||
| 614 | * @param array $arguments |
||
| 615 | * |
||
| 616 | * @return Field|$this |
||
| 617 | */ |
||
| 618 | public function __call($method, $arguments) |
||
| 632 | |||
| 633 | /** |
||
| 634 | * @param Content $content |
||
| 635 | * @return Content |
||
| 636 | */ |
||
| 637 | public function __invoke(Content $content) |
||
| 643 | } |
||
| 644 |
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: