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 Select 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 Select, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | abstract class Select |
||
21 | { |
||
22 | /** |
||
23 | * Option values related to the generation of HTML output. Recognized |
||
24 | * options are: |
||
25 | * fmtDepth, integer. The current indent depth. |
||
26 | * fmtEol, string. The end of line string, default is linefeed. |
||
27 | * fmtIndent, string. The string to use for indentation, default is |
||
28 | * tab. |
||
29 | * |
||
30 | * @var array |
||
31 | * @since 1.0 |
||
32 | */ |
||
33 | protected static $formatOptions = array('format.depth' => 0, 'format.eol' => "\n", 'format.indent' => "\t"); |
||
34 | |||
35 | /** |
||
36 | * Default values for options. Organized by option group. |
||
37 | * |
||
38 | * @var array |
||
39 | * @since 1.0 |
||
40 | */ |
||
41 | static protected $optionDefaults = array( |
||
42 | 'option' => array('option.attr' => null, 'option.disable' => 'disable', 'option.id' => null, 'option.key' => 'value', |
||
43 | 'option.key.toHtml' => true, 'option.label' => null, 'option.label.toHtml' => true, 'option.text' => 'text', |
||
44 | 'option.text.toHtml' => true)); |
||
45 | |||
46 | /** |
||
47 | * Generates a yes/no radio list. |
||
48 | * |
||
49 | * @param string $name The value of the HTML name attribute |
||
50 | * @param array $attribs Additional HTML attributes for the <select> tag |
||
51 | * @param string $selected The key that is selected |
||
52 | * @param string $yes Language key for Yes |
||
53 | * @param string $no Language key for no |
||
54 | * @param string $id The id for the field |
||
55 | * |
||
56 | * @return string HTML for the radio list |
||
57 | * |
||
58 | * @see \Joomla\Form\Field\Radio |
||
59 | * @since 1.0 |
||
60 | */ |
||
61 | public static function booleanlist($name, $attribs = null, $selected = null, $yes = 'JYES', $no = 'JNO', $id = false) |
||
67 | |||
68 | /** |
||
69 | * Generates an HTML selection list. |
||
70 | * |
||
71 | * @param array $data An array of objects, arrays, or scalars. |
||
72 | * @param string $name The value of the HTML name attribute. |
||
73 | * @param mixed $attribs Additional HTML attributes for the <select> tag. This |
||
74 | * can be an array of attributes, or an array of options. Treated as options |
||
75 | * if it is the last argument passed. Valid options are: |
||
76 | * Format options, see {@see Select::$formatOptions}. |
||
77 | * Selection options, see {@see Select::options()}. |
||
78 | * list.attr, string|array: Additional attributes for the select |
||
79 | * element. |
||
80 | * id, string: Value to use as the select element id attribute. |
||
81 | * Defaults to the same as the name. |
||
82 | * list.select, string|array: Identifies one or more option elements |
||
83 | * to be selected, based on the option key values. |
||
84 | * @param string $optKey The name of the object variable for the option value. If |
||
85 | * set to null, the index of the value array is used. |
||
86 | * @param string $optText The name of the object variable for the option text. |
||
87 | * @param mixed $selected The key that is selected (accepts an array or a string). |
||
88 | * @param mixed $idtag Value of the field id or null by default |
||
89 | * @param boolean $translate True to translate |
||
90 | * |
||
91 | * @return string HTML for the select list. |
||
92 | * |
||
93 | * @since 1.0 |
||
94 | */ |
||
95 | public static function genericlist($data, $name, $attribs = null, $optKey = 'value', $optText = 'text', $selected = null, $idtag = false, |
||
145 | |||
146 | /** |
||
147 | * Generates a grouped HTML selection list from nested arrays. |
||
148 | * |
||
149 | * @param array $data An array of groups, each of which is an array of options. |
||
150 | * @param string $name The value of the HTML name attribute |
||
151 | * @param array $options Options, an array of key/value pairs. Valid options are: |
||
152 | * Format options, {@see Select::$formatOptions}. |
||
153 | * Selection options. See {@see Select::options()}. |
||
154 | * group.id: The property in each group to use as the group id |
||
155 | * attribute. Defaults to none. |
||
156 | * group.label: The property in each group to use as the group |
||
157 | * label. Defaults to "text". If set to null, the data array index key is |
||
158 | * used. |
||
159 | * group.items: The property in each group to use as the array of |
||
160 | * items in the group. Defaults to "items". If set to null, group.id and |
||
161 | * group. label are forced to null and the data element is assumed to be a |
||
162 | * list of selections. |
||
163 | * id: Value to use as the select element id attribute. Defaults to |
||
164 | * the same as the name. |
||
165 | * list.attr: Attributes for the select element. Can be a string or |
||
166 | * an array of key/value pairs. Defaults to none. |
||
167 | * list.select: either the value of one selected option or an array |
||
168 | * of selected options. Default: none. |
||
169 | * list.translate: Boolean. If set, text and labels are translated via |
||
170 | * Text::_(). |
||
171 | * |
||
172 | * @return string HTML for the select list |
||
173 | * |
||
174 | * @since 1.0 |
||
175 | * @throws \RuntimeException If a group has contents that cannot be processed. |
||
176 | */ |
||
177 | public static function groupedlist($data, $name, $options = array()) |
||
287 | |||
288 | /** |
||
289 | * Generates a selection list of integers. |
||
290 | * |
||
291 | * @param integer $start The start integer |
||
292 | * @param integer $end The end integer |
||
293 | * @param integer $inc The increment |
||
294 | * @param string $name The value of the HTML name attribute |
||
295 | * @param mixed $attribs Additional HTML attributes for the <select> tag, an array of |
||
296 | * attributes, or an array of options. Treated as options if it is the last |
||
297 | * argument passed. |
||
298 | * @param mixed $selected The key that is selected |
||
299 | * @param string $format The printf format to be applied to the number |
||
300 | * |
||
301 | * @return string HTML for the select list |
||
302 | * |
||
303 | * @since 1.0 |
||
304 | */ |
||
305 | public static function integerlist($start, $end, $inc, $name, $attribs = null, $selected = null, $format = '') |
||
342 | |||
343 | /** |
||
344 | * Create an object that represents an option in an option list. |
||
345 | * |
||
346 | * @param string $value The value of the option |
||
347 | * @param string $text The text for the option |
||
348 | * @param mixed $optKey If a string, the returned object property name for |
||
349 | * the value. If an array, options. Valid options are: |
||
350 | * attr: String|array. Additional attributes for this option. |
||
351 | * Defaults to none. |
||
352 | * disable: Boolean. If set, this option is disabled. |
||
353 | * label: String. The value for the option label. |
||
354 | * option.attr: The property in each option array to use for |
||
355 | * additional selection attributes. Defaults to none. |
||
356 | * option.disable: The property that will hold the disabled state. |
||
357 | * Defaults to "disable". |
||
358 | * option.key: The property that will hold the selection value. |
||
359 | * Defaults to "value". |
||
360 | * option.label: The property in each option array to use as the |
||
361 | * selection label attribute. If a "label" option is provided, defaults to |
||
362 | * "label", if no label is given, defaults to null (none). |
||
363 | * option.text: The property that will hold the the displayed text. |
||
364 | * Defaults to "text". If set to null, the option array is assumed to be a |
||
365 | * list of displayable scalars. |
||
366 | * @param string $optText The property that will hold the the displayed text. This |
||
367 | * parameter is ignored if an options array is passed. |
||
368 | * @param boolean $disable Not used. |
||
369 | * |
||
370 | * @return object |
||
371 | * |
||
372 | * @since 1.0 |
||
373 | */ |
||
374 | public static function option($value, $text = '', $optKey = 'value', $optText = 'text', $disable = false) |
||
426 | |||
427 | /** |
||
428 | * Generates the option tags for an HTML select list (with no select tag |
||
429 | * surrounding the options). |
||
430 | * |
||
431 | * @param array $arr An array of objects, arrays, or values. |
||
432 | * @param mixed $optKey If a string, this is the name of the object variable for |
||
433 | * the option value. If null, the index of the array of objects is used. If |
||
434 | * an array, this is a set of options, as key/value pairs. Valid options are: |
||
435 | * -Format options, {@see Select::$formatOptions}. |
||
436 | * -groups: Boolean. If set, looks for keys with the value |
||
437 | * "<optgroup>" and synthesizes groups from them. Deprecated. Defaults |
||
438 | * true for backwards compatibility. |
||
439 | * -list.select: either the value of one selected option or an array |
||
440 | * of selected options. Default: none. |
||
441 | * -list.translate: Boolean. If set, text and labels are translated via |
||
442 | * Text::_(). Default is false. |
||
443 | * -option.id: The property in each option array to use as the |
||
444 | * selection id attribute. Defaults to none. |
||
445 | * -option.key: The property in each option array to use as the |
||
446 | * selection value. Defaults to "value". If set to null, the index of the |
||
447 | * option array is used. |
||
448 | * -option.label: The property in each option array to use as the |
||
449 | * selection label attribute. Defaults to null (none). |
||
450 | * -option.text: The property in each option array to use as the |
||
451 | * displayed text. Defaults to "text". If set to null, the option array is |
||
452 | * assumed to be a list of displayable scalars. |
||
453 | * -option.attr: The property in each option array to use for |
||
454 | * additional selection attributes. Defaults to none. |
||
455 | * -option.disable: The property that will hold the disabled state. |
||
456 | * Defaults to "disable". |
||
457 | * -option.key: The property that will hold the selection value. |
||
458 | * Defaults to "value". |
||
459 | * -option.text: The property that will hold the the displayed text. |
||
460 | * Defaults to "text". If set to null, the option array is assumed to be a |
||
461 | * list of displayable scalars. |
||
462 | * @param string $optText The name of the object variable for the option text. |
||
463 | * @param mixed $selected The key that is selected (accepts an array or a string) |
||
464 | * @param boolean $translate Translate the option values. |
||
465 | * |
||
466 | * @return string HTML for the select list |
||
467 | * |
||
468 | * @since 1.0 |
||
469 | */ |
||
470 | public static function options($arr, $optKey = 'value', $optText = 'text', $selected = null, $translate = false) |
||
644 | |||
645 | /** |
||
646 | * Generates an HTML radio list. |
||
647 | * |
||
648 | * @param array $data An array of objects |
||
649 | * @param string $name The value of the HTML name attribute |
||
650 | * @param string $attribs Additional HTML attributes for the <select> tag |
||
651 | * @param mixed $optKey The key that is selected |
||
652 | * @param string $optText The name of the object variable for the option value |
||
653 | * @param string $selected The name of the object variable for the option text |
||
654 | * @param boolean $idtag Value of the field id or null by default |
||
655 | * @param boolean $translate True if options will be translated |
||
656 | * |
||
657 | * @return string HTML for the select list |
||
658 | * |
||
659 | * @since 1.0 |
||
660 | */ |
||
661 | public static function radiolist($data, $name, $attribs = null, $optKey = 'value', $optText = 'text', $selected = null, $idtag = false, |
||
710 | } |
||
711 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.