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 Parser 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 Parser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Parser |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | private $wildcards = '/^(\*|%|:first|:last|:(index|item)\[\d+\])$/'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array Supported Formats |
||
| 30 | */ |
||
| 31 | private $supported_formats = [ |
||
| 32 | // XML |
||
| 33 | 'application/xml' => 'Nathanmac\Utilities\Parser\Formats\XML', |
||
| 34 | 'text/xml' => 'Nathanmac\Utilities\Parser\Formats\XML', |
||
| 35 | 'xml' => 'Nathanmac\Utilities\Parser\Formats\XML', |
||
| 36 | // JSON |
||
| 37 | 'application/json' => 'Nathanmac\Utilities\Parser\Formats\JSON', |
||
| 38 | 'application/x-javascript' => 'Nathanmac\Utilities\Parser\Formats\JSON', |
||
| 39 | 'text/javascript' => 'Nathanmac\Utilities\Parser\Formats\JSON', |
||
| 40 | 'text/x-javascript' => 'Nathanmac\Utilities\Parser\Formats\JSON', |
||
| 41 | 'text/x-json' => 'Nathanmac\Utilities\Parser\Formats\JSON', |
||
| 42 | 'json' => 'Nathanmac\Utilities\Parser\Formats\JSON', |
||
| 43 | // BSON |
||
| 44 | 'application/bson' => 'Nathanmac\Utilities\Parser\Formats\BSON', |
||
| 45 | 'bson' => 'Nathanmac\Utilities\Parser\Formats\BSON', |
||
| 46 | // YAML |
||
| 47 | 'text/yaml' => 'Nathanmac\Utilities\Parser\Formats\YAML', |
||
| 48 | 'text/x-yaml' => 'Nathanmac\Utilities\Parser\Formats\YAML', |
||
| 49 | 'application/yaml' => 'Nathanmac\Utilities\Parser\Formats\YAML', |
||
| 50 | 'application/x-yaml' => 'Nathanmac\Utilities\Parser\Formats\YAML', |
||
| 51 | 'yaml' => 'Nathanmac\Utilities\Parser\Formats\YAML', |
||
| 52 | // MSGPACK |
||
| 53 | 'application/msgpack' => 'Nathanmac\Utilities\Parser\Formats\MSGPack', |
||
| 54 | 'application/x-msgpack' => 'Nathanmac\Utilities\Parser\Formats\MSGPack', |
||
| 55 | 'msgpack' => 'Nathanmac\Utilities\Parser\Formats\MSGPack', |
||
| 56 | // MISC |
||
| 57 | 'application/vnd.php.serialized' => 'Nathanmac\Utilities\Parser\Formats\Serialize', |
||
| 58 | 'serialize' => 'Nathanmac\Utilities\Parser\Formats\Serialize', |
||
| 59 | 'application/x-www-form-urlencoded' => 'Nathanmac\Utilities\Parser\Formats\QueryStr', |
||
| 60 | 'querystr' => 'Nathanmac\Utilities\Parser\Formats\QueryStr', |
||
| 61 | ]; |
||
| 62 | |||
| 63 | /* ------------ Access Methods/Helpers ------------ */ |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Get a subset of the items from the payload data. |
||
| 67 | * |
||
| 68 | * @param string|array $keys |
||
| 69 | * |
||
| 70 | * @return array |
||
| 71 | */ |
||
| 72 | 9 | public function only($keys) |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Get all of the input except for a specified array of items. |
||
| 86 | * |
||
| 87 | * @param string|array $keys |
||
| 88 | * @return array |
||
| 89 | */ |
||
| 90 | 6 | public function except($keys) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Determine if the payload contains a non-empty value for a given key. |
||
| 104 | * |
||
| 105 | * @param string|array $keys |
||
| 106 | * |
||
| 107 | * @return bool |
||
| 108 | */ |
||
| 109 | 21 | public function has($keys) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Retrieve an payload item from the payload data, return default item if item not found. |
||
| 125 | * |
||
| 126 | * @param string $key |
||
| 127 | * @param string $default |
||
| 128 | * |
||
| 129 | * @return mixed|null |
||
| 130 | */ |
||
| 131 | 18 | public function get($key = null, $default = null) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Mask input data with a given mapping. |
||
| 141 | * |
||
| 142 | * @param array $mask |
||
| 143 | * |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | 3 | public function mask(array $mask) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Recursive processor for processing user masks. |
||
| 158 | * |
||
| 159 | * @param array $mask |
||
| 160 | * |
||
| 161 | * @return string |
||
| 162 | */ |
||
| 163 | 3 | private function processMask($mask) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Parse the HTTP payload data, autodetect format and return all data in array. |
||
| 172 | * Override the format by providing a content type. |
||
| 173 | * |
||
| 174 | * @param string $format |
||
| 175 | * |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | 57 | public function payload($format = '') |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Alias to the payload function. |
||
| 186 | * |
||
| 187 | * @return array |
||
| 188 | */ |
||
| 189 | 3 | public function all() |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Autodetect the payload data type using content-type value. |
||
| 196 | * |
||
| 197 | * @return string Return the name of the formatter class. |
||
| 198 | */ |
||
| 199 | 66 | public function getFormatClass($format = '') |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Process the content-type values |
||
| 224 | * |
||
| 225 | * @param string $contentType Content-Type raw string |
||
| 226 | * |
||
| 227 | * @return bool|string |
||
| 228 | */ |
||
| 229 | 36 | private function processContentType($contentType) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Return the payload data from the HTTP post request. |
||
| 243 | * |
||
| 244 | * @codeCoverageIgnore |
||
| 245 | * |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | protected function getPayload() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Parse payload string using given formatter. |
||
| 255 | * |
||
| 256 | * @param string $payload |
||
| 257 | * @param FormatInterface $format |
||
| 258 | * |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | 136 | public function parse($payload, FormatInterface $format) |
|
| 265 | |||
| 266 | /* ------------ Format Registration Methods ------------ */ |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Register Format Class. |
||
| 270 | * |
||
| 271 | * @param $format |
||
| 272 | * @param $class |
||
| 273 | * |
||
| 274 | * @throws InvalidArgumentException |
||
| 275 | * |
||
| 276 | * @return self |
||
| 277 | */ |
||
| 278 | 3 | public function registerFormat($format, $class) |
|
| 291 | |||
| 292 | /* ------------ Helper Methods ------------ */ |
||
| 293 | |||
| 294 | /** |
||
| 295 | * XML parser, helper function. |
||
| 296 | * |
||
| 297 | * @param $payload |
||
| 298 | * |
||
| 299 | * @throws Exceptions\ParserException |
||
| 300 | * |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | 36 | public function xml($payload) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * JSON parser, helper function. |
||
| 310 | * |
||
| 311 | * @param $payload |
||
| 312 | * |
||
| 313 | * @throws Exceptions\ParserException |
||
| 314 | * @return array |
||
| 315 | */ |
||
| 316 | 9 | public function json($payload) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * BSON parser, helper function. |
||
| 323 | * |
||
| 324 | * @param $payload |
||
| 325 | * |
||
| 326 | * @throws Exceptions\ParserException |
||
| 327 | * |
||
| 328 | * @return array |
||
| 329 | */ |
||
| 330 | 3 | public function bson($payload) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Serialized Data parser, helper function. |
||
| 337 | * |
||
| 338 | * @param $payload |
||
| 339 | * |
||
| 340 | * @throws Exceptions\ParserException |
||
| 341 | * |
||
| 342 | * @return array |
||
| 343 | */ |
||
| 344 | 9 | public function serialize($payload) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Query String parser, helper function. |
||
| 351 | * |
||
| 352 | * @param $payload |
||
| 353 | * |
||
| 354 | * @return array |
||
| 355 | */ |
||
| 356 | 6 | public function querystr($payload) |
|
| 360 | |||
| 361 | /** |
||
| 362 | * YAML parser, helper function. |
||
| 363 | * |
||
| 364 | * @param $payload |
||
| 365 | * |
||
| 366 | * @throws Exceptions\ParserException |
||
| 367 | * |
||
| 368 | * @return array |
||
| 369 | */ |
||
| 370 | 9 | public function yaml($payload) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * MSGPack parser, helper function. |
||
| 377 | * |
||
| 378 | * @param $payload |
||
| 379 | * |
||
| 380 | * @throws Exceptions\ParserException |
||
| 381 | * |
||
| 382 | * @return array |
||
| 383 | */ |
||
| 384 | 7 | public function msgpack($payload) |
|
| 388 | |||
| 389 | /* ------------ Construction Methods ------------ */ |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Return a value from the array identified from the key. |
||
| 393 | * |
||
| 394 | * @param $key |
||
| 395 | * @param $data |
||
| 396 | * @return mixed |
||
| 397 | */ |
||
| 398 | 18 | private function getValueAtKey($key, $data) |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Array contains a value identified from the key, returns bool |
||
| 451 | * |
||
| 452 | * @param $key |
||
| 453 | * @param $data |
||
| 454 | * @return bool |
||
| 455 | */ |
||
| 456 | 21 | private function hasValueAtKey($key, $data) |
|
| 497 | |||
| 498 | /** |
||
| 499 | * Build the array structure for value. |
||
| 500 | * |
||
| 501 | * @param $route |
||
| 502 | * @param null $data |
||
| 503 | * @return array|null |
||
| 504 | */ |
||
| 505 | 9 | private function buildArray($route, $data = null) |
|
| 514 | |||
| 515 | /** |
||
| 516 | * Remove a value identified from the key |
||
| 517 | * |
||
| 518 | * @param $array |
||
| 519 | * @param $key |
||
| 520 | */ |
||
| 521 | 6 | private function removeValue(&$array, $key) |
|
| 537 | } |
||
| 538 |