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 |
||
| 60 | class Form implements Renderable |
||
| 61 | { |
||
| 62 | /** |
||
| 63 | * The title of form. |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | public $title; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var Field[] |
||
| 71 | */ |
||
| 72 | protected $fields = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | protected $data = []; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | protected $attributes = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Available buttons. |
||
| 86 | * |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | protected $buttons = ['reset', 'submit']; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Width for label and submit field. |
||
| 93 | * |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | protected $width = [ |
||
| 97 | 'label' => 2, |
||
| 98 | 'field' => 8, |
||
| 99 | ]; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Form constructor. |
||
| 103 | * |
||
| 104 | * @param array $data |
||
| 105 | */ |
||
| 106 | public function __construct($data = []) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Get form title. |
||
| 115 | * |
||
| 116 | * @return mixed |
||
| 117 | */ |
||
| 118 | protected function title() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return array |
||
| 125 | */ |
||
| 126 | public function data() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Fill data to form fields. |
||
| 133 | * |
||
| 134 | * @param array $data |
||
| 135 | * @return $this |
||
| 136 | */ |
||
| 137 | public function fill($data = []) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @return $this |
||
| 152 | */ |
||
| 153 | public function sanitize() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Initialize the form attributes. |
||
| 164 | */ |
||
| 165 | protected function initFormAttributes() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Add form attributes. |
||
| 178 | * |
||
| 179 | * @param string|array $attr |
||
| 180 | * @param string $value |
||
| 181 | * |
||
| 182 | * @return $this |
||
| 183 | */ |
||
| 184 | public function attribute($attr, $value = '') |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Format form attributes form array to html. |
||
| 199 | * |
||
| 200 | * @param array $attributes |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | public function formatAttribute($attributes = []) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Action uri of the form. |
||
| 222 | * |
||
| 223 | * @param string $action |
||
| 224 | * |
||
| 225 | * @return $this |
||
| 226 | */ |
||
| 227 | public function action($action) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Method of the form. |
||
| 234 | * |
||
| 235 | * @param string $method |
||
| 236 | * |
||
| 237 | * @return $this |
||
| 238 | */ |
||
| 239 | public function method($method = 'POST') |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Disable Pjax. |
||
| 252 | * |
||
| 253 | * @return $this |
||
| 254 | */ |
||
| 255 | public function disablePjax() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Disable reset button. |
||
| 264 | * |
||
| 265 | * @return $this |
||
| 266 | */ |
||
| 267 | public function disableReset() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Disable submit button. |
||
| 276 | * |
||
| 277 | * @return $this |
||
| 278 | */ |
||
| 279 | public function disableSubmit() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Set field and label width in current form. |
||
| 288 | * |
||
| 289 | * @param int $fieldWidth |
||
| 290 | * @param int $labelWidth |
||
| 291 | * |
||
| 292 | * @return $this |
||
| 293 | */ |
||
| 294 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Determine if the form has field type. |
||
| 312 | * |
||
| 313 | * @param string $name |
||
| 314 | * |
||
| 315 | * @return bool |
||
| 316 | */ |
||
| 317 | public function hasField($name) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Add a form field to form. |
||
| 324 | * |
||
| 325 | * @param Field $field |
||
| 326 | * |
||
| 327 | * @return $this |
||
| 328 | */ |
||
| 329 | public function pushField(Field &$field) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Get all fields of form. |
||
| 338 | * |
||
| 339 | * @return Field[] |
||
| 340 | */ |
||
| 341 | public function fields() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Get variables for render form. |
||
| 348 | * |
||
| 349 | * @return array |
||
| 350 | */ |
||
| 351 | protected function getVariables() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Determine if form fields has files. |
||
| 368 | * |
||
| 369 | * @return bool |
||
| 370 | */ |
||
| 371 | public function hasFile() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Validate this form fields. |
||
| 384 | * |
||
| 385 | * @param Request $request |
||
| 386 | * |
||
| 387 | * @return bool|MessageBag |
||
| 388 | */ |
||
| 389 | public function validate(Request $request) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Merge validation messages from input validators. |
||
| 415 | * |
||
| 416 | * @param \Illuminate\Validation\Validator[] $validators |
||
| 417 | * |
||
| 418 | * @return MessageBag |
||
| 419 | */ |
||
| 420 | protected function mergeValidationMessages($validators) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Render the form. |
||
| 433 | * |
||
| 434 | * @return string |
||
| 435 | */ |
||
| 436 | public function render() |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Generate a Field object and add to form builder if Field exists. |
||
| 461 | * |
||
| 462 | * @param string $method |
||
| 463 | * @param array $arguments |
||
| 464 | * |
||
| 465 | * @return Field|$this |
||
| 466 | */ |
||
| 467 | public function __call($method, $arguments) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Output as string. |
||
| 484 | * |
||
| 485 | * @return string |
||
| 486 | */ |
||
| 487 | public function __toString() |
||
| 491 | } |
||
| 492 |
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: