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) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Decode an Jaxon request argument and convert to UTF8 with iconv |
||
| 196 | * |
||
| 197 | * @param string|array $mArg The Jaxon request argument |
||
| 198 | * |
||
| 199 | * @return void |
||
| 200 | */ |
||
| 201 | private function __argumentDecodeUTF8_iconv(&$mArg) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Decode an Jaxon request argument and convert to UTF8 with mb_convert_encoding |
||
| 226 | * |
||
| 227 | * @param string|array $mArg The Jaxon request argument |
||
| 228 | * |
||
| 229 | * @return void |
||
| 230 | */ |
||
| 231 | View Code Duplication | private function __argumentDecodeUTF8_mb_convert_encoding(&$mArg) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Decode an Jaxon request argument from UTF8 |
||
| 256 | * |
||
| 257 | * @param string|array $mArg The Jaxon request argument |
||
| 258 | * |
||
| 259 | * @return void |
||
| 260 | */ |
||
| 261 | View Code Duplication | private function __argumentDecodeUTF8_utf8_decode(&$mArg) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Return the method that was used to send the arguments from the client |
||
| 288 | * |
||
| 289 | * The method is one of: Jaxon::METHOD_UNKNOWN, Jaxon::METHOD_GET, Jaxon::METHOD_POST. |
||
| 290 | * |
||
| 291 | * @return integer |
||
| 292 | */ |
||
| 293 | public function getRequestMethod() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Return the array of arguments that were extracted and parsed from the GET or POST data |
||
| 300 | * |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | public function process() |
||
| 333 | } |
||
| 334 |
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.