| Total Complexity | 94 |
| Total Lines | 634 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Kint 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, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class Kint |
||
| 4 | { |
||
| 5 | /** |
||
| 6 | * @var mixed Kint mode |
||
| 7 | * |
||
| 8 | * false: Disabled |
||
| 9 | * true: Enabled, default mode selection |
||
| 10 | * string: Manual mode selection |
||
| 11 | */ |
||
| 12 | public static $enabled_mode = true; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Default mode. |
||
| 16 | * |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | public static $mode_default = self::MODE_RICH; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Default mode in CLI with cli_detection on. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | public static $mode_default_cli = self::MODE_CLI; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var bool Return output instead of echoing |
||
| 30 | */ |
||
| 31 | public static $return; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string format of the link to the source file in trace entries. |
||
| 35 | * |
||
| 36 | * Use %f for file path, %l for line number. |
||
| 37 | * |
||
| 38 | * [!] EXAMPLE (works with for phpStorm and RemoteCall Plugin): |
||
| 39 | * |
||
| 40 | * Kint::$file_link_format = 'http://localhost:8091/?message=%f:%l'; |
||
| 41 | */ |
||
| 42 | public static $file_link_format = ''; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var bool whether to display where kint was called from |
||
| 46 | */ |
||
| 47 | public static $display_called_from = true; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array base directories of your application that will be displayed instead of the full path. |
||
| 51 | * |
||
| 52 | * Keys are paths, values are replacement strings |
||
| 53 | * |
||
| 54 | * [!] EXAMPLE (for Kohana framework): |
||
| 55 | * |
||
| 56 | * Kint::$app_root_dirs = array( |
||
| 57 | * APPPATH => 'APPPATH', |
||
| 58 | * SYSPATH => 'SYSPATH', |
||
| 59 | * MODPATH => 'MODPATH', |
||
| 60 | * DOCROOT => 'DOCROOT', |
||
| 61 | * ); |
||
| 62 | * |
||
| 63 | * [!] EXAMPLE #2 (for a semi-universal approach) |
||
| 64 | * |
||
| 65 | * Kint::$app_root_dirs = array( |
||
| 66 | * realpath( __DIR__ . '/../../..' ) => 'ROOT', // go up as many levels as needed |
||
| 67 | * ); |
||
| 68 | */ |
||
| 69 | public static $app_root_dirs = array(); |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var int max array/object levels to go deep, if zero no limits are applied |
||
| 73 | */ |
||
| 74 | public static $max_depth = 6; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var bool expand all trees by default for rich view |
||
| 78 | */ |
||
| 79 | public static $expanded = false; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var bool enable detection when Kint is command line. |
||
| 83 | * |
||
| 84 | * Formats output with whitespace only; does not HTML-escape it |
||
| 85 | */ |
||
| 86 | public static $cli_detection = true; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var array Kint aliases. Add debug functions in Kint wrappers here to fix modifiers and backtraces |
||
| 90 | */ |
||
| 91 | public static $aliases = array( |
||
| 92 | array('Kint', 'dump'), |
||
| 93 | array('Kint', 'trace'), |
||
| 94 | array('Kint', 'dumpArray'), |
||
| 95 | ); |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var array Kint_Renderer descendants. Add to array to extend. |
||
| 99 | */ |
||
| 100 | public static $renderers = array( |
||
| 101 | self::MODE_RICH => 'Kint_Renderer_Rich', |
||
| 102 | self::MODE_PLAIN => 'Kint_Renderer_Plain', |
||
| 103 | self::MODE_TEXT => 'Kint_Renderer_Text', |
||
| 104 | self::MODE_CLI => 'Kint_Renderer_Cli', |
||
| 105 | ); |
||
| 106 | |||
| 107 | const MODE_RICH = 'r'; |
||
| 108 | const MODE_TEXT = 't'; |
||
| 109 | const MODE_CLI = 'c'; |
||
| 110 | const MODE_PLAIN = 'p'; |
||
| 111 | |||
| 112 | public static $plugins = array( |
||
| 113 | 'Kint_Parser_Base64', |
||
| 114 | 'Kint_Parser_Blacklist', |
||
| 115 | 'Kint_Parser_ClassMethods', |
||
| 116 | 'Kint_Parser_ClassStatics', |
||
| 117 | 'Kint_Parser_Closure', |
||
| 118 | 'Kint_Parser_Color', |
||
| 119 | 'Kint_Parser_DateTime', |
||
| 120 | 'Kint_Parser_FsPath', |
||
| 121 | 'Kint_Parser_Iterator', |
||
| 122 | 'Kint_Parser_Json', |
||
| 123 | 'Kint_Parser_Microtime', |
||
| 124 | 'Kint_Parser_SimpleXMLElement', |
||
| 125 | 'Kint_Parser_SplFileInfo', |
||
| 126 | 'Kint_Parser_SplObjectStorage', |
||
| 127 | 'Kint_Parser_Stream', |
||
| 128 | 'Kint_Parser_Table', |
||
| 129 | 'Kint_Parser_Throwable', |
||
| 130 | 'Kint_Parser_Timestamp', |
||
| 131 | 'Kint_Parser_ToString', |
||
| 132 | 'Kint_Parser_Trace', |
||
| 133 | 'Kint_Parser_Xml', |
||
| 134 | ); |
||
| 135 | |||
| 136 | private static $plugin_pool = array(); |
||
| 137 | private static $dump_array = false; |
||
| 138 | private static $names = array(); |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Stashes or sets all settings at once. |
||
| 142 | * |
||
| 143 | * @param array|null $settings Array of all settings to be set or null to set none |
||
| 144 | * |
||
| 145 | * @return array Current settings |
||
| 146 | */ |
||
| 147 | public static function settings(array $settings = null) |
||
| 148 | { |
||
| 149 | static $keys = array( |
||
| 150 | 'aliases', |
||
| 151 | 'app_root_dirs', |
||
| 152 | 'cli_detection', |
||
| 153 | 'display_called_from', |
||
| 154 | 'enabled_mode', |
||
| 155 | 'expanded', |
||
| 156 | 'file_link_format', |
||
| 157 | 'max_depth', |
||
| 158 | 'mode_default', |
||
| 159 | 'mode_default_cli', |
||
| 160 | 'renderers', |
||
| 161 | 'return', |
||
| 162 | 'plugins', |
||
| 163 | ); |
||
| 164 | |||
| 165 | $out = array(); |
||
| 166 | |||
| 167 | foreach ($keys as $key) { |
||
| 168 | $out[$key] = self::$$key; |
||
| 169 | } |
||
| 170 | |||
| 171 | if ($settings !== null) { |
||
| 172 | $in = array_intersect_key($settings, $out); |
||
| 173 | foreach ($in as $key => $val) { |
||
| 174 | self::$$key = $val; |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | return $out; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Prints a debug backtrace, same as Kint::dump(1). |
||
| 183 | * |
||
| 184 | * @param array $trace [OPTIONAL] you can pass your own trace, otherwise, `debug_backtrace` will be called |
||
| 185 | * |
||
| 186 | * @return mixed |
||
| 187 | */ |
||
| 188 | public static function trace($trace = null) |
||
| 189 | { |
||
| 190 | if ($trace === null) { |
||
| 191 | if (KINT_PHP525) { |
||
| 192 | $trace = debug_backtrace(true); |
||
|
|
|||
| 193 | } else { |
||
| 194 | $trace = debug_backtrace(); |
||
| 195 | } |
||
| 196 | } else { |
||
| 197 | return self::dump($trace); |
||
| 198 | } |
||
| 199 | |||
| 200 | Kint_Parser_Trace::normalizeAliases(self::$aliases); |
||
| 201 | |||
| 202 | $trimmed_trace = array(); |
||
| 203 | |||
| 204 | foreach ($trace as $frame) { |
||
| 205 | if (Kint_Parser_Trace::frameIsListed($frame, self::$aliases)) { |
||
| 206 | $trimmed_trace = array(); |
||
| 207 | } |
||
| 208 | |||
| 209 | $trimmed_trace[] = $frame; |
||
| 210 | } |
||
| 211 | |||
| 212 | return self::dumpArray( |
||
| 213 | array($trimmed_trace), |
||
| 214 | array(Kint_Object::blank('Kint::trace()', 'debug_backtrace()')) |
||
| 215 | ); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Dumps an array as separate values, and uses $names to seed the parser. |
||
| 220 | * |
||
| 221 | * @param array $data Data to be dumped |
||
| 222 | * @param array[Kint_Object]|null $names Array of Kint_Object to seed the parser with |
||
| 223 | */ |
||
| 224 | public static function dumpArray(array $data, array $names = null) |
||
| 225 | { |
||
| 226 | self::$names = $names; |
||
| 227 | self::$dump_array = true; |
||
| 228 | |||
| 229 | $out = self::dump($data); |
||
| 230 | |||
| 231 | self::$names = null; |
||
| 232 | self::$dump_array = false; |
||
| 233 | |||
| 234 | return $out; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Dump information about variables, accepts any number of parameters, supports modifiers:. |
||
| 239 | * |
||
| 240 | * clean up any output before kint and place the dump at the top of page: |
||
| 241 | * - Kint::dump() |
||
| 242 | * ***** |
||
| 243 | * expand all nodes on display: |
||
| 244 | * ! Kint::dump() |
||
| 245 | * ***** |
||
| 246 | * dump variables disregarding their depth: |
||
| 247 | * + Kint::dump() |
||
| 248 | * ***** |
||
| 249 | * return output instead of displaying it: |
||
| 250 | * |
||
| 251 | * @ Kint::dump() |
||
| 252 | * ***** |
||
| 253 | * force output as plain text |
||
| 254 | * ~ Kint::dump() |
||
| 255 | * |
||
| 256 | * Modifiers are supported by all dump wrapper functions, including Kint::trace(). Space is optional. |
||
| 257 | * |
||
| 258 | * @param mixed $data |
||
| 259 | * |
||
| 260 | * @return int|string |
||
| 261 | */ |
||
| 262 | public static function dump($data = null) |
||
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * generic path display callback, can be configured in app_root_dirs; purpose is |
||
| 436 | * to show relevant path info and hide as much of the path as possible. |
||
| 437 | * |
||
| 438 | * @param string $file |
||
| 439 | * |
||
| 440 | * @return string |
||
| 441 | */ |
||
| 442 | public static function shortenPath($file) |
||
| 443 | { |
||
| 444 | $file = array_values(array_filter(explode('/', str_replace('\\', '/', $file)), 'strlen')); |
||
| 445 | |||
| 446 | $longest_match = 0; |
||
| 447 | $match = '/'; |
||
| 448 | |||
| 449 | foreach (self::$app_root_dirs as $path => $alias) { |
||
| 450 | if (empty($path)) { |
||
| 451 | continue; |
||
| 452 | } |
||
| 453 | |||
| 454 | $path = array_values(array_filter(explode('/', str_replace('\\', '/', $path)), 'strlen')); |
||
| 455 | |||
| 456 | if (array_slice($file, 0, count($path)) === $path && count($path) > $longest_match) { |
||
| 457 | $longest_match = count($path); |
||
| 458 | $match = $alias; |
||
| 459 | } |
||
| 460 | } |
||
| 461 | |||
| 462 | if ($longest_match) { |
||
| 463 | $file = array_merge(array($match), array_slice($file, $longest_match)); |
||
| 464 | |||
| 465 | return implode('/', $file); |
||
| 466 | } else { |
||
| 467 | // fallback to find common path with Kint dir |
||
| 468 | $kint = array_values(array_filter(explode('/', str_replace('\\', '/', KINT_DIR)), 'strlen')); |
||
| 469 | |||
| 470 | foreach ($file as $i => $part) { |
||
| 471 | if (!isset($kint[$i]) || $kint[$i] !== $part) { |
||
| 472 | return ($i ? '.../' : '/').implode('/', array_slice($file, $i)); |
||
| 473 | } |
||
| 474 | } |
||
| 475 | |||
| 476 | return '/'.implode('/', $file); |
||
| 477 | } |
||
| 478 | } |
||
| 479 | |||
| 480 | public static function getIdeLink($file, $line) |
||
| 481 | { |
||
| 482 | return str_replace(array('%f', '%l'), array($file, $line), self::$file_link_format); |
||
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * returns parameter names that the function was passed, as well as any predefined symbols before function |
||
| 487 | * call (modifiers). |
||
| 488 | * |
||
| 489 | * @param array $trace |
||
| 490 | * |
||
| 491 | * @return array($params, $modifiers, $callee, $caller, $miniTrace) |
||
| 492 | */ |
||
| 493 | private static function getCalleeInfo($trace, $num_params) |
||
| 494 | { |
||
| 495 | Kint_Parser_Trace::normalizeAliases(self::$aliases); |
||
| 496 | $miniTrace = array(); |
||
| 497 | |||
| 498 | foreach ($trace as $index => $frame) { |
||
| 499 | if (Kint_Parser_Trace::frameIsListed($frame, self::$aliases)) { |
||
| 500 | $miniTrace = array(); |
||
| 501 | } |
||
| 502 | |||
| 503 | if (!Kint_Parser_Trace::frameIsListed($frame, array('spl_autoload_call'))) { |
||
| 504 | $miniTrace[] = $frame; |
||
| 505 | } |
||
| 506 | } |
||
| 507 | |||
| 508 | $callee = reset($miniTrace); |
||
| 509 | $caller = next($miniTrace); |
||
| 510 | if (!$callee) { |
||
| 511 | $callee = null; |
||
| 512 | } |
||
| 513 | if (!$caller) { |
||
| 514 | $caller = null; |
||
| 515 | } |
||
| 516 | |||
| 517 | unset($miniTrace[0]); |
||
| 518 | |||
| 519 | foreach ($miniTrace as $index => &$frame) { |
||
| 520 | if (!isset($frame['file'], $frame['line'])) { |
||
| 521 | unset($miniTrace[$index]); |
||
| 522 | } else { |
||
| 523 | unset($frame['object'], $frame['args']); |
||
| 524 | } |
||
| 525 | } |
||
| 526 | |||
| 527 | $miniTrace = array_values($miniTrace); |
||
| 528 | |||
| 529 | if (!isset($callee['file'], $callee['line']) || !is_readable($callee['file'])) { |
||
| 530 | return array(null, array(), $callee, $caller, $miniTrace); |
||
| 531 | } |
||
| 532 | |||
| 533 | // open the file and read it up to the position where the function call expression ended |
||
| 534 | if (empty($callee['class'])) { |
||
| 535 | $callfunc = $callee['function']; |
||
| 536 | } else { |
||
| 537 | $callfunc = array($callee['class'], $callee['function']); |
||
| 538 | } |
||
| 539 | |||
| 540 | $calls = Kint_SourceParser::getFunctionCalls( |
||
| 541 | file_get_contents($callee['file']), |
||
| 542 | $callee['line'], |
||
| 543 | $callfunc |
||
| 544 | ); |
||
| 545 | |||
| 546 | $return = array(null, array(), $callee, $caller, $miniTrace); |
||
| 547 | |||
| 548 | foreach ($calls as $call) { |
||
| 549 | $is_unpack = false; |
||
| 550 | |||
| 551 | // Handle argument unpacking as a last resort |
||
| 552 | if (KINT_PHP56) { |
||
| 553 | foreach ($call['parameters'] as $i => &$param) { |
||
| 554 | if (strpos($param['name'], '...') === 0) { |
||
| 555 | if ($i === count($call['parameters']) - 1) { |
||
| 556 | for ($j = 1; $j + $i < $num_params; ++$j) { |
||
| 557 | $call['parameters'][] = array( |
||
| 558 | 'name' => 'array_values('.substr($param['name'], 3).')['.$j.']', |
||
| 559 | 'path' => 'array_values('.substr($param['path'], 3).')['.$j.']', |
||
| 560 | 'expression' => false, |
||
| 561 | ); |
||
| 562 | } |
||
| 563 | |||
| 564 | $param['name'] = 'reset('.substr($param['name'], 3).')'; |
||
| 565 | $param['path'] = 'reset('.substr($param['path'], 3).')'; |
||
| 566 | $param['expression'] = false; |
||
| 567 | } else { |
||
| 568 | $call['parameters'] = array_slice($call['parameters'], 0, $i); |
||
| 569 | } |
||
| 570 | |||
| 571 | $is_unpack = true; |
||
| 572 | break; |
||
| 573 | } |
||
| 574 | } |
||
| 575 | } |
||
| 576 | |||
| 577 | if ($is_unpack || count($call['parameters']) === $num_params) { |
||
| 578 | if ($return[0] === null) { |
||
| 579 | $return = array($call['parameters'], $call['modifiers'], $callee, $caller, $miniTrace); |
||
| 580 | } else { |
||
| 581 | // If we have multiple calls on the same line with the same amount of arguments, |
||
| 582 | // we can't be sure which it is so just return null and let them figure it out |
||
| 583 | return array(null, array(), $callee, $caller, $miniTrace); |
||
| 584 | } |
||
| 585 | } |
||
| 586 | } |
||
| 587 | |||
| 588 | return $return; |
||
| 589 | } |
||
| 590 | |||
| 591 | public static function composerGetExtras($key = 'kint') |
||
| 630 | } |
||
| 631 | |||
| 632 | public static function composerGetDisableHelperFunctions() |
||
| 637 | } |
||
| 638 | } |
||
| 639 |