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 |
||
| 27 | class XoopsFormCheckBox extends XoopsFormElement |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Availlable options |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | * @access private |
||
| 34 | */ |
||
| 35 | public $_options = array(); |
||
| 36 | |||
| 37 | /** |
||
| 38 | * pre-selected values in array |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | * @access private |
||
| 42 | */ |
||
| 43 | public $_value = array(); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * HTML to seperate the elements |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | * @access private |
||
| 50 | */ |
||
| 51 | public $_delimeter; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Column number for rendering |
||
| 55 | * |
||
| 56 | * @var int |
||
| 57 | * @access public |
||
| 58 | */ |
||
| 59 | public $columns; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Constructor |
||
| 63 | * |
||
| 64 | * @param string $caption |
||
| 65 | * @param string $name |
||
| 66 | * @param mixed $value Either one value as a string or an array of them. |
||
| 67 | * @param string $delimeter |
||
| 68 | */ |
||
| 69 | View Code Duplication | public function __construct($caption, $name, $value = null, $delimeter = ' ') |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Get the "value" |
||
| 82 | * |
||
| 83 | * @param bool $encode To sanitizer the text? |
||
| 84 | * @return array |
||
| 85 | */ |
||
| 86 | View Code Duplication | public function getValue($encode = false) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Set the "value" |
||
| 101 | * |
||
| 102 | * @param array $value |
||
| 103 | * |
||
| 104 | */ |
||
| 105 | public function setValue($value) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Add an option |
||
| 119 | * |
||
| 120 | * @param string $value |
||
| 121 | * @param string $name |
||
| 122 | */ |
||
| 123 | View Code Duplication | public function addOption($value, $name = '') |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Add multiple Options at once |
||
| 134 | * |
||
| 135 | * @param array $options Associative array of value->name pairs |
||
| 136 | */ |
||
| 137 | public function addOptionArray($options) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get an array with all the options |
||
| 148 | * |
||
| 149 | * @param bool|int $encode To sanitizer the text? potential values: 0 - skip; 1 - only for value; 2 - for both value and name |
||
| 150 | * @return array Associative array of value->name pairs |
||
| 151 | */ |
||
| 152 | View Code Duplication | public function getOptions($encode = false) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Get the delimiter of this group |
||
| 167 | * |
||
| 168 | * @param bool $encode To sanitizer the text? |
||
| 169 | * @return string The delimiter |
||
| 170 | */ |
||
| 171 | public function getDelimeter($encode = false) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * prepare HTML for output |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function render() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Render custom javascript validation code |
||
| 188 | * |
||
| 189 | * @seealso XoopsForm::renderValidationJS |
||
| 190 | */ |
||
| 191 | public function renderValidationJS() |
||
| 208 | } |
||
| 209 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.