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 Handler 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 Handler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Handler |
||
| 28 | { |
||
| 29 | use \Jaxon\Utils\Traits\Config; |
||
| 30 | use \Jaxon\Utils\Traits\Translator; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The plugin manager. |
||
| 34 | * |
||
| 35 | * @var Jaxon\Plugin\Manager |
||
| 36 | */ |
||
| 37 | private $xPluginManager; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * An array of arguments received via the GET or POST parameter jxnargs. |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | private $aArgs; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Stores the method that was used to send the arguments from the client. |
||
| 48 | * Will be one of: Jaxon::METHOD_UNKNOWN, Jaxon::METHOD_GET, Jaxon::METHOD_POST. |
||
| 49 | * |
||
| 50 | * @var integer |
||
| 51 | */ |
||
| 52 | private $nMethod; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The constructor |
||
| 56 | * |
||
| 57 | * Get and decode the arguments of the HTTP request |
||
| 58 | */ |
||
| 59 | public function __construct(\Jaxon\Plugin\Manager $xPluginManager) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Converts a string to a boolean var |
||
| 85 | * |
||
| 86 | * @param string $sValue The string to be converted |
||
| 87 | * |
||
| 88 | * @return boolean |
||
| 89 | */ |
||
| 90 | private function __convertStringToBool($sValue) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Strip the slashes from a string |
||
| 113 | * |
||
| 114 | * @param string $sArg The string to be stripped |
||
| 115 | * |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | private function __argumentStripSlashes(&$sArg) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Convert an Jaxon request argument to its value |
||
| 129 | * |
||
| 130 | * Depending of its first char, the Jaxon request argument is converted to a given type. |
||
| 131 | * |
||
| 132 | * @param string $sValue The keys of the options in the file |
||
| 133 | * |
||
| 134 | * @return mixed |
||
| 135 | */ |
||
| 136 | private function __convertValue($sValue) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Decode and convert an Jaxon request argument from JSON |
||
| 161 | * |
||
| 162 | * @param string $sArg The Jaxon request argument |
||
| 163 | * |
||
| 164 | * @return mixed |
||
| 165 | */ |
||
| 166 | private function __argumentDecode(&$sArg) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Decode an Jaxon request argument and convert to UTF8 with iconv |
||
| 204 | * |
||
| 205 | * @param string|array $mArg The Jaxon request argument |
||
| 206 | * |
||
| 207 | * @return void |
||
| 208 | */ |
||
| 209 | private function __argumentDecodeUTF8_iconv(&$mArg) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Decode an Jaxon request argument and convert to UTF8 with mb_convert_encoding |
||
| 234 | * |
||
| 235 | * @param string|array $mArg The Jaxon request argument |
||
| 236 | * |
||
| 237 | * @return void |
||
| 238 | */ |
||
| 239 | View Code Duplication | private function __argumentDecodeUTF8_mb_convert_encoding(&$mArg) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Decode an Jaxon request argument from UTF8 |
||
| 264 | * |
||
| 265 | * @param string|array $mArg The Jaxon request argument |
||
| 266 | * |
||
| 267 | * @return void |
||
| 268 | */ |
||
| 269 | View Code Duplication | private function __argumentDecodeUTF8_utf8_decode(&$mArg) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Return the method that was used to send the arguments from the client |
||
| 296 | * |
||
| 297 | * The method is one of: Jaxon::METHOD_UNKNOWN, Jaxon::METHOD_GET, Jaxon::METHOD_POST. |
||
| 298 | * |
||
| 299 | * @return integer |
||
| 300 | */ |
||
| 301 | public function getRequestMethod() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Return the array of arguments that were extracted and parsed from the GET or POST data |
||
| 308 | * |
||
| 309 | * @return array |
||
| 310 | */ |
||
| 311 | public function processArguments() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Check if the current request can be processed |
||
| 344 | * |
||
| 345 | * Calls each of the request plugins and determines if the current request can be processed by one of them. |
||
| 346 | * If no processor identifies the current request, then the request must be for the initial page load. |
||
| 347 | * |
||
| 348 | * @return boolean |
||
| 349 | */ |
||
| 350 | public function canProcessRequest() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Process the current request |
||
| 364 | * |
||
| 365 | * Calls each of the request plugins to request that they process the current request. |
||
| 366 | * If any plugin processes the request, it will return true. |
||
| 367 | * |
||
| 368 | * @return boolean |
||
| 369 | */ |
||
| 370 | public function processRequest() |
||
| 389 | } |
||
| 390 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..