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 FormHelper 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 FormHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class FormHelper extends Base |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Hidden CSRF token field. |
||
| 23 | * |
||
| 24 | * @return string |
||
| 25 | */ |
||
| 26 | public function csrf() |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Display a hidden form field. |
||
| 33 | * |
||
| 34 | * @param string $name Field name |
||
| 35 | * @param array $values Form values |
||
| 36 | * |
||
| 37 | * @return string |
||
| 38 | */ |
||
| 39 | public function hidden($name, array $values = []) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Display a select field. |
||
| 46 | * |
||
| 47 | * @param string $name Field name |
||
| 48 | * @param array $options Options |
||
| 49 | * @param array $values Form values |
||
| 50 | * @param array $errors Form errors |
||
| 51 | * @param array $attributes |
||
| 52 | * @param string $class CSS class |
||
| 53 | * |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | public function select($name, array $options, array $values = [], array $errors = [], array $attributes = [], $class = '') |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Display a radio field group. |
||
| 81 | * |
||
| 82 | * @param string $name Field name |
||
| 83 | * @param array $options Options |
||
| 84 | * @param array $values Form values |
||
| 85 | * |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | public function radios($name, array $options, array $values = []) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Display a radio field. |
||
| 101 | * |
||
| 102 | * @param string $name Field name |
||
| 103 | * @param string $label Form label |
||
| 104 | * @param string $value Form value |
||
| 105 | * @param bool $selected Field selected or not |
||
| 106 | * @param string $class CSS class |
||
| 107 | * |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | public function radio($name, $label, $value, $selected = false, $class = '') |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Display a checkboxes group. |
||
| 117 | * |
||
| 118 | * @param string $name Field name |
||
| 119 | * @param array $options Options |
||
| 120 | * @param array $values Form values |
||
| 121 | * |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | public function checkboxes($name, array $options, array $values = []) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Display a checkbox field. |
||
| 137 | * |
||
| 138 | * @param string $name Field name |
||
| 139 | * @param string $label Form label |
||
| 140 | * @param string $value Form value |
||
| 141 | * @param bool $checked Field selected or not |
||
| 142 | * @param string $class CSS class |
||
| 143 | * |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | public function checkbox($name, $label, $value, $checked = false, $class = '') |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Display a form label. |
||
| 153 | * |
||
| 154 | * @param string $name Field name |
||
| 155 | * @param string $label Form label |
||
| 156 | * @param array $attributes HTML attributes |
||
| 157 | * |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | public function label($label, $name, array $attributes = []) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Display a textarea. |
||
| 167 | * |
||
| 168 | * @param string $name Field name |
||
| 169 | * @param array $values Form values |
||
| 170 | * @param array $errors Form errors |
||
| 171 | * @param array $attributes HTML attributes |
||
| 172 | * @param string $class CSS class |
||
| 173 | * |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | public function textarea($name, $values = [], array $errors = [], array $attributes = [], $class = '') |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Display a markdown editor. |
||
| 191 | * |
||
| 192 | * @param string $name Field name |
||
| 193 | * @param array $values Form values |
||
| 194 | * @param array $errors Form errors |
||
| 195 | * @param array $attributes |
||
| 196 | * |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | public function textEditor($name, $values = [], array $errors = [], array $attributes = []) |
||
| 200 | { |
||
| 201 | $params = [ |
||
| 202 | 'name' => $name, |
||
| 203 | 'text' => isset($values[$name]) ? $values[$name] : '', |
||
| 204 | 'css' => $this->errorClass($errors, $name), |
||
| 205 | 'required' => isset($attributes['required']) && $attributes['required'], |
||
| 206 | 'tabindex' => isset($attributes['tabindex']) ? $attributes['tabindex'] : '-1', |
||
| 207 | 'labelPreview' => t('Preview'), |
||
| 208 | 'labelWrite' => t('Write'), |
||
| 209 | 'placeholder' => isset($attributes['placeholder']) ? $attributes['placeholder'] : t('Write your text in Markdown'), |
||
| 210 | ]; |
||
| 211 | $html = '<div class="js-text-editor" data-params=\''.json_encode($params, JSON_HEX_APOS).'\'></div>'; |
||
| 212 | $html .= $this->errorList($errors, $name); |
||
| 213 | |||
| 214 | return $html; |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Display file field. |
||
| 219 | * |
||
| 220 | * @param string $name |
||
| 221 | * @param array $errors |
||
| 222 | * @param bool $multiple |
||
| 223 | * |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | public function file($name, array $errors = [], $multiple = false) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Display a input field. |
||
| 236 | * |
||
| 237 | * @param string $type HMTL input tag type |
||
| 238 | * @param string $name Field name |
||
| 239 | * @param array $values Form values |
||
| 240 | * @param array $errors Form errors |
||
| 241 | * @param array $attributes HTML attributes |
||
| 242 | * @param string $class CSS class |
||
| 243 | * |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | public function input($type, $name, $values = [], array $errors = [], array $attributes = [], $class = '') |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Display a text field. |
||
| 264 | * |
||
| 265 | * @param string $name Field name |
||
| 266 | * @param array $values Form values |
||
| 267 | * @param array $errors Form errors |
||
| 268 | * @param array $attributes HTML attributes |
||
| 269 | * @param string $class CSS class |
||
| 270 | * |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | public function text($name, $values = [], array $errors = [], array $attributes = [], $class = '') |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Display a password field. |
||
| 280 | * |
||
| 281 | * @param string $name Field name |
||
| 282 | * @param array $values Form values |
||
| 283 | * @param array $errors Form errors |
||
| 284 | * @param array $attributes HTML attributes |
||
| 285 | * @param string $class CSS class |
||
| 286 | * |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | public function password($name, $values = [], array $errors = [], array $attributes = [], $class = '') |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Display an email field. |
||
| 296 | * |
||
| 297 | * @param string $name Field name |
||
| 298 | * @param array $values Form values |
||
| 299 | * @param array $errors Form errors |
||
| 300 | * @param array $attributes HTML attributes |
||
| 301 | * @param string $class CSS class |
||
| 302 | * |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | public function email($name, $values = [], array $errors = [], array $attributes = [], $class = '') |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Display a number field. |
||
| 312 | * |
||
| 313 | * @param string $name Field name |
||
| 314 | * @param array $values Form values |
||
| 315 | * @param array $errors Form errors |
||
| 316 | * @param array $attributes HTML attributes |
||
| 317 | * @param string $class CSS class |
||
| 318 | * |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | public function number($name, $values = [], array $errors = [], array $attributes = [], $class = '') |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Display a numeric field (allow decimal number). |
||
| 328 | * |
||
| 329 | * @param string $name Field name |
||
| 330 | * @param array $values Form values |
||
| 331 | * @param array $errors Form errors |
||
| 332 | * @param array $attributes HTML attributes |
||
| 333 | * @param string $class CSS class |
||
| 334 | * |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | public function numeric($name, $values = [], array $errors = [], array $attributes = [], $class = '') |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Date field. |
||
| 344 | * |
||
| 345 | * @param string $label |
||
| 346 | * @param string $name |
||
| 347 | * @param array $values |
||
| 348 | * @param array $errors |
||
| 349 | * @param array $attributes |
||
| 350 | * |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | View Code Duplication | public function date($label, $name, array $values, array $errors = [], array $attributes = []) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Datetime field. |
||
| 365 | * |
||
| 366 | * @param string $label |
||
| 367 | * @param string $name |
||
| 368 | * @param array $values |
||
| 369 | * @param array $errors |
||
| 370 | * @param array $attributes |
||
| 371 | * |
||
| 372 | * @return string |
||
| 373 | */ |
||
| 374 | View Code Duplication | public function datetime($label, $name, array $values, array $errors = [], array $attributes = []) |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Display the form error class. |
||
| 386 | * |
||
| 387 | * @param array $errors Error list |
||
| 388 | * @param string $name Field name |
||
| 389 | * |
||
| 390 | * @return string |
||
| 391 | */ |
||
| 392 | private function errorClass(array $errors, $name) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Display a list of form errors. |
||
| 399 | * |
||
| 400 | * @param array $errors List of errors |
||
| 401 | * @param string $name Field name |
||
| 402 | * |
||
| 403 | * @return string |
||
| 404 | */ |
||
| 405 | private function errorList(array $errors, $name) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Get an escaped form value. |
||
| 424 | * |
||
| 425 | * @param mixed $values Values |
||
| 426 | * @param string $name Field name |
||
| 427 | * |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | private function formValue($values, $name) |
||
| 438 | } |
||
| 439 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.