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 Manager 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 Manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Manager |
||
| 28 | { |
||
| 29 | use \Jaxon\Utils\Traits\Config; |
||
| 30 | use \Jaxon\Utils\Traits\Translator; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * An array of arguments received via the GET or POST parameter jxnargs. |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $aArgs; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Stores the method that was used to send the arguments from the client. |
||
| 41 | * Will be one of: Jaxon::METHOD_UNKNOWN, Jaxon::METHOD_GET, Jaxon::METHOD_POST. |
||
| 42 | * |
||
| 43 | * @var integer |
||
| 44 | */ |
||
| 45 | private $nMethod; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The constructor |
||
| 49 | * |
||
| 50 | * Get and decode the arguments of the HTTP request |
||
| 51 | */ |
||
| 52 | public function __construct() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Converts a string to a boolean var |
||
| 77 | * |
||
| 78 | * @param string $sValue The string to be converted |
||
| 79 | * |
||
| 80 | * @return boolean |
||
| 81 | */ |
||
| 82 | private function __convertStringToBool($sValue) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Strip the slashes from a string |
||
| 105 | * |
||
| 106 | * @param string $sArg The string to be stripped |
||
| 107 | * |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | private function __argumentStripSlashes(&$sArg) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Convert an Jaxon request argument to its value |
||
| 121 | * |
||
| 122 | * Depending of its first char, the Jaxon request argument is converted to a given type. |
||
| 123 | * |
||
| 124 | * @param string $sValue The keys of the options in the file |
||
| 125 | * |
||
| 126 | * @return mixed |
||
| 127 | */ |
||
| 128 | private function __convertValue($sValue) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Decode and convert an Jaxon request argument from JSON |
||
| 153 | * |
||
| 154 | * @param string $sArg The Jaxon request argument |
||
| 155 | * |
||
| 156 | * @return mixed |
||
| 157 | */ |
||
| 158 | private function __argumentDecode(&$sArg) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Decode an Jaxon request argument and convert to UTF8 with iconv |
||
| 179 | * |
||
| 180 | * @param string|array $mArg The Jaxon request argument |
||
| 181 | * |
||
| 182 | * @return void |
||
| 183 | */ |
||
| 184 | private function __argumentDecodeUTF8_iconv(&$mArg) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Decode an Jaxon request argument and convert to UTF8 with mb_convert_encoding |
||
| 209 | * |
||
| 210 | * @param string|array $mArg The Jaxon request argument |
||
| 211 | * |
||
| 212 | * @return void |
||
| 213 | */ |
||
| 214 | View Code Duplication | private function __argumentDecodeUTF8_mb_convert_encoding(&$mArg) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Decode an Jaxon request argument from UTF8 |
||
| 239 | * |
||
| 240 | * @param string|array $mArg The Jaxon request argument |
||
| 241 | * |
||
| 242 | * @return void |
||
| 243 | */ |
||
| 244 | View Code Duplication | private function __argumentDecodeUTF8_utf8_decode(&$mArg) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Return the method that was used to send the arguments from the client |
||
| 271 | * |
||
| 272 | * The method is one of: Jaxon::METHOD_UNKNOWN, Jaxon::METHOD_GET, Jaxon::METHOD_POST. |
||
| 273 | * |
||
| 274 | * @return integer |
||
| 275 | */ |
||
| 276 | public function getRequestMethod() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Return the array of arguments that were extracted and parsed from the GET or POST data |
||
| 283 | * |
||
| 284 | * @return array |
||
| 285 | */ |
||
| 286 | public function process() |
||
| 316 | } |
||
| 317 |
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: