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:
| 1 | <?php |
||
| 23 | class XoopsFormRadio extends XoopsFormElement |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Array of Options |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | * @access private |
||
| 30 | */ |
||
| 31 | public $_options = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Pre-selected value |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | * @access private |
||
| 38 | */ |
||
| 39 | public $_value; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * HTML to seperate the elements |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | * @access private |
||
| 46 | */ |
||
| 47 | public $_delimeter; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Column number for rendering |
||
| 51 | * |
||
| 52 | * @var int |
||
| 53 | * @access public |
||
| 54 | */ |
||
| 55 | public $columns; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Constructor |
||
| 59 | * |
||
| 60 | * @param string $caption Caption |
||
| 61 | * @param string $name "name" attribute |
||
| 62 | * @param string $value Pre-selected value |
||
| 63 | * @param string $delimeter |
||
| 64 | */ |
||
| 65 | public function __construct($caption, $name, $value = null, $delimeter = ' ') |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Get the "value" attribute |
||
| 77 | * |
||
| 78 | * @param bool $encode To sanitizer the text? |
||
| 79 | * @return string |
||
| 80 | */ |
||
| 81 | public function getValue($encode = false) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Set the pre-selected value |
||
| 88 | * |
||
| 89 | * @param $value string |
||
| 90 | */ |
||
| 91 | public function setValue($value) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Add an option |
||
| 98 | * |
||
| 99 | * @param string $value "value" attribute - This gets submitted as form-data. |
||
| 100 | * @param string $name "name" attribute - This is displayed. If empty, we use the "value" instead. |
||
| 101 | */ |
||
| 102 | View Code Duplication | public function addOption($value, $name = '') |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Adds multiple options |
||
| 113 | * |
||
| 114 | * @param array $options Associative array of value->name pairs. |
||
| 115 | */ |
||
| 116 | public function addOptionArray($options) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get an array with all the options |
||
| 127 | * |
||
| 128 | * @param bool|int $encode To sanitizer the text? potential values: 0 - skip; 1 - only for value; 2 - for both value and name |
||
| 129 | * |
||
| 130 | * @return array Associative array of value->name pairs |
||
| 131 | */ |
||
| 132 | View Code Duplication | public function getOptions($encode = false) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Get the delimiter of this group |
||
| 147 | * |
||
| 148 | * @param bool $encode To sanitizer the text? |
||
| 149 | * @return string The delimiter |
||
| 150 | */ |
||
| 151 | public function getDelimeter($encode = false) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Prepare HTML for output |
||
| 158 | * |
||
| 159 | * @return string HTML |
||
| 160 | */ |
||
| 161 | public function render() |
||
| 165 | } |
||
| 166 |