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 |
||
| 35 | View Code Duplication | class HTML_QuickForm_hidden extends HTML_QuickForm_input |
|
|
|
|||
| 36 | { |
||
| 37 | // {{{ constructor |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Class constructor |
||
| 41 | * |
||
| 42 | * @param string $elementName (optional)Input field name attribute |
||
| 43 | * @param string $value (optional)Input field value |
||
| 44 | * @param mixed $attributes (optional)Either a typical HTML attribute string |
||
| 45 | * or an associative array |
||
| 46 | * @since 1.0 |
||
| 47 | * @access public |
||
| 48 | * @return void |
||
| 49 | */ |
||
| 50 | public function __construct($elementName = null, $value = '', $attributes = null) |
||
| 51 | { |
||
| 52 | parent::__construct($elementName, null, $attributes); |
||
| 53 | $this->setType('hidden'); |
||
| 54 | $this->setValue($value); |
||
| 55 | } //end constructor |
||
| 56 | |||
| 57 | // }}} |
||
| 58 | // {{{ freeze() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Freeze the element so that only its value is returned |
||
| 62 | * |
||
| 63 | * @access public |
||
| 64 | * @return void |
||
| 65 | */ |
||
| 66 | function freeze() |
||
| 70 | |||
| 71 | // }}} |
||
| 72 | // {{{ accept() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Accepts a renderer |
||
| 76 | * |
||
| 77 | * @param HTML_QuickForm_Renderer renderer object |
||
| 78 | * @access public |
||
| 79 | * @return void |
||
| 80 | */ |
||
| 81 | //function accept(&$renderer) |
||
| 82 | function accept(&$renderer, $required=false, $error=null) |
||
| 86 | |||
| 87 | // }}} |
||
| 88 | |||
| 89 | } //end class HTML_QuickForm_hidden |
||
| 90 | ?> |
||
| 91 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.