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 ) { |
||
| 163 | $origParams = $params; |
||
| 164 | $op = array_shift( $params ); |
||
| 165 | |||
| 166 | try { |
||
| 167 | switch ( $op ) { |
||
| 168 | View Code Duplication | case 'AND': |
|
| 169 | foreach ( $params as $i => $p ) { |
||
| 170 | if ( !is_array( $p ) ) { |
||
| 171 | throw new MWException( |
||
| 172 | "Expected array, found " . gettype( $p ) . " at index $i" |
||
| 173 | ); |
||
| 174 | } |
||
| 175 | if ( !$this->isHiddenRecurse( $alldata, $p ) ) { |
||
| 176 | return false; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | return true; |
||
| 180 | |||
| 181 | case 'OR': |
||
| 182 | foreach ( $params as $i => $p ) { |
||
| 183 | if ( !is_array( $p ) ) { |
||
| 184 | throw new MWException( |
||
| 185 | "Expected array, found " . gettype( $p ) . " at index $i" |
||
| 186 | ); |
||
| 187 | } |
||
| 188 | if ( $this->isHiddenRecurse( $alldata, $p ) ) { |
||
| 189 | return true; |
||
| 190 | } |
||
| 191 | } |
||
| 192 | return false; |
||
| 193 | |||
| 194 | View Code Duplication | case 'NAND': |
|
| 195 | foreach ( $params as $i => $p ) { |
||
| 196 | if ( !is_array( $p ) ) { |
||
| 197 | throw new MWException( |
||
| 198 | "Expected array, found " . gettype( $p ) . " at index $i" |
||
| 199 | ); |
||
| 200 | } |
||
| 201 | if ( !$this->isHiddenRecurse( $alldata, $p ) ) { |
||
| 202 | return true; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | return false; |
||
| 206 | |||
| 207 | case 'NOR': |
||
| 208 | foreach ( $params as $i => $p ) { |
||
| 209 | if ( !is_array( $p ) ) { |
||
| 210 | throw new MWException( |
||
| 211 | "Expected array, found " . gettype( $p ) . " at index $i" |
||
| 212 | ); |
||
| 213 | } |
||
| 214 | if ( $this->isHiddenRecurse( $alldata, $p ) ) { |
||
| 215 | return false; |
||
| 216 | } |
||
| 217 | } |
||
| 218 | return true; |
||
| 219 | |||
| 220 | case 'NOT': |
||
| 221 | if ( count( $params ) !== 1 ) { |
||
| 222 | throw new MWException( "NOT takes exactly one parameter" ); |
||
| 223 | } |
||
| 224 | $p = $params[0]; |
||
| 225 | if ( !is_array( $p ) ) { |
||
| 226 | throw new MWException( |
||
| 227 | "Expected array, found " . gettype( $p ) . " at index 0" |
||
| 228 | ); |
||
| 229 | } |
||
| 230 | return !$this->isHiddenRecurse( $alldata, $p ); |
||
| 231 | |||
| 232 | case '===': |
||
| 233 | case '!==': |
||
| 234 | if ( count( $params ) !== 2 ) { |
||
| 235 | throw new MWException( "$op takes exactly two parameters" ); |
||
| 236 | } |
||
| 237 | list( $field, $value ) = $params; |
||
| 238 | if ( !is_string( $field ) || !is_string( $value ) ) { |
||
| 239 | throw new MWException( "Parameters for $op must be strings" ); |
||
| 240 | } |
||
| 241 | $testValue = $this->getNearestFieldByName( $alldata, $field ); |
||
| 242 | switch ( $op ) { |
||
| 243 | case '===': |
||
| 244 | return ( $value === $testValue ); |
||
| 245 | case '!==': |
||
| 246 | return ( $value !== $testValue ); |
||
| 247 | } |
||
| 248 | |||
| 249 | default: |
||
| 250 | throw new MWException( "Unknown operation" ); |
||
| 251 | } |
||
| 252 | } catch ( Exception $ex ) { |
||
| 253 | throw new MWException( |
||
| 254 | "Invalid hide-if specification for $this->mName: " . |
||
| 255 | $ex->getMessage() . " in " . var_export( $origParams, true ), |
||
| 256 | 0, $ex |
||
| 257 | ); |
||
| 258 | } |
||
| 259 | } |
||
| 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 ) { |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Get the complete table row for the input, including help text, |
||
| 459 | * labels, and whatever. |
||
| 460 | * |
||
| 461 | * @param string $value The value to set the input to. |
||
| 462 | * |
||
| 463 | * @return string Complete HTML table row. |
||
| 464 | */ |
||
| 465 | function getTableRow( $value ) { |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Get the complete div for the input, including help text, |
||
| 516 | * labels, and whatever. |
||
| 517 | * @since 1.20 |
||
| 518 | * |
||
| 519 | * @param string $value The value to set the input to. |
||
| 520 | * |
||
| 521 | * @return string Complete HTML table row. |
||
| 522 | */ |
||
| 523 | public function getDiv( $value ) { |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Get the OOUI version of the div. Falls back to getDiv by default. |
||
| 566 | * @since 1.26 |
||
| 567 | * |
||
| 568 | * @param string $value The value to set the input to. |
||
| 569 | * |
||
| 570 | * @return OOUI\FieldLayout|OOUI\ActionFieldLayout |
||
| 571 | */ |
||
| 572 | public function getOOUI( $value ) { |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Get label alignment when generating field for OOUI. |
||
| 620 | * @return string 'left', 'right', 'top' or 'inline' |
||
| 621 | */ |
||
| 622 | protected function getLabelAlignOOUI() { |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Get a FieldLayout (or subclass thereof) to wrap this field in when using OOUI output. |
||
| 628 | * @return OOUI\FieldLayout|OOUI\ActionFieldLayout |
||
| 629 | */ |
||
| 630 | protected function getFieldLayoutOOUI( $inputField, $config ) { |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Get the complete raw fields for the input, including help text, |
||
| 640 | * labels, and whatever. |
||
| 641 | * @since 1.20 |
||
| 642 | * |
||
| 643 | * @param string $value The value to set the input to. |
||
| 644 | * |
||
| 645 | * @return string Complete HTML table row. |
||
| 646 | */ |
||
| 647 | public function getRaw( $value ) { |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Get the complete field for the input, including help text, |
||
| 664 | * labels, and whatever. Fall back from 'vform' to 'div' when not overridden. |
||
| 665 | * |
||
| 666 | * @since 1.25 |
||
| 667 | * @param string $value The value to set the input to. |
||
| 668 | * @return string Complete HTML field. |
||
| 669 | */ |
||
| 670 | public function getVForm( $value ) { |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Get the complete field as an inline element. |
||
| 678 | * @since 1.25 |
||
| 679 | * @param string $value The value to set the input to. |
||
| 680 | * @return string Complete HTML inline element |
||
| 681 | */ |
||
| 682 | public function getInline( $value ) { |
||
| 696 | |||
| 697 | /** |
||
| 698 | * Generate help text HTML in table format |
||
| 699 | * @since 1.20 |
||
| 700 | * |
||
| 701 | * @param string|null $helptext |
||
| 702 | * @return string |
||
| 703 | */ |
||
| 704 | public function getHelpTextHtmlTable( $helptext ) { |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Generate help text HTML in div format |
||
| 727 | * @since 1.20 |
||
| 728 | * |
||
| 729 | * @param string|null $helptext |
||
| 730 | * |
||
| 731 | * @return string |
||
| 732 | */ |
||
| 733 | public function getHelpTextHtmlDiv( $helptext ) { |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Generate help text HTML formatted for raw output |
||
| 755 | * @since 1.20 |
||
| 756 | * |
||
| 757 | * @param string|null $helptext |
||
| 758 | * @return string |
||
| 759 | */ |
||
| 760 | public function getHelpTextHtmlRaw( $helptext ) { |
||
| 763 | |||
| 764 | /** |
||
| 765 | * Determine the help text to display |
||
| 766 | * @since 1.20 |
||
| 767 | * @return string HTML |
||
| 768 | */ |
||
| 769 | public function getHelpText() { |
||
| 795 | |||
| 796 | /** |
||
| 797 | * Determine form errors to display and their classes |
||
| 798 | * @since 1.20 |
||
| 799 | * |
||
| 800 | * @param string $value The value of the input |
||
| 801 | * @return array array( $errors, $errorClass ) |
||
| 802 | */ |
||
| 803 | public function getErrorsAndErrorClass( $value ) { |
||
| 816 | |||
| 817 | /** |
||
| 818 | * Determine form errors to display, returning them in an array. |
||
| 819 | * |
||
| 820 | * @since 1.26 |
||
| 821 | * @param string $value The value of the input |
||
| 822 | * @return string[] Array of error HTML strings |
||
| 823 | */ |
||
| 824 | public function getErrorsRaw( $value ) { |
||
| 842 | |||
| 843 | /** |
||
| 844 | * @return string HTML |
||
| 845 | */ |
||
| 846 | function getLabel() { |
||
| 849 | |||
| 850 | function getLabelHtml( $cellAttributes = [] ) { |
||
| 888 | |||
| 889 | function getDefault() { |
||
| 896 | |||
| 897 | /** |
||
| 898 | * Returns the attributes required for the tooltip and accesskey. |
||
| 899 | * |
||
| 900 | * @return array Attributes |
||
| 901 | */ |
||
| 902 | public function getTooltipAndAccessKey() { |
||
| 909 | |||
| 910 | /** |
||
| 911 | * Returns the given attributes from the parameters |
||
| 912 | * |
||
| 913 | * @param array $list List of attributes to get |
||
| 914 | * @return array Attributes |
||
| 915 | */ |
||
| 916 | public function getAttributes( array $list ) { |
||
| 932 | |||
| 933 | /** |
||
| 934 | * Given an array of msg-key => value mappings, returns an array with keys |
||
| 935 | * being the message texts. It also forces values to strings. |
||
| 936 | * |
||
| 937 | * @param array $options |
||
| 938 | * @return array |
||
| 939 | */ |
||
| 940 | private function lookupOptionsKeys( $options ) { |
||
| 950 | |||
| 951 | /** |
||
| 952 | * Recursively forces values in an array to strings, because issues arise |
||
| 953 | * with integer 0 as a value. |
||
| 954 | * |
||
| 955 | * @param array $array |
||
| 956 | * @return array |
||
| 957 | */ |
||
| 958 | static function forceToStringRecursive( $array ) { |
||
| 965 | |||
| 966 | /** |
||
| 967 | * Fetch the array of options from the field's parameters. In order, this |
||
| 968 | * checks 'options-messages', 'options', then 'options-message'. |
||
| 969 | * |
||
| 970 | * @return array|null Options array |
||
| 971 | */ |
||
| 972 | public function getOptions() { |
||
| 1014 | |||
| 1015 | /** |
||
| 1016 | * Get options and make them into arrays suitable for OOUI. |
||
| 1017 | * @return array Options for inclusion in a select or whatever. |
||
| 1018 | */ |
||
| 1019 | public function getOptionsOOUI() { |
||
| 1037 | |||
| 1038 | /** |
||
| 1039 | * flatten an array of options to a single array, for instance, |
||
| 1040 | * a set of "<options>" inside "<optgroups>". |
||
| 1041 | * |
||
| 1042 | * @param array $options Associative Array with values either Strings or Arrays |
||
| 1043 | * @return array Flattened input |
||
| 1044 | */ |
||
| 1045 | public static function flattenOptions( $options ) { |
||
| 1058 | |||
| 1059 | /** |
||
| 1060 | * Formats one or more errors as accepted by field validation-callback. |
||
| 1061 | * |
||
| 1062 | * @param string|Message|array $errors Array of strings or Message instances |
||
| 1063 | * @return string HTML |
||
| 1064 | * @since 1.18 |
||
| 1065 | */ |
||
| 1066 | protected static function formatErrors( $errors ) { |
||
| 1090 | |||
| 1091 | /** |
||
| 1092 | * Turns a *-message parameter (which could be a MessageSpecifier, or a message name, or a |
||
| 1093 | * name + parameters array) into a Message. |
||
| 1094 | * @param mixed $value |
||
| 1095 | * @return Message |
||
| 1096 | */ |
||
| 1097 | protected function getMessage( $value ) { |
||
| 1109 | } |
||
| 1110 |
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.