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 |
||
| 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 | * Get the value that this input has been set to from a posted form, |
||
| 354 | * or the input's default value if it has not been set. |
||
| 355 | * |
||
| 356 | * @param WebRequest $request |
||
| 357 | * @return string The value |
||
| 358 | */ |
||
| 359 | function loadDataFromRequest( $request ) { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Initialise the object |
||
| 369 | * |
||
| 370 | * @param array $params Associative Array. See HTMLForm doc for syntax. |
||
| 371 | * |
||
| 372 | * @since 1.22 The 'label' attribute no longer accepts raw HTML, use 'label-raw' instead |
||
| 373 | * @throws MWException |
||
| 374 | */ |
||
| 375 | function __construct( $params ) { |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Get the complete table row for the input, including help text, |
||
| 468 | * labels, and whatever. |
||
| 469 | * |
||
| 470 | * @param string $value The value to set the input to. |
||
| 471 | * |
||
| 472 | * @return string Complete HTML table row. |
||
| 473 | */ |
||
| 474 | function getTableRow( $value ) { |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Get the complete div for the input, including help text, |
||
| 525 | * labels, and whatever. |
||
| 526 | * @since 1.20 |
||
| 527 | * |
||
| 528 | * @param string $value The value to set the input to. |
||
| 529 | * |
||
| 530 | * @return string Complete HTML table row. |
||
| 531 | */ |
||
| 532 | public function getDiv( $value ) { |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Get the OOUI version of the div. Falls back to getDiv by default. |
||
| 575 | * @since 1.26 |
||
| 576 | * |
||
| 577 | * @param string $value The value to set the input to. |
||
| 578 | * |
||
| 579 | * @return OOUI\FieldLayout|OOUI\ActionFieldLayout |
||
| 580 | */ |
||
| 581 | public function getOOUI( $value ) { |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Get label alignment when generating field for OOUI. |
||
| 624 | * @return string 'left', 'right', 'top' or 'inline' |
||
| 625 | */ |
||
| 626 | protected function getLabelAlignOOUI() { |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Get a FieldLayout (or subclass thereof) to wrap this field in when using OOUI output. |
||
| 632 | * @return OOUI\FieldLayout|OOUI\ActionFieldLayout |
||
| 633 | */ |
||
| 634 | protected function getFieldLayoutOOUI( $inputField, $config ) { |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Get the complete raw fields for the input, including help text, |
||
| 644 | * labels, and whatever. |
||
| 645 | * @since 1.20 |
||
| 646 | * |
||
| 647 | * @param string $value The value to set the input to. |
||
| 648 | * |
||
| 649 | * @return string Complete HTML table row. |
||
| 650 | */ |
||
| 651 | public function getRaw( $value ) { |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Get the complete field for the input, including help text, |
||
| 668 | * labels, and whatever. Fall back from 'vform' to 'div' when not overridden. |
||
| 669 | * |
||
| 670 | * @since 1.25 |
||
| 671 | * @param string $value The value to set the input to. |
||
| 672 | * @return string Complete HTML field. |
||
| 673 | */ |
||
| 674 | public function getVForm( $value ) { |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Get the complete field as an inline element. |
||
| 682 | * @since 1.25 |
||
| 683 | * @param string $value The value to set the input to. |
||
| 684 | * @return string Complete HTML inline element |
||
| 685 | */ |
||
| 686 | public function getInline( $value ) { |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Generate help text HTML in table format |
||
| 703 | * @since 1.20 |
||
| 704 | * |
||
| 705 | * @param string|null $helptext |
||
| 706 | * @return string |
||
| 707 | */ |
||
| 708 | public function getHelpTextHtmlTable( $helptext ) { |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Generate help text HTML in div format |
||
| 731 | * @since 1.20 |
||
| 732 | * |
||
| 733 | * @param string|null $helptext |
||
| 734 | * |
||
| 735 | * @return string |
||
| 736 | */ |
||
| 737 | public function getHelpTextHtmlDiv( $helptext ) { |
||
| 756 | |||
| 757 | /** |
||
| 758 | * Generate help text HTML formatted for raw output |
||
| 759 | * @since 1.20 |
||
| 760 | * |
||
| 761 | * @param string|null $helptext |
||
| 762 | * @return string |
||
| 763 | */ |
||
| 764 | public function getHelpTextHtmlRaw( $helptext ) { |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Determine the help text to display |
||
| 770 | * @since 1.20 |
||
| 771 | * @return string HTML |
||
| 772 | */ |
||
| 773 | public function getHelpText() { |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Determine form errors to display and their classes |
||
| 803 | * @since 1.20 |
||
| 804 | * |
||
| 805 | * @param string $value The value of the input |
||
| 806 | * @return array array( $errors, $errorClass ) |
||
| 807 | */ |
||
| 808 | public function getErrorsAndErrorClass( $value ) { |
||
| 821 | |||
| 822 | /** |
||
| 823 | * Determine form errors to display, returning them in an array. |
||
| 824 | * |
||
| 825 | * @since 1.26 |
||
| 826 | * @param string $value The value of the input |
||
| 827 | * @return string[] Array of error HTML strings |
||
| 828 | */ |
||
| 829 | public function getErrorsRaw( $value ) { |
||
| 847 | |||
| 848 | /** |
||
| 849 | * @return string HTML |
||
| 850 | */ |
||
| 851 | function getLabel() { |
||
| 854 | |||
| 855 | function getLabelHtml( $cellAttributes = [] ) { |
||
| 893 | |||
| 894 | function getDefault() { |
||
| 901 | |||
| 902 | /** |
||
| 903 | * Returns the attributes required for the tooltip and accesskey. |
||
| 904 | * |
||
| 905 | * @return array Attributes |
||
| 906 | */ |
||
| 907 | public function getTooltipAndAccessKey() { |
||
| 914 | |||
| 915 | /** |
||
| 916 | * Returns the given attributes from the parameters |
||
| 917 | * |
||
| 918 | * @param array $list List of attributes to get |
||
| 919 | * @return array Attributes |
||
| 920 | */ |
||
| 921 | public function getAttributes( array $list ) { |
||
| 937 | |||
| 938 | /** |
||
| 939 | * Given an array of msg-key => value mappings, returns an array with keys |
||
| 940 | * being the message texts. It also forces values to strings. |
||
| 941 | * |
||
| 942 | * @param array $options |
||
| 943 | * @return array |
||
| 944 | */ |
||
| 945 | private function lookupOptionsKeys( $options ) { |
||
| 955 | |||
| 956 | /** |
||
| 957 | * Recursively forces values in an array to strings, because issues arise |
||
| 958 | * with integer 0 as a value. |
||
| 959 | * |
||
| 960 | * @param array $array |
||
| 961 | * @return array |
||
| 962 | */ |
||
| 963 | static function forceToStringRecursive( $array ) { |
||
| 970 | |||
| 971 | /** |
||
| 972 | * Fetch the array of options from the field's parameters. In order, this |
||
| 973 | * checks 'options-messages', 'options', then 'options-message'. |
||
| 974 | * |
||
| 975 | * @return array|null Options array |
||
| 976 | */ |
||
| 977 | public function getOptions() { |
||
| 1019 | |||
| 1020 | /** |
||
| 1021 | * Get options and make them into arrays suitable for OOUI. |
||
| 1022 | * @return array Options for inclusion in a select or whatever. |
||
| 1023 | */ |
||
| 1024 | public function getOptionsOOUI() { |
||
| 1042 | |||
| 1043 | /** |
||
| 1044 | * flatten an array of options to a single array, for instance, |
||
| 1045 | * a set of "<options>" inside "<optgroups>". |
||
| 1046 | * |
||
| 1047 | * @param array $options Associative Array with values either Strings or Arrays |
||
| 1048 | * @return array Flattened input |
||
| 1049 | */ |
||
| 1050 | public static function flattenOptions( $options ) { |
||
| 1063 | |||
| 1064 | /** |
||
| 1065 | * Formats one or more errors as accepted by field validation-callback. |
||
| 1066 | * |
||
| 1067 | * @param string|Message|array $errors Array of strings or Message instances |
||
| 1068 | * @return string HTML |
||
| 1069 | * @since 1.18 |
||
| 1070 | */ |
||
| 1071 | protected static function formatErrors( $errors ) { |
||
| 1095 | } |
||
| 1096 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.