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 HTMLFormField 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 HTMLFormField, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | abstract class HTMLFormField { |
||
| 8 | public $mParams; |
||
| 9 | |||
| 10 | protected $mValidationCallback; |
||
| 11 | protected $mFilterCallback; |
||
| 12 | protected $mName; |
||
| 13 | protected $mDir; |
||
| 14 | protected $mLabel; # String label, as HTML. Set on construction. |
||
| 15 | protected $mID; |
||
| 16 | protected $mClass = ''; |
||
| 17 | protected $mVFormClass = ''; |
||
| 18 | protected $mHelpClass = false; |
||
| 19 | protected $mDefault; |
||
| 20 | protected $mOptions = false; |
||
| 21 | protected $mOptionsLabelsNotFromMessage = false; |
||
| 22 | protected $mHideIf = null; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var bool If true will generate an empty div element with no label |
||
| 26 | * @since 1.22 |
||
| 27 | */ |
||
| 28 | protected $mShowEmptyLabels = true; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var HTMLForm|null |
||
| 32 | */ |
||
| 33 | public $mParent; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * This function must be implemented to return the HTML to generate |
||
| 37 | * the input object itself. It should not implement the surrounding |
||
| 38 | * table cells/rows, or labels/help messages. |
||
| 39 | * |
||
| 40 | * @param string $value The value to set the input to; eg a default |
||
| 41 | * text for a text input. |
||
| 42 | * |
||
| 43 | * @return string Valid HTML. |
||
| 44 | */ |
||
| 45 | abstract function getInputHTML( $value ); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Same as getInputHTML, but returns an OOUI object. |
||
| 49 | * Defaults to false, which getOOUI will interpret as "use the HTML version" |
||
| 50 | * |
||
| 51 | * @param string $value |
||
| 52 | * @return OOUI\Widget|false |
||
| 53 | */ |
||
| 54 | function getInputOOUI( $value ) { |
||
| 57 | |||
| 58 | /** |
||
| 59 | * True if this field type is able to display errors; false if validation errors need to be |
||
| 60 | * displayed in the main HTMLForm error area. |
||
| 61 | * @return bool |
||
| 62 | */ |
||
| 63 | public function canDisplayErrors() { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Get a translated interface message |
||
| 69 | * |
||
| 70 | * This is a wrapper around $this->mParent->msg() if $this->mParent is set |
||
| 71 | * and wfMessage() otherwise. |
||
| 72 | * |
||
| 73 | * Parameters are the same as wfMessage(). |
||
| 74 | * |
||
| 75 | * @return Message |
||
| 76 | */ |
||
| 77 | function msg() { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * If this field has a user-visible output or not. If not, |
||
| 91 | * it will not be rendered |
||
| 92 | * |
||
| 93 | * @return bool |
||
| 94 | */ |
||
| 95 | public function hasVisibleOutput() { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Fetch a field value from $alldata for the closest field matching a given |
||
| 101 | * name. |
||
| 102 | * |
||
| 103 | * This is complex because it needs to handle array fields like the user |
||
| 104 | * would expect. The general algorithm is to look for $name as a sibling |
||
| 105 | * of $this, then a sibling of $this's parent, and so on. Keeping in mind |
||
| 106 | * that $name itself might be referencing an array. |
||
| 107 | * |
||
| 108 | * @param array $alldata |
||
| 109 | * @param string $name |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | protected function getNearestFieldByName( $alldata, $name ) { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Helper function for isHidden to handle recursive data structures. |
||
| 156 | * |
||
| 157 | * @param array $alldata |
||
| 158 | * @param array $params |
||
| 159 | * @return bool |
||
| 160 | * @throws MWException |
||
| 161 | */ |
||
| 162 | protected function isHiddenRecurse( array $alldata, array $params ) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Test whether this field is supposed to be hidden, based on the values of |
||
| 263 | * the other form fields. |
||
| 264 | * |
||
| 265 | * @since 1.23 |
||
| 266 | * @param array $alldata The data collected from the form |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | function isHidden( $alldata ) { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Override this function if the control can somehow trigger a form |
||
| 279 | * submission that shouldn't actually submit the HTMLForm. |
||
| 280 | * |
||
| 281 | * @since 1.23 |
||
| 282 | * @param string|array $value The value the field was submitted with |
||
| 283 | * @param array $alldata The data collected from the form |
||
| 284 | * |
||
| 285 | * @return bool True to cancel the submission |
||
| 286 | */ |
||
| 287 | function cancelSubmit( $value, $alldata ) { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Override this function to add specific validation checks on the |
||
| 293 | * field input. Don't forget to call parent::validate() to ensure |
||
| 294 | * that the user-defined callback mValidationCallback is still run |
||
| 295 | * |
||
| 296 | * @param string|array $value The value the field was submitted with |
||
| 297 | * @param array $alldata The data collected from the form |
||
| 298 | * |
||
| 299 | * @return bool|string True on success, or String error to display, or |
||
| 300 | * false to fail validation without displaying an error. |
||
| 301 | */ |
||
| 302 | function validate( $value, $alldata ) { |
||
| 320 | |||
| 321 | function filter( $value, $alldata ) { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Should this field have a label, or is there no input element with the |
||
| 331 | * appropriate id for the label to point to? |
||
| 332 | * |
||
| 333 | * @return bool True to output a label, false to suppress |
||
| 334 | */ |
||
| 335 | protected function needsLabel() { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Tell the field whether to generate a separate label element if its label |
||
| 341 | * is blank. |
||
| 342 | * |
||
| 343 | * @since 1.22 |
||
| 344 | * |
||
| 345 | * @param bool $show Set to false to not generate a label. |
||
| 346 | * @return void |
||
| 347 | */ |
||
| 348 | public function setShowEmptyLabel( $show ) { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Can we assume that the request is an attempt to submit a HTMLForm, as opposed to an attempt to |
||
| 354 | * just view it? This can't normally be distinguished for e.g. checkboxes. |
||
| 355 | * |
||
| 356 | * Returns true if the request has a field for a CSRF token (wpEditToken) or a form identifier |
||
| 357 | * (wpFormIdentifier). |
||
| 358 | * |
||
| 359 | * @param WebRequest $request |
||
| 360 | * @return boolean |
||
| 361 | */ |
||
| 362 | protected function isSubmitAttempt( WebRequest $request ) { |
||
| 363 | return $request->getCheck( 'wpEditToken' ) || $request->getCheck( 'wpFormIdentifier' ); |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Get the value that this input has been set to from a posted form, |
||
| 368 | * or the input's default value if it has not been set. |
||
| 369 | * |
||
| 370 | * @param WebRequest $request |
||
| 371 | * @return string The value |
||
| 372 | */ |
||
| 373 | function loadDataFromRequest( $request ) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Initialise the object |
||
| 383 | * |
||
| 384 | * @param array $params Associative Array. See HTMLForm doc for syntax. |
||
| 385 | * |
||
| 386 | * @since 1.22 The 'label' attribute no longer accepts raw HTML, use 'label-raw' instead |
||
| 387 | * @throws MWException |
||
| 388 | */ |
||
| 389 | function __construct( $params ) { |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Get the complete table row for the input, including help text, |
||
| 473 | * labels, and whatever. |
||
| 474 | * |
||
| 475 | * @param string $value The value to set the input to. |
||
| 476 | * |
||
| 477 | * @return string Complete HTML table row. |
||
| 478 | */ |
||
| 479 | function getTableRow( $value ) { |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Get the complete div for the input, including help text, |
||
| 530 | * labels, and whatever. |
||
| 531 | * @since 1.20 |
||
| 532 | * |
||
| 533 | * @param string $value The value to set the input to. |
||
| 534 | * |
||
| 535 | * @return string Complete HTML table row. |
||
| 536 | */ |
||
| 537 | public function getDiv( $value ) { |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Get the OOUI version of the div. Falls back to getDiv by default. |
||
| 580 | * @since 1.26 |
||
| 581 | * |
||
| 582 | * @param string $value The value to set the input to. |
||
| 583 | * |
||
| 584 | * @return OOUI\FieldLayout|OOUI\ActionFieldLayout |
||
| 585 | */ |
||
| 586 | public function getOOUI( $value ) { |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Get label alignment when generating field for OOUI. |
||
| 645 | * @return string 'left', 'right', 'top' or 'inline' |
||
| 646 | */ |
||
| 647 | protected function getLabelAlignOOUI() { |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Get a FieldLayout (or subclass thereof) to wrap this field in when using OOUI output. |
||
| 653 | * @return OOUI\FieldLayout|OOUI\ActionFieldLayout |
||
| 654 | */ |
||
| 655 | protected function getFieldLayoutOOUI( $inputField, $config ) { |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Whether the field should be automatically infused. Note that all OOjs UI HTMLForm fields are |
||
| 665 | * infusable (you can call OO.ui.infuse() on them), but not all are infused by default, since |
||
| 666 | * there is no benefit in doing it e.g. for buttons and it's a small performance hit on page load. |
||
| 667 | * |
||
| 668 | * @return bool |
||
| 669 | */ |
||
| 670 | protected function shouldInfuseOOUI() { |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Get the complete raw fields for the input, including help text, |
||
| 677 | * labels, and whatever. |
||
| 678 | * @since 1.20 |
||
| 679 | * |
||
| 680 | * @param string $value The value to set the input to. |
||
| 681 | * |
||
| 682 | * @return string Complete HTML table row. |
||
| 683 | */ |
||
| 684 | public function getRaw( $value ) { |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Get the complete field for the input, including help text, |
||
| 701 | * labels, and whatever. Fall back from 'vform' to 'div' when not overridden. |
||
| 702 | * |
||
| 703 | * @since 1.25 |
||
| 704 | * @param string $value The value to set the input to. |
||
| 705 | * @return string Complete HTML field. |
||
| 706 | */ |
||
| 707 | public function getVForm( $value ) { |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Get the complete field as an inline element. |
||
| 715 | * @since 1.25 |
||
| 716 | * @param string $value The value to set the input to. |
||
| 717 | * @return string Complete HTML inline element |
||
| 718 | */ |
||
| 719 | public function getInline( $value ) { |
||
| 733 | |||
| 734 | /** |
||
| 735 | * Generate help text HTML in table format |
||
| 736 | * @since 1.20 |
||
| 737 | * |
||
| 738 | * @param string|null $helptext |
||
| 739 | * @return string |
||
| 740 | */ |
||
| 741 | public function getHelpTextHtmlTable( $helptext ) { |
||
| 761 | |||
| 762 | /** |
||
| 763 | * Generate help text HTML in div format |
||
| 764 | * @since 1.20 |
||
| 765 | * |
||
| 766 | * @param string|null $helptext |
||
| 767 | * |
||
| 768 | * @return string |
||
| 769 | */ |
||
| 770 | public function getHelpTextHtmlDiv( $helptext ) { |
||
| 789 | |||
| 790 | /** |
||
| 791 | * Generate help text HTML formatted for raw output |
||
| 792 | * @since 1.20 |
||
| 793 | * |
||
| 794 | * @param string|null $helptext |
||
| 795 | * @return string |
||
| 796 | */ |
||
| 797 | public function getHelpTextHtmlRaw( $helptext ) { |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Determine the help text to display |
||
| 803 | * @since 1.20 |
||
| 804 | * @return string HTML |
||
| 805 | */ |
||
| 806 | public function getHelpText() { |
||
| 832 | |||
| 833 | /** |
||
| 834 | * Determine form errors to display and their classes |
||
| 835 | * @since 1.20 |
||
| 836 | * |
||
| 837 | * @param string $value The value of the input |
||
| 838 | * @return array array( $errors, $errorClass ) |
||
| 839 | */ |
||
| 840 | public function getErrorsAndErrorClass( $value ) { |
||
| 853 | |||
| 854 | /** |
||
| 855 | * Determine form errors to display, returning them in an array. |
||
| 856 | * |
||
| 857 | * @since 1.26 |
||
| 858 | * @param string $value The value of the input |
||
| 859 | * @return string[] Array of error HTML strings |
||
| 860 | */ |
||
| 861 | public function getErrorsRaw( $value ) { |
||
| 879 | |||
| 880 | /** |
||
| 881 | * Determine notices to display for the field. |
||
| 882 | * |
||
| 883 | * @since 1.28 |
||
| 884 | * @return string[] |
||
| 885 | */ |
||
| 886 | function getNotices() { |
||
| 903 | |||
| 904 | /** |
||
| 905 | * @return string HTML |
||
| 906 | */ |
||
| 907 | function getLabel() { |
||
| 910 | |||
| 911 | function getLabelHtml( $cellAttributes = [] ) { |
||
| 949 | |||
| 950 | function getDefault() { |
||
| 957 | |||
| 958 | /** |
||
| 959 | * Returns the attributes required for the tooltip and accesskey. |
||
| 960 | * |
||
| 961 | * @return array Attributes |
||
| 962 | */ |
||
| 963 | public function getTooltipAndAccessKey() { |
||
| 970 | |||
| 971 | /** |
||
| 972 | * Returns the given attributes from the parameters |
||
| 973 | * |
||
| 974 | * @param array $list List of attributes to get |
||
| 975 | * @return array Attributes |
||
| 976 | */ |
||
| 977 | public function getAttributes( array $list ) { |
||
| 993 | |||
| 994 | /** |
||
| 995 | * Given an array of msg-key => value mappings, returns an array with keys |
||
| 996 | * being the message texts. It also forces values to strings. |
||
| 997 | * |
||
| 998 | * @param array $options |
||
| 999 | * @return array |
||
| 1000 | */ |
||
| 1001 | private function lookupOptionsKeys( $options ) { |
||
| 1011 | |||
| 1012 | /** |
||
| 1013 | * Recursively forces values in an array to strings, because issues arise |
||
| 1014 | * with integer 0 as a value. |
||
| 1015 | * |
||
| 1016 | * @param array $array |
||
| 1017 | * @return array |
||
| 1018 | */ |
||
| 1019 | static function forceToStringRecursive( $array ) { |
||
| 1026 | |||
| 1027 | /** |
||
| 1028 | * Fetch the array of options from the field's parameters. In order, this |
||
| 1029 | * checks 'options-messages', 'options', then 'options-message'. |
||
| 1030 | * |
||
| 1031 | * @return array|null Options array |
||
| 1032 | */ |
||
| 1033 | public function getOptions() { |
||
| 1075 | |||
| 1076 | /** |
||
| 1077 | * Get options and make them into arrays suitable for OOUI. |
||
| 1078 | * @return array Options for inclusion in a select or whatever. |
||
| 1079 | */ |
||
| 1080 | public function getOptionsOOUI() { |
||
| 1098 | |||
| 1099 | /** |
||
| 1100 | * flatten an array of options to a single array, for instance, |
||
| 1101 | * a set of "<options>" inside "<optgroups>". |
||
| 1102 | * |
||
| 1103 | * @param array $options Associative Array with values either Strings or Arrays |
||
| 1104 | * @return array Flattened input |
||
| 1105 | */ |
||
| 1106 | public static function flattenOptions( $options ) { |
||
| 1119 | |||
| 1120 | /** |
||
| 1121 | * Formats one or more errors as accepted by field validation-callback. |
||
| 1122 | * |
||
| 1123 | * @param string|Message|array $errors Array of strings or Message instances |
||
| 1124 | * @return string HTML |
||
| 1125 | * @since 1.18 |
||
| 1126 | */ |
||
| 1127 | protected static function formatErrors( $errors ) { |
||
| 1151 | |||
| 1152 | /** |
||
| 1153 | * Turns a *-message parameter (which could be a MessageSpecifier, or a message name, or a |
||
| 1154 | * name + parameters array) into a Message. |
||
| 1155 | * @param mixed $value |
||
| 1156 | * @return Message |
||
| 1157 | */ |
||
| 1158 | protected function getMessage( $value ) { |
||
| 1167 | |||
| 1168 | /** |
||
| 1169 | * Skip this field when collecting data. |
||
| 1170 | * @param WebRequest $request |
||
| 1171 | * @return bool |
||
| 1172 | * @since 1.27 |
||
| 1173 | */ |
||
| 1174 | public function skipLoadData( $request ) { |
||
| 1177 | } |
||
| 1178 |