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 |
||
| 61 | class Form implements Renderable |
||
| 62 | { |
||
| 63 | /** |
||
| 64 | * The title of form. |
||
| 65 | * |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | public $title; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var Field[] |
||
| 72 | */ |
||
| 73 | protected $fields = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $data = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | protected $attributes = []; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Available buttons. |
||
| 87 | * |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | protected $buttons = ['reset', 'submit']; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Width for label and submit field. |
||
| 94 | * |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | protected $width = [ |
||
| 98 | 'label' => 2, |
||
| 99 | 'field' => 8, |
||
| 100 | ]; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var bool |
||
| 104 | */ |
||
| 105 | public $inbox = true; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Form constructor. |
||
| 109 | * |
||
| 110 | * @param array $data |
||
| 111 | */ |
||
| 112 | public function __construct($data = []) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Get form title. |
||
| 121 | * |
||
| 122 | * @return mixed |
||
| 123 | */ |
||
| 124 | protected function title() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @return array |
||
| 131 | */ |
||
| 132 | public function data() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Fill data to form fields. |
||
| 139 | * |
||
| 140 | * @param array $data |
||
| 141 | * |
||
| 142 | * @return $this |
||
| 143 | */ |
||
| 144 | public function fill($data = []) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return $this |
||
| 159 | */ |
||
| 160 | public function sanitize() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Initialize the form attributes. |
||
| 171 | */ |
||
| 172 | protected function initFormAttributes() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Add form attributes. |
||
| 185 | * |
||
| 186 | * @param string|array $attr |
||
| 187 | * @param string $value |
||
| 188 | * |
||
| 189 | * @return $this |
||
| 190 | */ |
||
| 191 | public function attribute($attr, $value = '') |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Format form attributes form array to html. |
||
| 206 | * |
||
| 207 | * @param array $attributes |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | public function formatAttribute($attributes = []) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Action uri of the form. |
||
| 229 | * |
||
| 230 | * @param string $action |
||
| 231 | * |
||
| 232 | * @return $this |
||
| 233 | */ |
||
| 234 | public function action($action) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Method of the form. |
||
| 241 | * |
||
| 242 | * @param string $method |
||
| 243 | * |
||
| 244 | * @return $this |
||
| 245 | */ |
||
| 246 | public function method($method = 'POST') |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Disable Pjax. |
||
| 259 | * |
||
| 260 | * @return $this |
||
| 261 | */ |
||
| 262 | public function disablePjax() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Disable reset button. |
||
| 271 | * |
||
| 272 | * @return $this |
||
| 273 | */ |
||
| 274 | public function disableReset() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Disable submit button. |
||
| 283 | * |
||
| 284 | * @return $this |
||
| 285 | */ |
||
| 286 | public function disableSubmit() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Set field and label width in current form. |
||
| 295 | * |
||
| 296 | * @param int $fieldWidth |
||
| 297 | * @param int $labelWidth |
||
| 298 | * |
||
| 299 | * @return $this |
||
| 300 | */ |
||
| 301 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Determine if the form has field type. |
||
| 319 | * |
||
| 320 | * @param string $name |
||
| 321 | * |
||
| 322 | * @return bool |
||
| 323 | */ |
||
| 324 | public function hasField($name) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Add a form field to form. |
||
| 331 | * |
||
| 332 | * @param Field $field |
||
| 333 | * |
||
| 334 | * @return $this |
||
| 335 | */ |
||
| 336 | public function pushField(Field &$field) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Get all fields of form. |
||
| 345 | * |
||
| 346 | * @return Field[] |
||
| 347 | */ |
||
| 348 | public function fields() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Get variables for render form. |
||
| 355 | * |
||
| 356 | * @return array |
||
| 357 | */ |
||
| 358 | protected function getVariables() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Determine if form fields has files. |
||
| 375 | * |
||
| 376 | * @return bool |
||
| 377 | */ |
||
| 378 | public function hasFile() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Validate this form fields. |
||
| 391 | * |
||
| 392 | * @param Request $request |
||
| 393 | * |
||
| 394 | * @return bool|MessageBag |
||
| 395 | */ |
||
| 396 | public function validate(Request $request) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Merge validation messages from input validators. |
||
| 422 | * |
||
| 423 | * @param \Illuminate\Validation\Validator[] $validators |
||
| 424 | * |
||
| 425 | * @return MessageBag |
||
| 426 | */ |
||
| 427 | protected function mergeValidationMessages($validators) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Add a fieldset to form. |
||
| 440 | * |
||
| 441 | * @param string $title |
||
| 442 | * @param Closure $setCallback |
||
| 443 | * |
||
| 444 | * @return Field\Fieldset |
||
| 445 | */ |
||
| 446 | View Code Duplication | public function fieldset(string $title, Closure $setCallback) |
|
| 458 | |||
| 459 | public function unbox() |
||
| 465 | |||
| 466 | protected function prepareForm() |
||
| 472 | |||
| 473 | protected function prepareHandle() |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Render the form. |
||
| 484 | * |
||
| 485 | * @return string |
||
| 486 | */ |
||
| 487 | public function render() |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Generate a Field object and add to form builder if Field exists. |
||
| 504 | * |
||
| 505 | * @param string $method |
||
| 506 | * @param array $arguments |
||
| 507 | * |
||
| 508 | * @return Field|$this |
||
| 509 | */ |
||
| 510 | public function __call($method, $arguments) |
||
| 524 | } |
||
| 525 |
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: