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 | * Form constructor. |
||
| 104 | * |
||
| 105 | * @param array $data |
||
| 106 | */ |
||
| 107 | public function __construct($data = []) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get form title. |
||
| 116 | * |
||
| 117 | * @return mixed |
||
| 118 | */ |
||
| 119 | protected function title() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | public function data() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Fill data to form fields. |
||
| 134 | * |
||
| 135 | * @param array $data |
||
| 136 | * |
||
| 137 | * @return $this |
||
| 138 | */ |
||
| 139 | public function fill($data = []) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @return $this |
||
| 154 | */ |
||
| 155 | public function sanitize() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Initialize the form attributes. |
||
| 166 | */ |
||
| 167 | protected function initFormAttributes() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Add form attributes. |
||
| 180 | * |
||
| 181 | * @param string|array $attr |
||
| 182 | * @param string $value |
||
| 183 | * |
||
| 184 | * @return $this |
||
| 185 | */ |
||
| 186 | public function attribute($attr, $value = '') |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Format form attributes form array to html. |
||
| 201 | * |
||
| 202 | * @param array $attributes |
||
| 203 | * |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public function formatAttribute($attributes = []) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Action uri of the form. |
||
| 224 | * |
||
| 225 | * @param string $action |
||
| 226 | * |
||
| 227 | * @return $this |
||
| 228 | */ |
||
| 229 | public function action($action) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Method of the form. |
||
| 236 | * |
||
| 237 | * @param string $method |
||
| 238 | * |
||
| 239 | * @return $this |
||
| 240 | */ |
||
| 241 | public function method($method = 'POST') |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Disable Pjax. |
||
| 254 | * |
||
| 255 | * @return $this |
||
| 256 | */ |
||
| 257 | public function disablePjax() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Disable reset button. |
||
| 266 | * |
||
| 267 | * @return $this |
||
| 268 | */ |
||
| 269 | public function disableReset() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Disable submit button. |
||
| 278 | * |
||
| 279 | * @return $this |
||
| 280 | */ |
||
| 281 | public function disableSubmit() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Set field and label width in current form. |
||
| 290 | * |
||
| 291 | * @param int $fieldWidth |
||
| 292 | * @param int $labelWidth |
||
| 293 | * |
||
| 294 | * @return $this |
||
| 295 | */ |
||
| 296 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Determine if the form has field type. |
||
| 314 | * |
||
| 315 | * @param string $name |
||
| 316 | * |
||
| 317 | * @return bool |
||
| 318 | */ |
||
| 319 | public function hasField($name) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Add a form field to form. |
||
| 326 | * |
||
| 327 | * @param Field $field |
||
| 328 | * |
||
| 329 | * @return $this |
||
| 330 | */ |
||
| 331 | public function pushField(Field &$field) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Get all fields of form. |
||
| 340 | * |
||
| 341 | * @return Field[] |
||
| 342 | */ |
||
| 343 | public function fields() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Get variables for render form. |
||
| 350 | * |
||
| 351 | * @return array |
||
| 352 | */ |
||
| 353 | protected function getVariables() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Determine if form fields has files. |
||
| 370 | * |
||
| 371 | * @return bool |
||
| 372 | */ |
||
| 373 | public function hasFile() |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Validate this form fields. |
||
| 386 | * |
||
| 387 | * @param Request $request |
||
| 388 | * |
||
| 389 | * @return bool|MessageBag |
||
| 390 | */ |
||
| 391 | public function validate(Request $request) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Merge validation messages from input validators. |
||
| 417 | * |
||
| 418 | * @param \Illuminate\Validation\Validator[] $validators |
||
| 419 | * |
||
| 420 | * @return MessageBag |
||
| 421 | */ |
||
| 422 | protected function mergeValidationMessages($validators) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Add a fieldset to form. |
||
| 435 | * |
||
| 436 | * @param string $title |
||
| 437 | * @param Closure $setCallback |
||
| 438 | * |
||
| 439 | * @return Field\Fieldset |
||
| 440 | */ |
||
| 441 | View Code Duplication | public function fieldset(string $title, Closure $setCallback) |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Render the form. |
||
| 456 | * |
||
| 457 | * @return string |
||
| 458 | */ |
||
| 459 | public function render() |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Generate a Field object and add to form builder if Field exists. |
||
| 484 | * |
||
| 485 | * @param string $method |
||
| 486 | * @param array $arguments |
||
| 487 | * |
||
| 488 | * @return Field|$this |
||
| 489 | */ |
||
| 490 | public function __call($method, $arguments) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Output as string. |
||
| 507 | * |
||
| 508 | * @return string |
||
| 509 | */ |
||
| 510 | public function __toString() |
||
| 514 | } |
||
| 515 |
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: