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 |
||
| 38 | class HTML_QuickForm_checkbox extends HTML_QuickForm_input |
||
| 39 | { |
||
| 40 | // {{{ properties |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Checkbox display text |
||
| 44 | * @var string |
||
| 45 | * @since 1.1 |
||
| 46 | * @access private |
||
| 47 | */ |
||
| 48 | public $_text = ''; |
||
| 49 | public $labelClass; |
||
| 50 | public $checkboxClass; |
||
| 51 | |||
| 52 | // }}} |
||
| 53 | // {{{ constructor |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Class constructor |
||
| 57 | * |
||
| 58 | * @param string $elementName (optional)Input field name attribute |
||
| 59 | * @param string $elementLabel (optional)Input field value |
||
| 60 | * @param string $text (optional)Checkbox display text |
||
| 61 | * @param mixed $attributes (optional)Either a typical HTML attribute string |
||
| 62 | * or an associative array |
||
| 63 | * @since 1.0 |
||
| 64 | * @access public |
||
| 65 | * @return void |
||
|
|
|||
| 66 | */ |
||
| 67 | public function __construct( |
||
| 68 | $elementName = null, |
||
| 69 | $elementLabel = null, |
||
| 70 | $text = '', |
||
| 71 | $attributes = null |
||
| 72 | ) { |
||
| 73 | $this->labelClass = isset($attributes['label-class']) ? $attributes['label-class'] : ''; |
||
| 74 | $this->checkboxClass = isset($attributes['checkbox-class']) ? $attributes['checkbox-class'] : 'checkbox'; |
||
| 75 | |||
| 76 | if (isset($attributes['label-class'])) { |
||
| 77 | unset($attributes['label-class']); |
||
| 78 | } |
||
| 79 | |||
| 80 | if (isset($attributes['checkbox-class'])) { |
||
| 81 | unset($attributes['checkbox-class']); |
||
| 82 | } |
||
| 83 | |||
| 84 | parent::__construct($elementName, $elementLabel, $attributes); |
||
| 85 | $this->_persistantFreeze = true; |
||
| 86 | $this->_text = $text; |
||
| 87 | $this->setType('checkbox'); |
||
| 88 | |||
| 89 | if (!isset($attributes['value'])) { |
||
| 90 | $this->updateAttributes(array('value' => 1)); |
||
| 91 | } else { |
||
| 92 | $this->updateAttributes(array('value' => $attributes['value'])); |
||
| 93 | } |
||
| 94 | |||
| 95 | $this->_generateId(); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Sets whether a checkbox is checked |
||
| 100 | * |
||
| 101 | * @param bool $checked Whether the field is checked or not |
||
| 102 | * @since 1.0 |
||
| 103 | * @access public |
||
| 104 | * @return void |
||
| 105 | */ |
||
| 106 | function setChecked($checked) |
||
| 114 | |||
| 115 | // }}} |
||
| 116 | // {{{ getChecked() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Returns whether a checkbox is checked |
||
| 120 | * |
||
| 121 | * @since 1.0 |
||
| 122 | * @access public |
||
| 123 | * @return bool |
||
| 124 | */ |
||
| 125 | function getChecked() |
||
| 129 | |||
| 130 | // }}} |
||
| 131 | // {{{ toHtml() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Returns the checkbox element in HTML |
||
| 135 | * |
||
| 136 | * @since 1.0 |
||
| 137 | * @access public |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | View Code Duplication | public function toHtml() |
|
| 141 | { |
||
| 142 | if (0 == strlen($this->_text)) { |
||
| 143 | $label = ''; |
||
| 144 | } elseif ($this->_flagFrozen) { |
||
| 145 | $label = $this->_text; |
||
| 146 | } else { |
||
| 147 | $labelClass = $this->labelClass; |
||
| 148 | $checkboxClass = $this->checkboxClass; |
||
| 149 | |||
| 150 | $label =' |
||
| 151 | <div class="'.$checkboxClass.'"> |
||
| 152 | <label class="'.$labelClass.'">' . |
||
| 153 | HTML_QuickForm_input::toHtml().$this->_text. |
||
| 154 | '</label> |
||
| 155 | </div> |
||
| 156 | '; |
||
| 157 | |||
| 158 | return $label; |
||
| 159 | } |
||
| 160 | |||
| 161 | return HTML_QuickForm_input::toHtml() . $label; |
||
| 162 | } //end func toHtml |
||
| 163 | |||
| 164 | // }}} |
||
| 165 | // {{{ getFrozenHtml() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Returns the value of field without HTML tags |
||
| 169 | * |
||
| 170 | * @since 1.0 |
||
| 171 | * @access public |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | function getFrozenHtml() |
||
| 183 | |||
| 184 | // }}} |
||
| 185 | // {{{ setText() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Sets the checkbox text |
||
| 189 | * |
||
| 190 | * @param string $text |
||
| 191 | * @since 1.1 |
||
| 192 | * @access public |
||
| 193 | * @return void |
||
| 194 | */ |
||
| 195 | function setText($text) |
||
| 199 | |||
| 200 | // }}} |
||
| 201 | // {{{ getText() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Returns the checkbox text |
||
| 205 | * |
||
| 206 | * @since 1.1 |
||
| 207 | * @access public |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | function getText() |
||
| 214 | |||
| 215 | // }}} |
||
| 216 | // {{{ setValue() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Sets the value of the form element |
||
| 220 | * |
||
| 221 | * @param string $value Default value of the form element |
||
| 222 | * @since 1.0 |
||
| 223 | * @access public |
||
| 224 | * @return void |
||
| 225 | */ |
||
| 226 | function setValue($value) |
||
| 230 | |||
| 231 | // }}} |
||
| 232 | // {{{ getValue() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Returns the value of the form element |
||
| 236 | * |
||
| 237 | * @since 1.0 |
||
| 238 | * @access public |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | function getValue() |
||
| 245 | |||
| 246 | // }}} |
||
| 247 | // {{{ onQuickFormEvent() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Called by HTML_QuickForm whenever form event is made on this element |
||
| 251 | * |
||
| 252 | * @param string $event Name of event |
||
| 253 | * @param mixed $arg event arguments |
||
| 254 | * @param object &$caller calling object |
||
| 255 | * @since 1.0 |
||
| 256 | * @access public |
||
| 257 | * @return void |
||
| 258 | */ |
||
| 259 | function onQuickFormEvent($event, $arg, &$caller) |
||
| 287 | |||
| 288 | // }}} |
||
| 289 | // {{{ exportValue() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Return true if the checkbox is checked, null if it is not checked (getValue() returns false) |
||
| 293 | */ |
||
| 294 | View Code Duplication | function exportValue(&$submitValues, $assoc = false) |
|
| 302 | } |
||
| 303 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.