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) |
|
| 73 | { |
||
| 74 | 9 | $keys = is_array($keys) ? $keys : func_get_args(); |
|
| 75 | |||
| 76 | 9 | $results = []; |
|
| 77 | 9 | foreach ($keys as $key) { |
|
| 78 | 9 | $results = array_merge_recursive($results, $this->buildArray(explode('.', $key), $this->get($key))); |
|
| 79 | 6 | } |
|
| 80 | |||
| 81 | 9 | return $results; |
|
| 82 | } |
||
| 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) |
|
| 91 | { |
||
| 92 | 6 | $keys = is_array($keys) ? $keys : func_get_args(); |
|
| 93 | |||
| 94 | 6 | $results = $this->payload(); |
|
| 95 | |||
| 96 | 6 | foreach ($keys as $key) { |
|
| 97 | 6 | $this->removeValue($results, $key); |
|
| 98 | 4 | } |
|
| 99 | 6 | return $results; |
|
| 100 | } |
||
| 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) |
|
| 110 | { |
||
| 111 | 21 | $keys = is_array($keys) ? $keys : func_get_args(); |
|
| 112 | |||
| 113 | 21 | $results = $this->payload(); |
|
| 114 | |||
| 115 | 21 | foreach ($keys as $value) { |
|
| 116 | 21 | if ($this->hasValueAtKey($value, $results) === false) { |
|
| 117 | 19 | return false; |
|
| 118 | } |
||
| 119 | 14 | } |
|
| 120 | 21 | return true; |
|
| 121 | } |
||
| 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) |
|
| 132 | { |
||
| 133 | 18 | if ($this->has($key)) { |
|
| 134 | 18 | return $this->getValueAtKey($key, $this->payload()); |
|
| 135 | } |
||
| 136 | 9 | return $default; |
|
| 137 | } |
||
| 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) |
|
| 147 | { |
||
| 148 | 3 | $keys = []; |
|
| 149 | 3 | View Code Duplication | foreach ($mask as $key => $value) { |
| 150 | 3 | $keys[] = $key . (is_array($value) ? $this->processMask($value) : ''); |
|
| 151 | 2 | } |
|
| 152 | |||
| 153 | 3 | return $this->only($keys); |
|
| 154 | } |
||
| 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) |
|
| 164 | { |
||
| 165 | 3 | View Code Duplication | foreach ($mask as $key => $value) { |
| 166 | 3 | return '.' . $key . (is_array($value) ? $this->processMask($value) : ''); |
|
| 167 | } |
||
| 168 | } |
||
| 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 = '') |
|
| 179 | { |
||
| 180 | 57 | $class = $this->getFormatClass($format); |
|
| 181 | 57 | return $this->parse($this->getPayload(), new $class); |
|
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Alias to the payload function. |
||
| 186 | * |
||
| 187 | * @return array |
||
| 188 | */ |
||
| 189 | 3 | public function all() |
|
| 190 | { |
||
| 191 | 3 | return $this->payload(); |
|
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Autodetect the payload data type using content-type value. |
||
| 196 | * |
||
| 197 | * @return string Return the short format code (xml, json, ...). |
||
| 198 | */ |
||
| 199 | 66 | public function getFormatClass($format = '') |
|
|
|
|||
| 200 | { |
||
| 201 | 66 | if ( ! empty($format)) { |
|
| 202 | 9 | return $this->processContentType($format); |
|
| 203 | } |
||
| 204 | |||
| 205 | 57 | if (isset($_SERVER['CONTENT_TYPE'])) { |
|
| 206 | 18 | $type = $this->processContentType($_SERVER['CONTENT_TYPE']); |
|
| 207 | 18 | if ($type !== false) { |
|
| 208 | 3 | return $type; |
|
| 209 | } |
||
| 210 | 12 | } |
|
| 211 | |||
| 212 | 57 | if (isset($_SERVER['HTTP_CONTENT_TYPE'])) { |
|
| 213 | 27 | $type = $this->processContentType($_SERVER['HTTP_CONTENT_TYPE']); |
|
| 214 | 27 | if ($type !== false) { |
|
| 215 | 21 | return $type; |
|
| 216 | } |
||
| 217 | 4 | } |
|
| 218 | |||
| 219 | 36 | return 'Nathanmac\Utilities\Parser\Formats\JSON'; |
|
| 220 | } |
||
| 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) |
|
| 230 | { |
||
| 231 | 36 | foreach (explode(';', $contentType) as $type) { |
|
| 232 | 36 | $type = strtolower(trim($type)); |
|
| 233 | 36 | if (isset($this->supported_formats[$type])) { |
|
| 234 | 34 | return $this->supported_formats[$type]; |
|
| 235 | } |
||
| 236 | 12 | } |
|
| 237 | |||
| 238 | 18 | return false; |
|
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Return the payload data from the HTTP post request. |
||
| 243 | * |
||
| 244 | * @codeCoverageIgnore |
||
| 245 | * |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | protected function getPayload() |
||
| 249 | { |
||
| 250 | return file_get_contents('php://input'); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Parse payload string using given formatter. |
||
| 255 | * |
||
| 256 | * @param string $payload |
||
| 257 | * @param FormatInterface $format |
||
| 258 | * |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | 105 | public function parse($payload, FormatInterface $format) |
|
| 262 | { |
||
| 263 | 105 | return $format->parse($payload); |
|
| 264 | } |
||
| 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) |
|
| 279 | { |
||
| 280 | 3 | if ( ! class_exists($class)) { |
|
| 281 | throw new \InvalidArgumentException("Parser formatter class {$class} not found."); |
||
| 282 | } |
||
| 283 | 3 | if ( ! is_a($class, 'Nathanmac\Utilities\Parser\Formats\FormatInterface', true)) { |
|
| 284 | throw new \InvalidArgumentException('Parser formatters must implement the Nathanmac\Utilities\Parser\Formats\FormatInterface interface.'); |
||
| 285 | } |
||
| 286 | |||
| 287 | 3 | $this->supported_formats[$format] = $class; |
|
| 288 | |||
| 289 | 3 | return $this; |
|
| 290 | } |
||
| 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 | 9 | public function xml($payload) |
|
| 304 | { |
||
| 305 | 9 | return $this->parse($payload, new XML()); |
|
| 306 | } |
||
| 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) |
|
| 317 | { |
||
| 318 | 9 | return $this->parse($payload, new JSON()); |
|
| 319 | } |
||
| 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) |
|
| 331 | { |
||
| 332 | 3 | return $this->parse($payload, new BSON()); |
|
| 333 | } |
||
| 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) |
|
| 345 | { |
||
| 346 | 9 | return $this->parse($payload, new Serialize()); |
|
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Query String parser, helper function. |
||
| 351 | * |
||
| 352 | * @param $payload |
||
| 353 | * |
||
| 354 | * @return array |
||
| 355 | */ |
||
| 356 | 6 | public function querystr($payload) |
|
| 357 | { |
||
| 358 | 6 | return $this->parse($payload, new QueryStr()); |
|
| 359 | } |
||
| 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) |
|
| 371 | { |
||
| 372 | 9 | return $this->parse($payload, new Yaml()); |
|
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * MSGPack parser, helper function. |
||
| 377 | * |
||
| 378 | * @param $payload |
||
| 379 | * |
||
| 380 | * @throws Exceptions\ParserException |
||
| 381 | * |
||
| 382 | * @return array |
||
| 383 | */ |
||
| 384 | 3 | 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) |
|
| 399 | { |
||
| 400 | 18 | $keys = explode('.', $key); |
|
| 401 | |||
| 402 | 18 | while (count($keys) > 1) { |
|
| 403 | 12 | $key = array_shift($keys); |
|
| 404 | |||
| 405 | // Wildcard Key |
||
| 406 | 12 | if (preg_match($this->wildcards, $key) && is_array($data) && ! empty($data)) { |
|
| 407 | // Shift the first item of the array |
||
| 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 |
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: