| Total Complexity | 83 |
| Total Lines | 466 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Kint_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.
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 Kint_Parser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class Kint_Parser |
||
| 4 | { |
||
| 5 | public $caller_class; |
||
| 6 | public $max_depth; |
||
| 7 | |||
| 8 | private $marker; |
||
| 9 | private $object_hashes = array(); |
||
| 10 | private $parse_break = false; |
||
| 11 | private $plugins = array(); |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Plugin triggers. |
||
| 15 | * |
||
| 16 | * These are constants indicating trigger points for plugins |
||
| 17 | * |
||
| 18 | * BEGIN: Before normal parsing |
||
| 19 | * SUCCESS: After successful parsing |
||
| 20 | * RECURSION: After parsing cancelled by recursion |
||
| 21 | * DEPTH_LIMIT: After parsing cancelled by depth limit |
||
| 22 | * COMPLETE: SUCCESS | RECURSION | DEPTH_LIMIT |
||
| 23 | * |
||
| 24 | * While a plugin's getTriggers may return any of these |
||
| 25 | */ |
||
| 26 | const TRIGGER_NONE = 0; |
||
| 27 | const TRIGGER_BEGIN = 1; |
||
| 28 | const TRIGGER_SUCCESS = 2; |
||
| 29 | const TRIGGER_RECURSION = 4; |
||
| 30 | const TRIGGER_DEPTH_LIMIT = 8; |
||
| 31 | const TRIGGER_COMPLETE = 14; |
||
| 32 | |||
| 33 | public function __construct($max_depth = false, $c = null) |
||
| 34 | { |
||
| 35 | $this->marker = uniqid("kint\0", true); |
||
| 36 | $this->caller_class = $c; |
||
| 37 | $this->max_depth = $max_depth; |
||
| 38 | } |
||
| 39 | |||
| 40 | public function parse(&$var, Kint_Object $o) |
||
| 41 | { |
||
| 42 | $o->type = strtolower(gettype($var)); |
||
| 43 | |||
| 44 | if (!$this->applyPlugins($var, $o, self::TRIGGER_BEGIN)) { |
||
| 45 | return $o; |
||
| 46 | } |
||
| 47 | |||
| 48 | switch ($o->type) { |
||
| 49 | case 'array': |
||
| 50 | return $this->parseArray($var, $o); |
||
| 51 | case 'boolean': |
||
| 52 | case 'double': |
||
| 53 | case 'integer': |
||
| 54 | case 'null': |
||
| 55 | return $this->parseGeneric($var, $o); |
||
| 56 | case 'object': |
||
| 57 | return $this->parseObject($var, $o); |
||
| 58 | case 'resource': |
||
| 59 | return $this->parseResource($var, $o); |
||
| 60 | case 'string': |
||
| 61 | return $this->parseString($var, $o); |
||
| 62 | default: |
||
| 63 | return $this->parseUnknown($var, $o); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | private function parseGeneric(&$var, Kint_Object $o) |
||
| 68 | { |
||
| 69 | $rep = new Kint_Object_Representation('Contents'); |
||
| 70 | $rep->contents = $var; |
||
| 71 | $rep->implicit_label = true; |
||
| 72 | $o->addRepresentation($rep); |
||
| 73 | |||
| 74 | $this->applyPlugins($var, $o, self::TRIGGER_SUCCESS); |
||
| 75 | |||
| 76 | return $o; |
||
| 77 | } |
||
| 78 | |||
| 79 | private function parseString(&$var, Kint_Object $o) |
||
| 80 | { |
||
| 81 | $string = $o->transplant(new Kint_Object_Blob()); |
||
| 82 | $string->encoding = Kint_Object_Blob::detectEncoding($var); |
||
| 83 | $string->size = Kint_Object_Blob::strlen($var, $string->encoding); |
||
| 84 | |||
| 85 | $rep = new Kint_Object_Representation('Contents'); |
||
| 86 | $rep->contents = $var; |
||
| 87 | $rep->implicit_label = true; |
||
| 88 | |||
| 89 | $string->addRepresentation($rep); |
||
| 90 | |||
| 91 | $this->applyPlugins($var, $string, self::TRIGGER_SUCCESS); |
||
| 92 | |||
| 93 | return $string; |
||
| 94 | } |
||
| 95 | |||
| 96 | private function parseArray(array &$var, Kint_Object $o) |
||
| 97 | { |
||
| 98 | $array = $o->transplant(new Kint_Object()); |
||
| 99 | $array->size = count($var); |
||
| 100 | |||
| 101 | if (isset($var[$this->marker])) { |
||
| 102 | --$array->size; |
||
| 103 | $array->hints[] = 'recursion'; |
||
| 104 | |||
| 105 | $this->applyPlugins($var, $array, self::TRIGGER_RECURSION); |
||
| 106 | |||
| 107 | return $array; |
||
| 108 | } |
||
| 109 | |||
| 110 | $rep = new Kint_Object_Representation('Contents'); |
||
| 111 | $rep->implicit_label = true; |
||
| 112 | $array->addRepresentation($rep); |
||
| 113 | |||
| 114 | if ($array->size) { |
||
| 115 | if ($this->max_depth && $o->depth >= $this->max_depth) { |
||
| 116 | $array->hints[] = 'depth_limit'; |
||
| 117 | |||
| 118 | $this->applyPlugins($var, $array, self::TRIGGER_DEPTH_LIMIT); |
||
| 119 | |||
| 120 | return $array; |
||
| 121 | } |
||
| 122 | |||
| 123 | // Don't even bother with reference checking below 5.2.2. It's an |
||
| 124 | // absolute nightmare. The foreach loop depends on the array pointer |
||
| 125 | // which "conveniently" moves about semi-randomly when you alter |
||
| 126 | // the value you're looping over by means of a reference. |
||
| 127 | if (KINT_PHP522) { |
||
| 128 | $copy = array_values($var); |
||
| 129 | } |
||
| 130 | |||
| 131 | // It's really really hard to access numeric string keys in arrays, |
||
| 132 | // and it's really really hard to access integer properties in |
||
| 133 | // objects, so we just use array_values and index by counter to get |
||
| 134 | // at it reliably for reference testing. This also affects access |
||
| 135 | // paths since it's pretty much impossible to access these things |
||
| 136 | // without complicated stuff you should never need to do. |
||
| 137 | $i = 0; |
||
| 138 | |||
| 139 | // Set the marker for recursion |
||
| 140 | $var[$this->marker] = $array->depth; |
||
| 141 | |||
| 142 | foreach ($var as $key => &$val) { |
||
| 143 | if ($key === $this->marker) { |
||
| 144 | continue; |
||
| 145 | } |
||
| 146 | |||
| 147 | $child = new Kint_Object(); |
||
| 148 | $child->name = $key; |
||
| 149 | $child->depth = $array->depth + 1; |
||
| 150 | $child->access = Kint_Object::ACCESS_NONE; |
||
| 151 | $child->operator = Kint_Object::OPERATOR_ARRAY; |
||
| 152 | |||
| 153 | if ($array->access_path !== null) { |
||
| 154 | if (is_string($key) && (string) (int) $key === $key) { |
||
| 155 | $child->access_path = 'array_values('.$array->access_path.')['.$i.']'; |
||
| 156 | } else { |
||
| 157 | $child->access_path = $array->access_path.'['.var_export($key, true).']'; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | if (KINT_PHP522) { |
||
| 162 | $stash = $val; |
||
| 163 | $copy[$i] = $this->marker; |
||
| 164 | if ($val === $this->marker) { |
||
| 165 | $child->reference = true; |
||
| 166 | $val = $stash; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | $rep->contents[] = $this->parse($val, $child); |
||
| 171 | ++$i; |
||
| 172 | } |
||
| 173 | |||
| 174 | $this->applyPlugins($var, $array, self::TRIGGER_SUCCESS); |
||
| 175 | unset($var[$this->marker]); |
||
| 176 | |||
| 177 | return $array; |
||
| 178 | } else { |
||
| 179 | $this->applyPlugins($var, $array, self::TRIGGER_SUCCESS); |
||
| 180 | |||
| 181 | return $array; |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | private function parseObject(&$var, Kint_Object $o) |
||
| 314 | } |
||
| 315 | |||
| 316 | private function parseResource(&$var, Kint_Object $o) |
||
| 317 | { |
||
| 318 | $resource = $o->transplant(new Kint_Object_Resource()); |
||
| 319 | $resource->resource_type = get_resource_type($var); |
||
| 320 | |||
| 321 | $this->applyPlugins($var, $resource, self::TRIGGER_SUCCESS); |
||
| 322 | |||
| 323 | return $resource; |
||
| 324 | } |
||
| 325 | |||
| 326 | private function parseUnknown(&$var, Kint_Object $o) |
||
| 327 | { |
||
| 328 | $o->type = 'unknown'; |
||
| 329 | $this->applyPlugins($var, $o, self::TRIGGER_SUCCESS); |
||
| 330 | |||
| 331 | return $o; |
||
| 332 | } |
||
| 333 | |||
| 334 | public function addPlugin(Kint_Parser_Plugin $p) |
||
| 335 | { |
||
| 336 | if (!$types = $p->getTypes()) { |
||
| 337 | return false; |
||
| 338 | } |
||
| 339 | |||
| 340 | if (!$triggers = $p->getTriggers()) { |
||
| 341 | return false; |
||
| 342 | } |
||
| 343 | |||
| 344 | $p->setParser($this); |
||
| 345 | |||
| 346 | foreach ($types as $type) { |
||
| 347 | if (!isset($this->plugins[$type])) { |
||
| 348 | $this->plugins[$type] = array( |
||
| 349 | self::TRIGGER_BEGIN => array(), |
||
| 350 | self::TRIGGER_SUCCESS => array(), |
||
| 351 | self::TRIGGER_RECURSION => array(), |
||
| 352 | self::TRIGGER_DEPTH_LIMIT => array(), |
||
| 353 | ); |
||
| 354 | } |
||
| 355 | |||
| 356 | foreach ($this->plugins[$type] as $trigger => &$pool) { |
||
| 357 | if ($triggers & $trigger) { |
||
| 358 | $pool[] = $p; |
||
| 359 | } |
||
| 360 | } |
||
| 361 | } |
||
| 362 | |||
| 363 | return true; |
||
| 364 | } |
||
| 365 | |||
| 366 | public function clearPlugins() |
||
| 367 | { |
||
| 368 | $this->plugins = array(); |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Applies plugins for an object type. |
||
| 373 | * |
||
| 374 | * @param mixed &$var variable |
||
| 375 | * @param Kint_Object &$o Kint object parsed so far |
||
| 376 | * @param int $trigger The trigger to check for the plugins |
||
| 377 | * |
||
| 378 | * @return bool Continue parsing |
||
| 379 | */ |
||
| 380 | private function applyPlugins(&$var, Kint_Object &$o, $trigger) |
||
| 381 | { |
||
| 382 | $break_stash = $this->parse_break; |
||
| 383 | $this->parse_break = false; |
||
| 384 | |||
| 385 | $plugins = array(); |
||
| 386 | |||
| 387 | if (isset($this->plugins[$o->type][$trigger])) { |
||
| 388 | $plugins = $this->plugins[$o->type][$trigger]; |
||
| 389 | } |
||
| 390 | |||
| 391 | foreach ($plugins as $plugin) { |
||
| 392 | try { |
||
| 393 | $plugin->parse($var, $o, $trigger); |
||
| 394 | } catch (Exception $e) { |
||
| 395 | trigger_error( |
||
| 396 | 'An exception ('.get_class($e).') was thrown in '.$e->getFile().' on line '.$e->getLine().' while executing Kint Parser Plugin "'.get_class($plugin).'". Error message: '.$e->getMessage(), |
||
| 397 | E_USER_WARNING |
||
| 398 | ); |
||
| 399 | } |
||
| 400 | |||
| 401 | if ($this->parse_break) { |
||
| 402 | $this->parse_break = $break_stash; |
||
| 403 | |||
| 404 | return false; |
||
| 405 | } |
||
| 406 | } |
||
| 407 | |||
| 408 | $this->parse_break = $break_stash; |
||
| 409 | |||
| 410 | return true; |
||
| 411 | } |
||
| 412 | |||
| 413 | public function haltParse() |
||
| 416 | } |
||
| 417 | |||
| 418 | public function childHasPath(Kint_Object_Instance $parent, Kint_Object $child) |
||
| 419 | { |
||
| 420 | if ($parent->type === 'object' && ($parent->access_path !== null || $child->static || $child->const)) { |
||
| 421 | if ($child->access === Kint_Object::ACCESS_PUBLIC) { |
||
| 422 | return true; |
||
| 423 | } elseif ($child->access === Kint_Object::ACCESS_PRIVATE && $this->caller_class && $this->caller_class === $child->owner_class) { |
||
| 424 | // We can't accurately determine owner class on statics / consts below 5.3 so deny |
||
| 425 | // the access path just to be sure. See ClassStatics for more info |
||
| 426 | if (KINT_PHP53 || (!$child->static && !$child->const)) { |
||
| 427 | return true; |
||
| 428 | } |
||
| 429 | } elseif ($child->access === Kint_Object::ACCESS_PROTECTED && $this->caller_class) { |
||
| 430 | if (is_a($this->caller_class, $child->owner_class, true) || is_a($child->owner_class, $this->caller_class, true)) { |
||
| 431 | return true; |
||
| 432 | } |
||
| 433 | } |
||
| 434 | } |
||
| 435 | |||
| 436 | return false; |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Returns an array without the recursion marker in it. |
||
| 441 | * |
||
| 442 | * DO NOT pass an array that has had it's marker removed back |
||
| 443 | * into the parser, it will result in an extra recursion |
||
| 444 | * |
||
| 445 | * @param array $array Array potentially containing a recursion marker |
||
| 446 | * |
||
| 447 | * @return array Array with recursion marker removed |
||
| 448 | */ |
||
| 449 | public function getCleanArray(array $array) |
||
| 450 | { |
||
| 451 | unset($array[$this->marker]); |
||
| 452 | |||
| 453 | return $array; |
||
| 454 | } |
||
| 455 | |||
| 456 | private static function sortObjectProperties(Kint_Object $a, Kint_Object $b) |
||
| 469 | } |
||
| 470 | } |
||
| 471 |