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 Field 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 Field, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Field implements Renderable |
||
| 17 | { |
||
| 18 | use Macroable; |
||
| 19 | |||
| 20 | const FILE_DELETE_FLAG = '_file_del_'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Element id. |
||
| 24 | * |
||
| 25 | * @var array|string |
||
| 26 | */ |
||
| 27 | protected $id; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Element value. |
||
| 31 | * |
||
| 32 | * @var mixed |
||
| 33 | */ |
||
| 34 | protected $value; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Field original value. |
||
| 38 | * |
||
| 39 | * @var mixed |
||
| 40 | */ |
||
| 41 | protected $original; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Field default value. |
||
| 45 | * |
||
| 46 | * @var mixed |
||
| 47 | */ |
||
| 48 | protected $default; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Element label. |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $label = ''; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Column name. |
||
| 59 | * |
||
| 60 | * @var string|array |
||
| 61 | */ |
||
| 62 | protected $column = ''; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Form element name. |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | protected $elementName = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Form element classes. |
||
| 73 | * |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | protected $elementClass = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Variables of elements. |
||
| 80 | * |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | protected $variables = []; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Options for specify elements. |
||
| 87 | * |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | protected $options = []; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Validation rules. |
||
| 94 | * |
||
| 95 | * @var string|\Closure |
||
| 96 | */ |
||
| 97 | protected $rules = ''; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var callable |
||
| 101 | */ |
||
| 102 | protected $validator; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Validation messages. |
||
| 106 | * |
||
| 107 | * @var array |
||
| 108 | */ |
||
| 109 | protected $validationMessages = []; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Css required by this field. |
||
| 113 | * |
||
| 114 | * @var array |
||
| 115 | */ |
||
| 116 | protected static $css = []; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Js required by this field. |
||
| 120 | * |
||
| 121 | * @var array |
||
| 122 | */ |
||
| 123 | protected static $js = []; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Script for field. |
||
| 127 | * |
||
| 128 | * @var string |
||
| 129 | */ |
||
| 130 | protected $script = ''; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Element attributes. |
||
| 134 | * |
||
| 135 | * @var array |
||
| 136 | */ |
||
| 137 | protected $attributes = []; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Parent form. |
||
| 141 | * |
||
| 142 | * @var Form |
||
| 143 | */ |
||
| 144 | protected $form = null; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * View for field to render. |
||
| 148 | * |
||
| 149 | * @var string |
||
| 150 | */ |
||
| 151 | protected $view = ''; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Help block. |
||
| 155 | * |
||
| 156 | * @var array |
||
| 157 | */ |
||
| 158 | protected $help = []; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Key for errors. |
||
| 162 | * |
||
| 163 | * @var mixed |
||
| 164 | */ |
||
| 165 | protected $errorKey; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Placeholder for this field. |
||
| 169 | * |
||
| 170 | * @var string|array |
||
| 171 | */ |
||
| 172 | protected $placeholder; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Width for label and field. |
||
| 176 | * |
||
| 177 | * @var array |
||
| 178 | */ |
||
| 179 | protected $width = [ |
||
| 180 | 'label' => 2, |
||
| 181 | 'field' => 8, |
||
| 182 | ]; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * If the form horizontal layout. |
||
| 186 | * |
||
| 187 | * @var bool |
||
| 188 | */ |
||
| 189 | protected $horizontal = true; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * column data format. |
||
| 193 | * |
||
| 194 | * @var Closure |
||
| 195 | */ |
||
| 196 | protected $customFormat = null; |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @var bool |
||
| 200 | */ |
||
| 201 | protected $display = true; |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @var array |
||
| 205 | */ |
||
| 206 | protected $labelClass = []; |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Field constructor. |
||
| 210 | * |
||
| 211 | * @param $column |
||
| 212 | * @param array $arguments |
||
| 213 | */ |
||
| 214 | public function __construct($column, $arguments = []) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Get assets required by this field. |
||
| 223 | * |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | public static function getAssets() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Format the field element id. |
||
| 236 | * |
||
| 237 | * @param string|array $column |
||
| 238 | * |
||
| 239 | * @return string|array |
||
| 240 | */ |
||
| 241 | protected function formatId($column) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Format the label value. |
||
| 248 | * |
||
| 249 | * @param array $arguments |
||
| 250 | * |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | protected function formatLabel($arguments = []) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Format the name of the field. |
||
| 264 | * |
||
| 265 | * @param string $column |
||
| 266 | * |
||
| 267 | * @return array|mixed|string |
||
| 268 | */ |
||
| 269 | protected function formatName($column) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Set form element name. |
||
| 300 | * |
||
| 301 | * @param string $name |
||
| 302 | * |
||
| 303 | * @return $this |
||
| 304 | * |
||
| 305 | * @author Edwin Hui |
||
| 306 | */ |
||
| 307 | public function setElementName($name) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Fill data to the field. |
||
| 316 | * |
||
| 317 | * @param array $data |
||
| 318 | * |
||
| 319 | * @return void |
||
| 320 | */ |
||
| 321 | public function fill($data) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * custom format form column data when edit. |
||
| 344 | * |
||
| 345 | * @param Closure $call |
||
| 346 | * |
||
| 347 | * @return [null] |
||
|
|
|||
| 348 | */ |
||
| 349 | public function customFormat(\Closure $call) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Set original value to the field. |
||
| 356 | * |
||
| 357 | * @param array $data |
||
| 358 | * |
||
| 359 | * @return void |
||
| 360 | */ |
||
| 361 | public function setOriginal($data) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param Form $form |
||
| 376 | * |
||
| 377 | * @return $this |
||
| 378 | */ |
||
| 379 | public function setForm(Form $form = null) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Set width for field and label. |
||
| 388 | * |
||
| 389 | * @param int $field |
||
| 390 | * @param int $label |
||
| 391 | * |
||
| 392 | * @return $this |
||
| 393 | */ |
||
| 394 | public function setWidth($field = 8, $label = 2) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Set the field options. |
||
| 406 | * |
||
| 407 | * @param array $options |
||
| 408 | * |
||
| 409 | * @return $this |
||
| 410 | */ |
||
| 411 | View Code Duplication | public function options($options = []) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Get or set rules. |
||
| 424 | * |
||
| 425 | * @param null $rules |
||
| 426 | * @param array $messages |
||
| 427 | * |
||
| 428 | * @return $this |
||
| 429 | */ |
||
| 430 | public function rules($rules = null, $messages = []) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Get field validation rules. |
||
| 453 | * |
||
| 454 | * @return string |
||
| 455 | */ |
||
| 456 | protected function getRules() |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Remove a specific rule by keyword. |
||
| 467 | * |
||
| 468 | * @param string $rule |
||
| 469 | * |
||
| 470 | * @return void |
||
| 471 | */ |
||
| 472 | protected function removeRule($rule) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Set field validator. |
||
| 484 | * |
||
| 485 | * @param callable $validator |
||
| 486 | * |
||
| 487 | * @return $this |
||
| 488 | */ |
||
| 489 | public function validator(callable $validator) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Get key for error message. |
||
| 498 | * |
||
| 499 | * @return string |
||
| 500 | */ |
||
| 501 | public function getErrorKey() |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Set key for error message. |
||
| 508 | * |
||
| 509 | * @param string $key |
||
| 510 | * |
||
| 511 | * @return $this |
||
| 512 | */ |
||
| 513 | public function setErrorKey($key) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Set or get value of the field. |
||
| 522 | * |
||
| 523 | * @param null $value |
||
| 524 | * |
||
| 525 | * @return mixed |
||
| 526 | */ |
||
| 527 | View Code Duplication | public function value($value = null) |
|
| 537 | |||
| 538 | /** |
||
| 539 | * Set default value for field. |
||
| 540 | * |
||
| 541 | * @param $default |
||
| 542 | * |
||
| 543 | * @return $this |
||
| 544 | */ |
||
| 545 | public function default($default) |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Get default value. |
||
| 554 | * |
||
| 555 | * @return mixed |
||
| 556 | */ |
||
| 557 | public function getDefault() |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Set help block for current field. |
||
| 568 | * |
||
| 569 | * @param string $text |
||
| 570 | * @param string $icon |
||
| 571 | * |
||
| 572 | * @return $this |
||
| 573 | */ |
||
| 574 | public function help($text = '', $icon = 'fa-info-circle') |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Get column of the field. |
||
| 583 | * |
||
| 584 | * @return string|array |
||
| 585 | */ |
||
| 586 | public function column() |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Get label of the field. |
||
| 593 | * |
||
| 594 | * @return string |
||
| 595 | */ |
||
| 596 | public function label() |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Get original value of the field. |
||
| 603 | * |
||
| 604 | * @return mixed |
||
| 605 | */ |
||
| 606 | public function original() |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Get validator for this field. |
||
| 613 | * |
||
| 614 | * @param array $input |
||
| 615 | * |
||
| 616 | * @return bool|Validator |
||
| 617 | */ |
||
| 618 | public function getValidator(array $input) |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Sanitize input data. |
||
| 657 | * |
||
| 658 | * @param array $input |
||
| 659 | * @param string $column |
||
| 660 | * |
||
| 661 | * @return array |
||
| 662 | */ |
||
| 663 | protected function sanitizeInput($input, $column) |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Add html attributes to elements. |
||
| 675 | * |
||
| 676 | * @param array|string $attribute |
||
| 677 | * @param mixed $value |
||
| 678 | * |
||
| 679 | * @return $this |
||
| 680 | */ |
||
| 681 | public function attribute($attribute, $value = null) |
||
| 691 | |||
| 692 | |||
| 693 | /** |
||
| 694 | * Set the field automatically get focus. |
||
| 695 | * |
||
| 696 | * @return Field |
||
| 697 | */ |
||
| 698 | public function autofocus() |
||
| 702 | |||
| 703 | |||
| 704 | /** |
||
| 705 | * Set the field as readonly mode. |
||
| 706 | * |
||
| 707 | * @return Field |
||
| 708 | */ |
||
| 709 | public function readOnly() |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Set field as disabled. |
||
| 716 | * |
||
| 717 | * @return Field |
||
| 718 | */ |
||
| 719 | public function disable() |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Set field placeholder. |
||
| 726 | * |
||
| 727 | * @param string $placeholder |
||
| 728 | * |
||
| 729 | * @return Field |
||
| 730 | */ |
||
| 731 | public function placeholder($placeholder = '') |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Get placeholder. |
||
| 740 | * |
||
| 741 | * @return string |
||
| 742 | */ |
||
| 743 | public function getPlaceholder() |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Prepare for a field value before update or insert. |
||
| 750 | * |
||
| 751 | * @param $value |
||
| 752 | * |
||
| 753 | * @return mixed |
||
| 754 | */ |
||
| 755 | public function prepare($value) |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Format the field attributes. |
||
| 762 | * |
||
| 763 | * @return string |
||
| 764 | */ |
||
| 765 | protected function formatAttributes() |
||
| 775 | |||
| 776 | /** |
||
| 777 | * @return $this |
||
| 778 | */ |
||
| 779 | public function disableHorizontal() |
||
| 785 | |||
| 786 | /** |
||
| 787 | * @return array |
||
| 788 | */ |
||
| 789 | public function getViewElementClasses() |
||
| 801 | |||
| 802 | /** |
||
| 803 | * Set form element class. |
||
| 804 | * |
||
| 805 | * @param string|array $class |
||
| 806 | * |
||
| 807 | * @return $this |
||
| 808 | */ |
||
| 809 | public function setElementClass($class) |
||
| 815 | |||
| 816 | /** |
||
| 817 | * Get element class. |
||
| 818 | * |
||
| 819 | * @return array |
||
| 820 | */ |
||
| 821 | protected function getElementClass() |
||
| 831 | |||
| 832 | /** |
||
| 833 | * Get element class string. |
||
| 834 | * |
||
| 835 | * @return mixed |
||
| 836 | */ |
||
| 837 | View Code Duplication | protected function getElementClassString() |
|
| 853 | |||
| 854 | /** |
||
| 855 | * Get element class selector. |
||
| 856 | * |
||
| 857 | * @return string |
||
| 858 | */ |
||
| 859 | View Code Duplication | protected function getElementClassSelector() |
|
| 875 | |||
| 876 | /** |
||
| 877 | * Add the element class. |
||
| 878 | * |
||
| 879 | * @param $class |
||
| 880 | * |
||
| 881 | * @return $this |
||
| 882 | */ |
||
| 883 | public function addElementClass($class) |
||
| 893 | |||
| 894 | /** |
||
| 895 | * Remove element class. |
||
| 896 | * |
||
| 897 | * @param $class |
||
| 898 | * |
||
| 899 | * @return $this |
||
| 900 | */ |
||
| 901 | public function removeElementClass($class) |
||
| 917 | |||
| 918 | /** |
||
| 919 | * Add variables to field view. |
||
| 920 | * |
||
| 921 | * @param array $variables |
||
| 922 | * |
||
| 923 | * @return $this |
||
| 924 | */ |
||
| 925 | protected function addVariables(array $variables = []) |
||
| 931 | |||
| 932 | /** |
||
| 933 | * @return string |
||
| 934 | */ |
||
| 935 | public function getLabelClass() |
||
| 940 | |||
| 941 | /** |
||
| 942 | * @param array $labelClass |
||
| 943 | * |
||
| 944 | * @return self |
||
| 945 | */ |
||
| 946 | public function setLabelClass(array $labelClass) |
||
| 953 | |||
| 954 | /** |
||
| 955 | * Get the view variables of this field. |
||
| 956 | * |
||
| 957 | * @return array |
||
| 958 | */ |
||
| 959 | public function variables() |
||
| 975 | |||
| 976 | /** |
||
| 977 | * Get view of this field. |
||
| 978 | * |
||
| 979 | * @return string |
||
| 980 | */ |
||
| 981 | public function getView() |
||
| 991 | |||
| 992 | /** |
||
| 993 | * Get script of current field. |
||
| 994 | * |
||
| 995 | * @return string |
||
| 996 | */ |
||
| 997 | public function getScript() |
||
| 1001 | |||
| 1002 | /** |
||
| 1003 | * Render this filed. |
||
| 1004 | * |
||
| 1005 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 1006 | */ |
||
| 1007 | public function render() |
||
| 1017 | |||
| 1018 | /** |
||
| 1019 | * @return string |
||
| 1020 | */ |
||
| 1021 | public function __toString() |
||
| 1025 | } |
||
| 1026 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.