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 | class XoopsFormSelect extends XoopsFormElement |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * Options |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | * @access private |
||
| 42 | */ |
||
| 43 | public $_options = array(); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Allow multiple selections? |
||
| 47 | * |
||
| 48 | * @var bool |
||
| 49 | * @access private |
||
| 50 | */ |
||
| 51 | public $_multiple = false; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Number of rows. "1" makes a dropdown list. |
||
| 55 | * |
||
| 56 | * @var int |
||
| 57 | * @access private |
||
| 58 | */ |
||
| 59 | public $_size; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Pre-selcted values |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | * @access private |
||
| 66 | */ |
||
| 67 | public $_value = array(); |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Constructor |
||
| 71 | * |
||
| 72 | * @param string $caption Caption |
||
| 73 | * @param string $name "name" attribute |
||
| 74 | * @param mixed $value Pre-selected value (or array of them). |
||
| 75 | * @param int $size Number or rows. "1" makes a drop-down-list |
||
| 76 | * @param bool $multiple Allow multiple selections? |
||
| 77 | */ |
||
| 78 | View Code Duplication | public function __construct($caption, $name, $value = null, $size = 1, $multiple = false) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Are multiple selections allowed? |
||
| 91 | * |
||
| 92 | * @return bool |
||
| 93 | */ |
||
| 94 | public function isMultiple() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get the size |
||
| 101 | * |
||
| 102 | * @return int |
||
| 103 | */ |
||
| 104 | public function getSize() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get an array of pre-selected values |
||
| 111 | * |
||
| 112 | * @param bool $encode To sanitizer the text? |
||
| 113 | * @return array |
||
| 114 | */ |
||
| 115 | View Code Duplication | public function getValue($encode = false) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Set pre-selected values |
||
| 130 | * |
||
| 131 | * @param $value mixed |
||
| 132 | */ |
||
| 133 | View Code Duplication | public function setValue($value) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Add an option |
||
| 146 | * |
||
| 147 | * @param string $value "value" attribute |
||
| 148 | * @param string $name "name" attribute |
||
| 149 | */ |
||
| 150 | View Code Duplication | public function addOption($value, $name = '') |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Add multiple options |
||
| 161 | * |
||
| 162 | * @param array $options Associative array of value->name pairs |
||
| 163 | */ |
||
| 164 | public function addOptionArray($options) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get an array with all the options |
||
| 175 | * |
||
| 176 | * Note: both name and value should be sanitized. However for backward compatibility, only value is sanitized for now. |
||
| 177 | * |
||
| 178 | * @param bool|int $encode To sanitizer the text? potential values: 0 - skip; 1 - only for value; 2 - for both value and name |
||
| 179 | * |
||
| 180 | * @return array Associative array of value->name pairs |
||
| 181 | */ |
||
| 182 | View Code Duplication | public function getOptions($encode = false) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Prepare HTML for output |
||
| 197 | * |
||
| 198 | * @return string HTML |
||
| 199 | */ |
||
| 200 | public function render() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Render custom javascript validation code |
||
| 207 | * |
||
| 208 | * @seealso XoopsForm::renderValidationJS |
||
| 209 | */ |
||
| 210 | public function renderValidationJS() |
||
| 227 | } |
||
| 228 |
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.