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 | * Checked for specify elements. |
||
| 94 | * |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | protected $checked = []; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Validation rules. |
||
| 101 | * |
||
| 102 | * @var string|\Closure |
||
| 103 | */ |
||
| 104 | protected $rules = ''; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var callable |
||
| 108 | */ |
||
| 109 | protected $validator; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Validation messages. |
||
| 113 | * |
||
| 114 | * @var array |
||
| 115 | */ |
||
| 116 | protected $validationMessages = []; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Css required by this field. |
||
| 120 | * |
||
| 121 | * @var array |
||
| 122 | */ |
||
| 123 | protected static $css = []; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Js required by this field. |
||
| 127 | * |
||
| 128 | * @var array |
||
| 129 | */ |
||
| 130 | protected static $js = []; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Script for field. |
||
| 134 | * |
||
| 135 | * @var string |
||
| 136 | */ |
||
| 137 | protected $script = ''; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Element attributes. |
||
| 141 | * |
||
| 142 | * @var array |
||
| 143 | */ |
||
| 144 | protected $attributes = []; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Parent form. |
||
| 148 | * |
||
| 149 | * @var Form |
||
| 150 | */ |
||
| 151 | protected $form = null; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * View for field to render. |
||
| 155 | * |
||
| 156 | * @var string |
||
| 157 | */ |
||
| 158 | protected $view = ''; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Help block. |
||
| 162 | * |
||
| 163 | * @var array |
||
| 164 | */ |
||
| 165 | protected $help = []; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Key for errors. |
||
| 169 | * |
||
| 170 | * @var mixed |
||
| 171 | */ |
||
| 172 | protected $errorKey; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Placeholder for this field. |
||
| 176 | * |
||
| 177 | * @var string|array |
||
| 178 | */ |
||
| 179 | protected $placeholder; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Width for label and field. |
||
| 183 | * |
||
| 184 | * @var array |
||
| 185 | */ |
||
| 186 | protected $width = [ |
||
| 187 | 'label' => 2, |
||
| 188 | 'field' => 8, |
||
| 189 | ]; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * If the form horizontal layout. |
||
| 193 | * |
||
| 194 | * @var bool |
||
| 195 | */ |
||
| 196 | protected $horizontal = true; |
||
| 197 | |||
| 198 | /** |
||
| 199 | * column data format. |
||
| 200 | * |
||
| 201 | * @var \Closure |
||
| 202 | */ |
||
| 203 | protected $customFormat = null; |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @var bool |
||
| 207 | */ |
||
| 208 | protected $display = true; |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @var array |
||
| 212 | */ |
||
| 213 | protected $labelClass = []; |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Field constructor. |
||
| 217 | * |
||
| 218 | * @param $column |
||
| 219 | * @param array $arguments |
||
| 220 | */ |
||
| 221 | public function __construct($column, $arguments = []) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get assets required by this field. |
||
| 230 | * |
||
| 231 | * @return array |
||
| 232 | */ |
||
| 233 | public static function getAssets() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Format the field element id. |
||
| 243 | * |
||
| 244 | * @param string|array $column |
||
| 245 | * |
||
| 246 | * @return string|array |
||
| 247 | */ |
||
| 248 | protected function formatId($column) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Format the label value. |
||
| 255 | * |
||
| 256 | * @param array $arguments |
||
| 257 | * |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | protected function formatLabel($arguments = []) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Format the name of the field. |
||
| 271 | * |
||
| 272 | * @param string $column |
||
| 273 | * |
||
| 274 | * @return array|mixed|string |
||
| 275 | */ |
||
| 276 | protected function formatName($column) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Set form element name. |
||
| 307 | * |
||
| 308 | * @param string $name |
||
| 309 | * |
||
| 310 | * @return $this |
||
| 311 | * |
||
| 312 | * @author Edwin Hui |
||
| 313 | */ |
||
| 314 | public function setElementName($name) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Fill data to the field. |
||
| 323 | * |
||
| 324 | * @param array $data |
||
| 325 | * |
||
| 326 | * @return void |
||
| 327 | */ |
||
| 328 | public function fill($data) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * custom format form column data when edit. |
||
| 378 | * |
||
| 379 | * @param \Closure $call |
||
| 380 | * |
||
| 381 | * @return $this |
||
| 382 | */ |
||
| 383 | public function customFormat(\Closure $call) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Set original value to the field. |
||
| 392 | * |
||
| 393 | * @param array $data |
||
| 394 | * |
||
| 395 | * @return void |
||
| 396 | */ |
||
| 397 | public function setOriginal($data) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @param Form $form |
||
| 412 | * |
||
| 413 | * @return $this |
||
| 414 | */ |
||
| 415 | public function setForm(Form $form = null) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Set width for field and label. |
||
| 424 | * |
||
| 425 | * @param int $field |
||
| 426 | * @param int $label |
||
| 427 | * |
||
| 428 | * @return $this |
||
| 429 | */ |
||
| 430 | public function setWidth($field = 8, $label = 2) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Set the field options. |
||
| 442 | * |
||
| 443 | * @param array $options |
||
| 444 | * |
||
| 445 | * @return $this |
||
| 446 | */ |
||
| 447 | View Code Duplication | public function options($options = []) |
|
| 457 | |||
| 458 | /** |
||
| 459 | * Set the field option checked. |
||
| 460 | * |
||
| 461 | * @param array $checked |
||
| 462 | * |
||
| 463 | * @return $this |
||
| 464 | */ |
||
| 465 | View Code Duplication | public function checked($checked = []) |
|
| 475 | |||
| 476 | /** |
||
| 477 | * Get or set rules. |
||
| 478 | * |
||
| 479 | * @param null $rules |
||
| 480 | * @param array $messages |
||
| 481 | * |
||
| 482 | * @return $this |
||
| 483 | */ |
||
| 484 | public function rules($rules = null, $messages = []) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Get field validation rules. |
||
| 507 | * |
||
| 508 | * @return string |
||
| 509 | */ |
||
| 510 | protected function getRules() |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Remove a specific rule by keyword. |
||
| 521 | * |
||
| 522 | * @param string $rule |
||
| 523 | * |
||
| 524 | * @return void |
||
| 525 | */ |
||
| 526 | protected function removeRule($rule) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Set field validator. |
||
| 538 | * |
||
| 539 | * @param callable $validator |
||
| 540 | * |
||
| 541 | * @return $this |
||
| 542 | */ |
||
| 543 | public function validator(callable $validator) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Get key for error message. |
||
| 552 | * |
||
| 553 | * @return string |
||
| 554 | */ |
||
| 555 | public function getErrorKey() |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Set key for error message. |
||
| 562 | * |
||
| 563 | * @param string $key |
||
| 564 | * |
||
| 565 | * @return $this |
||
| 566 | */ |
||
| 567 | public function setErrorKey($key) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Set or get value of the field. |
||
| 576 | * |
||
| 577 | * @param null $value |
||
| 578 | * |
||
| 579 | * @return mixed |
||
| 580 | */ |
||
| 581 | View Code Duplication | public function value($value = null) |
|
| 591 | |||
| 592 | /** |
||
| 593 | * Set default value for field. |
||
| 594 | * |
||
| 595 | * @param $default |
||
| 596 | * |
||
| 597 | * @return $this |
||
| 598 | */ |
||
| 599 | public function default($default) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Get default value. |
||
| 608 | * |
||
| 609 | * @return mixed |
||
| 610 | */ |
||
| 611 | public function getDefault() |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Set help block for current field. |
||
| 622 | * |
||
| 623 | * @param string $text |
||
| 624 | * @param string $icon |
||
| 625 | * |
||
| 626 | * @return $this |
||
| 627 | */ |
||
| 628 | public function help($text = '', $icon = 'fa-info-circle') |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Get column of the field. |
||
| 637 | * |
||
| 638 | * @return string|array |
||
| 639 | */ |
||
| 640 | public function column() |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Get label of the field. |
||
| 647 | * |
||
| 648 | * @return string |
||
| 649 | */ |
||
| 650 | public function label() |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Get original value of the field. |
||
| 657 | * |
||
| 658 | * @return mixed |
||
| 659 | */ |
||
| 660 | public function original() |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Get validator for this field. |
||
| 667 | * |
||
| 668 | * @param array $input |
||
| 669 | * |
||
| 670 | * @return bool|Validator |
||
| 671 | */ |
||
| 672 | public function getValidator(array $input) |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Sanitize input data. |
||
| 711 | * |
||
| 712 | * @param array $input |
||
| 713 | * @param string $column |
||
| 714 | * |
||
| 715 | * @return array |
||
| 716 | */ |
||
| 717 | protected function sanitizeInput($input, $column) |
||
| 726 | |||
| 727 | /** |
||
| 728 | * Add html attributes to elements. |
||
| 729 | * |
||
| 730 | * @param array|string $attribute |
||
| 731 | * @param mixed $value |
||
| 732 | * |
||
| 733 | * @return $this |
||
| 734 | */ |
||
| 735 | public function attribute($attribute, $value = null) |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Specifies a regular expression against which to validate the value of the input. |
||
| 748 | * |
||
| 749 | * @param string $regexp |
||
| 750 | * |
||
| 751 | * @return Field |
||
| 752 | */ |
||
| 753 | public function pattern($regexp) |
||
| 757 | |||
| 758 | /** |
||
| 759 | * Set the field automatically get focus. |
||
| 760 | * |
||
| 761 | * @return Field |
||
| 762 | */ |
||
| 763 | public function autofocus() |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Set the field as readonly mode. |
||
| 770 | * |
||
| 771 | * @return Field |
||
| 772 | */ |
||
| 773 | public function readOnly() |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Set field as disabled. |
||
| 780 | * |
||
| 781 | * @return Field |
||
| 782 | */ |
||
| 783 | public function disable() |
||
| 787 | |||
| 788 | /** |
||
| 789 | * Set field placeholder. |
||
| 790 | * |
||
| 791 | * @param string $placeholder |
||
| 792 | * |
||
| 793 | * @return Field |
||
| 794 | */ |
||
| 795 | public function placeholder($placeholder = '') |
||
| 801 | |||
| 802 | /** |
||
| 803 | * Get placeholder. |
||
| 804 | * |
||
| 805 | * @return string |
||
| 806 | */ |
||
| 807 | public function getPlaceholder() |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Prepare for a field value before update or insert. |
||
| 814 | * |
||
| 815 | * @param $value |
||
| 816 | * |
||
| 817 | * @return mixed |
||
| 818 | */ |
||
| 819 | public function prepare($value) |
||
| 823 | |||
| 824 | /** |
||
| 825 | * Format the field attributes. |
||
| 826 | * |
||
| 827 | * @return string |
||
| 828 | */ |
||
| 829 | protected function formatAttributes() |
||
| 839 | |||
| 840 | /** |
||
| 841 | * @return $this |
||
| 842 | */ |
||
| 843 | public function disableHorizontal() |
||
| 849 | |||
| 850 | /** |
||
| 851 | * @return array |
||
| 852 | */ |
||
| 853 | public function getViewElementClasses() |
||
| 865 | |||
| 866 | /** |
||
| 867 | * Set form element class. |
||
| 868 | * |
||
| 869 | * @param string|array $class |
||
| 870 | * |
||
| 871 | * @return $this |
||
| 872 | */ |
||
| 873 | public function setElementClass($class) |
||
| 879 | |||
| 880 | /** |
||
| 881 | * Get element class. |
||
| 882 | * |
||
| 883 | * @return array |
||
| 884 | */ |
||
| 885 | protected function getElementClass() |
||
| 895 | |||
| 896 | /** |
||
| 897 | * Get element class string. |
||
| 898 | * |
||
| 899 | * @return mixed |
||
| 900 | */ |
||
| 901 | View Code Duplication | protected function getElementClassString() |
|
| 917 | |||
| 918 | /** |
||
| 919 | * Get element class selector. |
||
| 920 | * |
||
| 921 | * @return string|array |
||
| 922 | */ |
||
| 923 | View Code Duplication | protected function getElementClassSelector() |
|
| 939 | |||
| 940 | /** |
||
| 941 | * Add the element class. |
||
| 942 | * |
||
| 943 | * @param $class |
||
| 944 | * |
||
| 945 | * @return $this |
||
| 946 | */ |
||
| 947 | public function addElementClass($class) |
||
| 957 | |||
| 958 | /** |
||
| 959 | * Remove element class. |
||
| 960 | * |
||
| 961 | * @param $class |
||
| 962 | * |
||
| 963 | * @return $this |
||
| 964 | */ |
||
| 965 | public function removeElementClass($class) |
||
| 981 | |||
| 982 | /** |
||
| 983 | * Add variables to field view. |
||
| 984 | * |
||
| 985 | * @param array $variables |
||
| 986 | * |
||
| 987 | * @return $this |
||
| 988 | */ |
||
| 989 | protected function addVariables(array $variables = []) |
||
| 995 | |||
| 996 | /** |
||
| 997 | * @return string |
||
| 998 | */ |
||
| 999 | public function getLabelClass() |
||
| 1004 | |||
| 1005 | /** |
||
| 1006 | * @param array $labelClass |
||
| 1007 | * |
||
| 1008 | * @return self |
||
| 1009 | */ |
||
| 1010 | public function setLabelClass(array $labelClass) |
||
| 1017 | |||
| 1018 | /** |
||
| 1019 | * Get the view variables of this field. |
||
| 1020 | * |
||
| 1021 | * @return array |
||
| 1022 | */ |
||
| 1023 | public function variables() |
||
| 1039 | |||
| 1040 | /** |
||
| 1041 | * Get view of this field. |
||
| 1042 | * |
||
| 1043 | * @return string |
||
| 1044 | */ |
||
| 1045 | public function getView() |
||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * Get script of current field. |
||
| 1058 | * |
||
| 1059 | * @return string |
||
| 1060 | */ |
||
| 1061 | public function getScript() |
||
| 1065 | |||
| 1066 | /** |
||
| 1067 | * If this field should render. |
||
| 1068 | * |
||
| 1069 | * @return bool |
||
| 1070 | */ |
||
| 1071 | protected function shouldRender() |
||
| 1079 | |||
| 1080 | /** |
||
| 1081 | * Render this filed. |
||
| 1082 | * |
||
| 1083 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string |
||
| 1084 | */ |
||
| 1085 | public function render() |
||
| 1095 | |||
| 1096 | /** |
||
| 1097 | * @return string |
||
| 1098 | */ |
||
| 1099 | public function __toString() |
||
| 1103 | } |
||
| 1104 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: