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 |
||
| 8 | abstract class Form { |
||
| 9 | |||
| 10 | /** |
||
| 11 | * The request implementation used by the form. |
||
| 12 | * |
||
| 13 | * @var \Helmut\Forms\Request |
||
| 14 | */ |
||
| 15 | protected $request; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The renderer used by the form. |
||
| 19 | * |
||
| 20 | * @var \Helmut\Forms\Renderer |
||
| 21 | */ |
||
| 22 | protected $renderer; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The unique id of the form. |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | public $id; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The action for the form. |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | public $action = ''; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Array containing all the form fields. |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $fields = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Array of button keys. |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $buttons = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Array of models. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $models = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The active template package. |
||
| 61 | * |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected $template; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The active language used. |
||
| 68 | * |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | protected $lang = 'en'; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * A cache of language translations. |
||
| 75 | * |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $translations = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * An array of plugins. |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected $plugins = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * An array of namespaces for class loading. |
||
| 89 | * |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | protected $namespaces = []; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * An array of paths for autoloading. |
||
| 96 | * |
||
| 97 | * @var array |
||
| 98 | */ |
||
| 99 | protected $paths = []; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * The static instantiation counter. Used |
||
| 103 | * to make sure each form implementation can |
||
| 104 | * have a unique id. |
||
| 105 | * |
||
| 106 | * @var integer |
||
| 107 | */ |
||
| 108 | protected static $count = 0; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Create a new form. |
||
| 112 | * |
||
| 113 | * @param \Helmut\Forms\Request $request |
||
| 114 | * @param \Helmut\Forms\Renderer $renderer |
||
| 115 | */ |
||
| 116 | public function __construct(Request $request = null, Renderer $renderer = null) |
||
| 127 | 134 | ||
| 128 | 134 | /** |
|
| 129 | 134 | * Load namespaces. |
|
| 130 | 134 | * |
|
| 131 | * @return void |
||
| 132 | */ |
||
| 133 | public function loadNamespaces() |
||
| 149 | 134 | ||
| 150 | 134 | /** |
|
| 151 | * Set a unique id. |
||
| 152 | 134 | * |
|
| 153 | * @return void |
||
| 154 | */ |
||
| 155 | public function setId() |
||
| 161 | 134 | ||
| 162 | 134 | /** |
|
| 163 | 134 | * Load form defaults |
|
| 164 | 134 | * |
|
| 165 | * @return void |
||
| 166 | */ |
||
| 167 | public function loadDefaults() |
||
| 173 | 134 | ||
| 174 | 134 | /** |
|
| 175 | * Trigger the definition. |
||
| 176 | 134 | * |
|
| 177 | * @return void |
||
| 178 | */ |
||
| 179 | public function triggerDefinition() |
||
| 187 | |||
| 188 | /** |
||
| 189 | 134 | * Add a new field. |
|
| 190 | 134 | * |
|
| 191 | * @param string $type |
||
| 192 | * @param string $name |
||
| 193 | * @return \Helmut\Forms\Fields\Field |
||
| 194 | */ |
||
| 195 | public function addField($type, $name) |
||
| 207 | 118 | ||
| 208 | /** |
||
| 209 | 118 | * Set all the field values. |
|
| 210 | * |
||
| 211 | * @return void |
||
| 212 | */ |
||
| 213 | public function setValues() |
||
| 231 | 111 | ||
| 232 | /** |
||
| 233 | * Add default models to the form. |
||
| 234 | 113 | * |
|
| 235 | * @return void |
||
| 236 | */ |
||
| 237 | public function defaults() |
||
| 246 | 9 | ||
| 247 | 9 | /** |
|
| 248 | * Fetch all fields or just those matching |
||
| 249 | * the names provided. |
||
| 250 | 9 | * |
|
| 251 | * @param array $names |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | public function fields($names = null) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get a field matching name. |
||
| 265 | * |
||
| 266 | * @param string $name |
||
| 267 | * @return \Helmut\Forms\Field |
||
| 268 | */ |
||
| 269 | public function field($name) |
||
| 275 | |||
| 276 | 26 | /** |
|
| 277 | 26 | * Fetch all of the field keys. |
|
| 278 | * |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | public function keys() |
||
| 291 | 1 | ||
| 292 | /** |
||
| 293 | * Fetch all of the field values. |
||
| 294 | 1 | * |
|
| 295 | * @return array |
||
| 296 | */ |
||
| 297 | public function all() |
||
| 309 | 1 | ||
| 310 | /** |
||
| 311 | * Fetch the value of a field. |
||
| 312 | 1 | * |
|
| 313 | * @param string $name |
||
| 314 | * @param string $key |
||
| 315 | * @return array |
||
| 316 | */ |
||
| 317 | public function get($name, $key = null) |
||
| 327 | |||
| 328 | 25 | /** |
|
| 329 | 25 | * Fill a model with values. |
|
| 330 | * |
||
| 331 | * @param object $model |
||
| 332 | * @param array $names |
||
| 333 | */ |
||
| 334 | public function fill($model, $names = null) |
||
| 344 | |||
| 345 | 13 | /** |
|
| 346 | 13 | * Check if a form has been submitted and valid in one |
|
| 347 | * quick and easy step. |
||
| 348 | 13 | * |
|
| 349 | * @param string $name |
||
| 350 | * @return bool |
||
| 351 | */ |
||
| 352 | public function completed($name = null) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Check if a form has been submitted. If no specific name |
||
| 363 | * is requested check for any button. |
||
| 364 | * |
||
| 365 | * @param string $name |
||
| 366 | * @return bool |
||
| 367 | */ |
||
| 368 | public function submitted($name = null) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Add a button. |
||
| 384 | 108 | * |
|
| 385 | * @param string $name |
||
| 386 | * @return void |
||
| 387 | */ |
||
| 388 | public function addButton($name) |
||
| 392 | |||
| 393 | 134 | /** |
|
| 394 | * Add a new namespace. |
||
| 395 | 134 | * |
|
| 396 | 134 | * @param string $namespace |
|
| 397 | * @return void |
||
| 398 | */ |
||
| 399 | public function addNamespace($namespace) |
||
| 405 | |||
| 406 | 134 | /** |
|
| 407 | 134 | * Get namespaces. |
|
| 408 | * |
||
| 409 | 134 | * @return array |
|
| 410 | */ |
||
| 411 | public function namespaces() |
||
| 415 | |||
| 416 | 2 | /** |
|
| 417 | * Add a new autoload path. |
||
| 418 | 2 | * |
|
| 419 | * @param string $path |
||
| 420 | * @return void |
||
| 421 | */ |
||
| 422 | public function addPath($path) |
||
| 430 | |||
| 431 | 134 | /** |
|
| 432 | 134 | * Find and add a new namespace using a class. |
|
| 433 | * |
||
| 434 | 134 | * @param string $class |
|
| 435 | * @return void |
||
| 436 | */ |
||
| 437 | public function addNamespaceForClass($class) |
||
| 451 | |||
| 452 | 134 | /** |
|
| 453 | 134 | * Check if a type exists. |
|
| 454 | * |
||
| 455 | * @param string $type |
||
| 456 | * @return string |
||
| 457 | */ |
||
| 458 | public function typeExists($type) |
||
| 462 | |||
| 463 | 118 | /** |
|
| 464 | * Convert a string type to full class name. |
||
| 465 | * |
||
| 466 | * @param string $type |
||
| 467 | * @return string |
||
| 468 | */ |
||
| 469 | public function typeToClass($type) |
||
| 479 | 118 | ||
| 480 | /** |
||
| 481 | 10 | * Get the validation errors. With option to |
|
| 482 | * get only for a specific field. |
||
| 483 | * |
||
| 484 | * @param string $name |
||
| 485 | * @return array |
||
| 486 | */ |
||
| 487 | public function errors($name = null) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Perform validation on the form. |
||
| 506 | * |
||
| 507 | * @return void |
||
| 508 | */ |
||
| 509 | public function validate() |
||
| 521 | |||
| 522 | 28 | /** |
|
| 523 | 28 | * Perform validation on the form. With option |
|
| 524 | * to check if a particular field is valid. |
||
| 525 | * |
||
| 526 | * @param string $name |
||
| 527 | * @return bool |
||
| 528 | */ |
||
| 529 | public function valid($name = null) |
||
| 546 | 27 | ||
| 547 | 27 | /** |
|
| 548 | * The opposite of valid. |
||
| 549 | * |
||
| 550 | * @param string $name |
||
| 551 | * @return bool |
||
| 552 | */ |
||
| 553 | public function invalid($name = null) |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Set the active language. |
||
| 560 | * |
||
| 561 | * @param string $lang |
||
| 562 | * @return void |
||
| 563 | */ |
||
| 564 | public function setLanguage($lang) |
||
| 568 | |||
| 569 | 1 | /** |
|
| 570 | 1 | * Set the template package. |
|
| 571 | * |
||
| 572 | * @param string $template |
||
| 573 | * @return void |
||
| 574 | */ |
||
| 575 | public function setTemplate($template) |
||
| 585 | |||
| 586 | 134 | /** |
|
| 587 | 134 | * Get template configuration. |
|
| 588 | * |
||
| 589 | * @param string $template |
||
| 590 | * @return array |
||
| 591 | */ |
||
| 592 | public function templateConfig($template) |
||
| 607 | |||
| 608 | 4 | /** |
|
| 609 | * Install template configuration. |
||
| 610 | * |
||
| 611 | * @param string $template |
||
| 612 | * @return void |
||
| 613 | */ |
||
| 614 | public function installTemplate($template) |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Uninstall template configuration. |
||
| 631 | * |
||
| 632 | * @param string $template |
||
| 633 | * @return void |
||
| 634 | 4 | */ |
|
| 635 | public function uninstallTemplate($template) |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Fetch a fields properties. |
||
| 648 | * |
||
| 649 | * @param \Helmut\Forms\Field $field |
||
| 650 | * @return string |
||
| 651 | 45 | */ |
|
| 652 | public function fieldProperties($field) |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Render the field using templates. |
||
| 701 | * |
||
| 702 | * @param \Helmut\Forms\Field $field |
||
| 703 | * @param array $properties |
||
| 704 | * @return string |
||
| 705 | 45 | */ |
|
| 706 | public function renderField($field, $properties = null) |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Render the field using templates. |
||
| 715 | * |
||
| 716 | * @param \Helmut\Forms\Field $field |
||
| 717 | * @param array $properties |
||
| 718 | * @return string |
||
| 719 | 45 | */ |
|
| 720 | public function renderFieldErrors($field, $properties = null) |
||
| 726 | |||
| 727 | /** |
||
| 728 | * Render the form using templates. |
||
| 729 | * |
||
| 730 | * @param string $template |
||
| 731 | * @return string |
||
| 732 | 47 | */ |
|
| 733 | public function render($template = null) |
||
| 771 | |||
| 772 | /** |
||
| 773 | * Render a template. |
||
| 774 | * |
||
| 775 | * @param string $template |
||
| 776 | * @param array $properties |
||
| 777 | * @param \Helmut\Forms\Field $field |
||
| 778 | * @return string |
||
| 779 | 47 | */ |
|
| 780 | public function renderTemplate($template, $properties = [], $field = null) |
||
| 802 | |||
| 803 | /** |
||
| 804 | * Translate a specific key. |
||
| 805 | 1 | * |
|
| 806 | * @param string $key |
||
| 807 | 1 | * @param \Helmut\Forms\Field $field |
|
| 808 | * @return string |
||
| 809 | 1 | */ |
|
| 810 | 1 | public function translate($key, $field = null) |
|
| 820 | |||
| 821 | /** |
||
| 822 | 47 | * Get translations and cache them if necessary. |
|
| 823 | * |
||
| 824 | 47 | * @param \Helmut\Forms\Field $field |
|
| 825 | * @return array |
||
| 826 | 47 | */ |
|
| 827 | 47 | public function translations($field = null) |
|
| 859 | 47 | ||
| 860 | /** |
||
| 861 | 47 | * Load the translations from paths. |
|
| 862 | * |
||
| 863 | 47 | * @param string $lang |
|
| 864 | 47 | * @param array $paths |
|
| 865 | 47 | * @return array |
|
| 866 | 47 | */ |
|
| 867 | public function loadTranslations($lang, $paths) |
||
| 880 | |||
| 881 | 134 | /** |
|
| 882 | * Add a plugin to a form. |
||
| 883 | 134 | * |
|
| 884 | * @param string $name |
||
| 885 | 134 | * @param array $config |
|
| 886 | * @return \Helmut\Forms\Plugin |
||
| 887 | 134 | */ |
|
| 888 | public function addPlugin($name, $config = []) |
||
| 906 | 134 | ||
| 907 | 134 | /** |
|
| 908 | * Remove a plugin. |
||
| 909 | 134 | * |
|
| 910 | * @param string $name |
||
| 911 | * @return void |
||
| 912 | */ |
||
| 913 | public function removePlugin($name) |
||
| 919 | 134 | ||
| 920 | /** |
||
| 921 | 134 | * Get a plugin instance. |
|
| 922 | * |
||
| 923 | * @param string $name |
||
| 924 | * @return \Helmut\Forms\Plugin |
||
| 925 | */ |
||
| 926 | public function getPlugin($name) |
||
| 930 | 134 | ||
| 931 | /** |
||
| 932 | 134 | * Remove all plugins. |
|
| 933 | 134 | * |
|
| 934 | * @return void |
||
| 935 | 134 | */ |
|
| 936 | public function removeAllPlugins() |
||
| 942 | |||
| 943 | /** |
||
| 944 | 47 | * Broadcast an event. |
|
| 945 | * |
||
| 946 | 47 | * @param string $event |
|
| 947 | * @param array $params |
||
| 948 | * @return void |
||
| 949 | */ |
||
| 950 | private function broadcast($event, $params = []) |
||
| 956 | 53 | ||
| 957 | /** |
||
| 958 | 53 | * Get all paths to the language files or just to those |
|
| 959 | * for a specific field. |
||
| 960 | * |
||
| 961 | * @param \Helmut\Forms\Field $field |
||
| 962 | * @return array |
||
| 963 | */ |
||
| 964 | public function langPaths($field = null) |
||
| 968 | |||
| 969 | 134 | /** |
|
| 970 | * Get all paths to template packages or just to the |
||
| 971 | 134 | * packages for a specific field. |
|
| 972 | * |
||
| 973 | 134 | * @param \Helmut\Forms\Field $field |
|
| 974 | 45 | * @return array |
|
| 975 | */ |
||
| 976 | public function templatePaths($field = null) |
||
| 980 | 134 | ||
| 981 | /** |
||
| 982 | * Get all autoload paths or just the path |
||
| 983 | 134 | * of a specific resource. |
|
| 984 | * |
||
| 985 | * @param \Helmut\Forms\Field $field |
||
| 986 | * @param string $append |
||
| 987 | * @return array |
||
| 988 | */ |
||
| 989 | public function paths($field = null, $append = null) |
||
| 1005 | |||
| 1006 | /** |
||
| 1007 | * Get autoload path for a class. |
||
| 1008 | * |
||
| 1009 | * @param string|object $class |
||
| 1010 | * @return string |
||
| 1011 | */ |
||
| 1012 | public function pathForClass($class) |
||
| 1018 | |||
| 1019 | |||
| 1020 | /** |
||
| 1021 | * Set the form action. |
||
| 1022 | * |
||
| 1023 | * @param string $action |
||
| 1024 | * @return void |
||
| 1025 | */ |
||
| 1026 | public function setAction($action) |
||
| 1030 | |||
| 1031 | 1 | /** |
|
| 1032 | * Get the active template package. |
||
| 1033 | * |
||
| 1034 | * @return string |
||
| 1035 | */ |
||
| 1036 | public function getTemplate() |
||
| 1040 | |||
| 1041 | /** |
||
| 1042 | * Get the request implementation. |
||
| 1043 | * |
||
| 1044 | * @return \Helmut\Forms\Request |
||
| 1045 | */ |
||
| 1046 | public function getRequest() |
||
| 1050 | |||
| 1051 | 134 | /** |
|
| 1052 | 134 | * Get the renderer implementation. |
|
| 1053 | * |
||
| 1054 | * @return \Helmut\Forms\Renderer |
||
| 1055 | 134 | */ |
|
| 1056 | public function getRenderer() |
||
| 1060 | |||
| 1061 | /** |
||
| 1062 | * Get all of the plugins |
||
| 1063 | * |
||
| 1064 | * @return array |
||
| 1065 | */ |
||
| 1066 | public function getPlugins() |
||
| 1070 | |||
| 1071 | /** |
||
| 1072 | * Get all of the namespaces |
||
| 1073 | * |
||
| 1074 | * @return array |
||
| 1075 | */ |
||
| 1076 | 118 | public function getNamespaces() |
|
| 1080 | 118 | ||
| 1081 | 118 | /** |
|
| 1082 | * Requests directly on the object could be trying to |
||
| 1083 | * create a field so check if type exists. |
||
| 1084 | 10 | * |
|
| 1085 | * @param string $method |
||
| 1086 | * @param array $parameters |
||
| 1087 | * @return mixed |
||
| 1088 | */ |
||
| 1089 | public function __call($method, $parameters) |
||
| 1098 | |||
| 1099 | /** |
||
| 1100 | * Render the form. |
||
| 1101 | * |
||
| 1102 | * @return string |
||
| 1103 | */ |
||
| 1104 | public function __toString() |
||
| 1108 | |||
| 1109 | } |
||
| 1110 |