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 Request 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 Request, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Request { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Check if form was submitted. |
||
| 22 | * |
||
| 23 | * @param string $submitFieldName Name of the submit field (button). |
||
| 24 | * Default: "submitBtn" |
||
| 25 | * |
||
| 26 | * @return bool |
||
| 27 | */ |
||
| 28 | public static function isFormSubmitted($submitFieldName = "submitBtn") { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Verify if field is exists in the request. |
||
| 34 | * |
||
| 35 | * @param mixed $fieldName String name of the field or complex name as array. |
||
| 36 | * @param string $source Http::GET or Http::POST constant. |
||
| 37 | * |
||
| 38 | * @return bool |
||
| 39 | */ |
||
| 40 | public static function issetField($fieldName, $source = null) { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Gets value of the field from $_REQUEST or $_SESSION (is some REQUEST values |
||
| 46 | * needs to be stored by scenario). Also it takes values from $_GET or $_POST |
||
| 47 | * separately if second parameter is passed. |
||
| 48 | * |
||
| 49 | * @param mixed $fieldName String name of the field or complex hierarchy name. |
||
| 50 | * @param string $source Http::GET or Http::POST constant. |
||
| 51 | * |
||
| 52 | * @return mixed Value of the field, NULL otherwise. |
||
| 53 | */ |
||
| 54 | public static function getFieldValue($fieldName, $source = null) { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Returns value of the HTTP GET requet field. |
||
| 95 | * |
||
| 96 | * @param mixed $fieldName String name of the field or complex name as array. |
||
| 97 | * @param mixed $defaultValue Default value. |
||
| 98 | * |
||
| 99 | * @return mixed Value of the field, NULL otherwise. |
||
| 100 | */ |
||
| 101 | View Code Duplication | public static function _get($fieldName, $defaultValue = null) { |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Returns value of the HTTP POST requet field. |
||
| 112 | * |
||
| 113 | * @param mixed $fieldName String name of the field or complex name as array. |
||
| 114 | * @param mixed $defaultValue Default value. |
||
| 115 | * |
||
| 116 | * @return mixed Value of the field, NULL otherwise. |
||
| 117 | */ |
||
| 118 | View Code Duplication | public static function _post($fieldName, $defaultValue = null) { |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Returns value of the HTTP POST or GET requet field. |
||
| 129 | * |
||
| 130 | * @param mixed $fieldName String name of the field or complex name as array. |
||
| 131 | * @param mixed $defaultValue Default value. |
||
| 132 | * |
||
| 133 | * @return mixed Value of the field, NULL otherwise. |
||
| 134 | */ |
||
| 135 | View Code Duplication | public static function _field($fieldName, $defaultValue = null) { |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Returns value of the filter field. |
||
| 146 | * |
||
| 147 | * @param string $filterName Name of the filter field. |
||
| 148 | * @param mixed $defaultValue Default value. |
||
| 149 | * |
||
| 150 | * @return mixed |
||
| 151 | */ |
||
| 152 | public static function _filter($filterName, $defaultValue) { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Sets value of the field or creates new field by pair $fieldName => $fieldValue. |
||
| 158 | * |
||
| 159 | * @global array<mixed> $_FIELDS Global fields list. |
||
| 160 | * @param mixed $fieldName Name of the field as a string or complex name as |
||
| 161 | * an array. |
||
| 162 | * @param mixed $fieldValue Value of the field. |
||
| 163 | * |
||
| 164 | * @throws \Exception |
||
| 165 | */ |
||
| 166 | public static function setFieldValue($fieldName, $fieldValue) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Remembers field in session. |
||
| 174 | * |
||
| 175 | * @param string $fieldName Name of the field. |
||
| 176 | * @param mixed $fieldValue |
||
| 177 | */ |
||
| 178 | public static function rememberField($fieldName, $fieldValue) { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Forget cross session field. |
||
| 184 | * |
||
| 185 | * @param string $fieldName Field name. |
||
| 186 | */ |
||
| 187 | public static function forgetField($fieldName) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Forget all cross session fields. |
||
| 193 | * |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | public static function forgetFields() { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Clean all request parameters from provided source. |
||
| 202 | * |
||
| 203 | * @param string $source Http::GET or Http::POST constant. |
||
| 204 | */ |
||
| 205 | public static function clean($source = null) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Change value of the existing field. |
||
| 222 | * |
||
| 223 | * @global array<mixed> $_FIELDS Global fields list. |
||
| 224 | * @param string $fieldName Name of the field. |
||
| 225 | * @param mixed $fieldValue Value of the field. |
||
| 226 | */ |
||
| 227 | public static function changeFieldValue($fieldName, $fieldValue) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Casts value if the existing field to specified type. |
||
| 239 | * |
||
| 240 | * @global array<mixed> $_FIELDS Global fields list. |
||
| 241 | * @param string $fieldName Name of the field. |
||
| 242 | * @param string $type New field value type. |
||
| 243 | */ |
||
| 244 | public static function castFieldValue($fieldName, $type) { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Normilize all boolean checkboxes even they are not checked. |
||
| 277 | * |
||
| 278 | * @global array $_FIELDS Submitted form fields. |
||
| 279 | * @param array<string> $fieldNames Names of all boolean checkboxes what need |
||
| 280 | * fixes. |
||
| 281 | */ |
||
| 282 | public static function normalizeCheckboxes($fieldNames) { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Removes fields from global fields list. |
||
| 292 | * |
||
| 293 | * @param array<string> $fieldNames Names of all boolean checkboxes what need |
||
| 294 | * fixes (may be list of complex field names). |
||
| 295 | */ |
||
| 296 | public static function removeFields($fieldNames) { |
||
| 303 | |||
| 304 | } |
||
| 305 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: