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. 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 Kint, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Kint |
||
| 14 | { |
||
| 15 | // these are all public and 1:1 config array keys so you can switch them easily |
||
| 16 | |||
| 17 | const MODE_CLI = 'c'; # stores mode and active statuses |
||
| 18 | |||
| 19 | const MODE_PLAIN = 'p'; |
||
| 20 | |||
| 21 | const MODE_JS = 'j'; |
||
| 22 | |||
| 23 | const MODE_RICH = 'r'; |
||
| 24 | |||
| 25 | const MODE_WHITESPACE = 'w'; |
||
| 26 | |||
| 27 | public static $delayedMode; |
||
| 28 | |||
| 29 | public static $returnOutput; |
||
| 30 | |||
| 31 | public static $fileLinkFormat; |
||
| 32 | |||
| 33 | public static $displayCalledFrom; |
||
| 34 | |||
| 35 | public static $maxStrLength; |
||
| 36 | |||
| 37 | public static $appRootDirs; |
||
| 38 | |||
| 39 | public static $maxLevels; |
||
| 40 | |||
| 41 | public static $theme; |
||
| 42 | |||
| 43 | public static $expandedByDefault; |
||
| 44 | |||
| 45 | public static $cliDetection; |
||
| 46 | |||
| 47 | public static $cliColors; |
||
| 48 | |||
| 49 | public static $aliases = array( |
||
| 50 | 'methods' => array( |
||
| 51 | array('Kint', 'dump'), |
||
| 52 | array('Kint', 'trace'), |
||
| 53 | ), |
||
| 54 | 'functions' => array( |
||
| 55 | 'd', |
||
| 56 | 'dd', |
||
| 57 | 'ddd', |
||
| 58 | 'de', |
||
| 59 | 's', |
||
| 60 | 'sd', |
||
| 61 | 'se', |
||
| 62 | 'j', |
||
| 63 | 'jd', |
||
| 64 | 'je', |
||
| 65 | ), |
||
| 66 | ); |
||
| 67 | |||
| 68 | private static $_enabledMode; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * returns parameter names that the function was passed, as well as any predefined symbols before function |
||
| 72 | * call (modifiers) |
||
| 73 | * |
||
| 74 | * @param array $trace |
||
| 75 | * |
||
| 76 | * @return array( $parameters, $modifier, $callee, $previousCaller ) |
||
|
|
|||
| 77 | */ |
||
| 78 | 2 | private static function _getCalleeInfo($trace) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @param array $data |
||
| 278 | * |
||
| 279 | * @return array|false |
||
| 280 | */ |
||
| 281 | 2 | private static function _parseTrace(array $data) |
|
| 282 | { |
||
| 283 | 2 | $trace = array(); |
|
| 284 | 2 | $traceFields = array('file', 'line', 'args', 'class'); |
|
| 285 | 2 | $fileFound = false; # file element must exist in one of the steps |
|
| 286 | |||
| 287 | # validate whether a trace was indeed passed |
||
| 288 | /** @noinspection PhpAssignmentInConditionInspection */ |
||
| 289 | 2 | while ($step = array_pop($data)) { |
|
| 290 | 2 | if (!is_array($step) || !isset($step['function'])) { |
|
| 291 | 2 | return false; |
|
| 292 | } |
||
| 293 | if (!$fileFound && isset($step['file']) && file_exists($step['file'])) { |
||
| 294 | $fileFound = true; |
||
| 295 | } |
||
| 296 | |||
| 297 | $valid = false; |
||
| 298 | foreach ($traceFields as $element) { |
||
| 299 | if (isset($step[$element])) { |
||
| 300 | $valid = true; |
||
| 301 | break; |
||
| 302 | } |
||
| 303 | } |
||
| 304 | if (!$valid) { |
||
| 305 | return false; |
||
| 306 | } |
||
| 307 | |||
| 308 | if (self::_stepIsInternal($step)) { |
||
| 309 | $step = array( |
||
| 310 | 'file' => $step['file'], |
||
| 311 | 'line' => $step['line'], |
||
| 312 | 'function' => '', |
||
| 313 | ); |
||
| 314 | array_unshift($trace, $step); |
||
| 315 | break; |
||
| 316 | } |
||
| 317 | if ($step['function'] !== 'spl_autoload_call') { # meaningless |
||
| 318 | array_unshift($trace, $step); |
||
| 319 | } |
||
| 320 | } |
||
| 321 | if (!$fileFound) { |
||
| 322 | return false; |
||
| 323 | } |
||
| 324 | |||
| 325 | $output = array(); |
||
| 326 | foreach ($trace as $step) { |
||
| 327 | if (isset($step['file'])) { |
||
| 328 | $file = $step['file']; |
||
| 329 | |||
| 330 | if (isset($step['line'])) { |
||
| 331 | $line = $step['line']; |
||
| 332 | # include the source of this step |
||
| 333 | if (self::enabled() === self::MODE_RICH) { |
||
| 334 | $source = self::_showSource($file, $line); |
||
| 335 | } |
||
| 336 | } |
||
| 337 | } |
||
| 338 | |||
| 339 | $function = $step['function']; |
||
| 340 | |||
| 341 | if (in_array($function, array('include', 'include_once', 'require', 'require_once'), true)) { |
||
| 342 | if (empty($step['args'])) { |
||
| 343 | # no arguments |
||
| 344 | $args = array(); |
||
| 345 | } else { |
||
| 346 | # sanitize the included file path |
||
| 347 | $args = array('file' => self::shortenPath($step['args'][0])); |
||
| 348 | } |
||
| 349 | } elseif (isset($step['args'])) { |
||
| 350 | if (empty($step['class']) && !function_exists($function)) { |
||
| 351 | # introspection on closures or language constructs in a stack trace is impossible before PHP 5.3 |
||
| 352 | $params = null; |
||
| 353 | } else { |
||
| 354 | try { |
||
| 355 | if (isset($step['class'])) { |
||
| 356 | if (method_exists($step['class'], $function)) { |
||
| 357 | $reflection = new \ReflectionMethod($step['class'], $function); |
||
| 358 | } elseif (isset($step['type']) && $step['type'] == '::') { |
||
| 359 | $reflection = new \ReflectionMethod($step['class'], '__callStatic'); |
||
| 360 | } else { |
||
| 361 | $reflection = new \ReflectionMethod($step['class'], '__call'); |
||
| 362 | } |
||
| 363 | } else { |
||
| 364 | $reflection = new \ReflectionFunction($function); |
||
| 365 | } |
||
| 366 | |||
| 367 | # get the function parameters |
||
| 368 | $params = $reflection->getParameters(); |
||
| 369 | } catch (\Exception $e) { # avoid various PHP version incompatibilities |
||
| 370 | $params = null; |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | $args = array(); |
||
| 375 | foreach ($step['args'] as $i => $arg) { |
||
| 376 | if (isset($params[$i])) { |
||
| 377 | # assign the argument by the parameter name |
||
| 378 | $args[$params[$i]->name] = $arg; |
||
| 379 | } else { |
||
| 380 | # assign the argument by number |
||
| 381 | $args['#' . ($i + 1)] = $arg; |
||
| 382 | } |
||
| 383 | } |
||
| 384 | } |
||
| 385 | |||
| 386 | if (isset($step['class'])) { |
||
| 387 | # Class->method() or Class::method() |
||
| 388 | $function = $step['class'] . $step['type'] . $function; |
||
| 389 | } |
||
| 390 | |||
| 391 | // TODO: it's possible to parse the object name out from the source! |
||
| 392 | $output[] = array( |
||
| 393 | 'function' => $function, |
||
| 394 | 'args' => isset($args) ? $args : null, |
||
| 395 | 'file' => isset($file) ? $file : null, |
||
| 396 | 'line' => isset($line) ? $line : null, |
||
| 397 | 'source' => isset($source) ? $source : null, |
||
| 398 | 'object' => isset($step['object']) ? $step['object'] : null, |
||
| 399 | ); |
||
| 400 | |||
| 401 | unset($function, $args, $file, $line, $source); |
||
| 402 | } |
||
| 403 | |||
| 404 | return $output; |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * removes comments and zaps whitespace & <?php tags from php code, makes for easier further parsing |
||
| 409 | * |
||
| 410 | * @param string $source |
||
| 411 | * |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | private static function _removeAllButCode($source) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * trace helper, shows the place in code inline |
||
| 453 | * |
||
| 454 | * @param string $file full path to file |
||
| 455 | * @param int $lineNumber the line to display |
||
| 456 | * @param int $padding surrounding lines to show besides the main one |
||
| 457 | * |
||
| 458 | * @return bool|string |
||
| 459 | */ |
||
| 460 | private static function _showSource($file, $lineNumber, $padding = 7) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * returns whether current trace step belongs to Kint or its wrappers |
||
| 518 | * |
||
| 519 | * @param $step |
||
| 520 | * |
||
| 521 | * @return array |
||
| 522 | */ |
||
| 523 | 2 | private static function _stepIsInternal($step) |
|
| 524 | { |
||
| 525 | 2 | if (isset($step['class'])) { |
|
| 526 | 2 | foreach (self::$aliases['methods'] as $alias) { |
|
| 527 | if ( |
||
| 528 | 2 | $alias[0] === $step['class'] |
|
| 529 | && |
||
| 530 | 2 | $alias[1] === $step['function'] |
|
| 531 | ) { |
||
| 532 | 2 | return true; |
|
| 533 | } |
||
| 534 | } |
||
| 535 | |||
| 536 | 2 | return false; |
|
| 537 | } else { |
||
| 538 | return in_array($step['function'], self::$aliases['functions'], true); |
||
| 539 | } |
||
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Dump information about variables, accepts any number of parameters, supports modifiers: |
||
| 544 | * |
||
| 545 | * clean up any output before kint and place the dump at the top of page: |
||
| 546 | * - Kint::dump() |
||
| 547 | * ***** |
||
| 548 | * expand all nodes on display: |
||
| 549 | * ! Kint::dump() |
||
| 550 | * ***** |
||
| 551 | * dump variables disregarding their depth: |
||
| 552 | * + Kint::dump() |
||
| 553 | * ***** |
||
| 554 | * return output instead of displaying it: |
||
| 555 | * @ Kint::dump() |
||
| 556 | * ***** |
||
| 557 | * force output as plain text |
||
| 558 | * ~ Kint::dump() |
||
| 559 | * |
||
| 560 | * Modifiers are supported by all dump wrapper functions, including Kint::trace(). Space is optional. |
||
| 561 | * |
||
| 562 | * |
||
| 563 | * You can also use the following shorthand to display debug_backtrace(): |
||
| 564 | * Kint::dump( 1 ); |
||
| 565 | * |
||
| 566 | * Passing the result from debug_backtrace() to kint::dump() as a single parameter will display it as trace too: |
||
| 567 | * $trace = debug_backtrace( true ); |
||
| 568 | * Kint::dump( $trace ); |
||
| 569 | * Or simply: |
||
| 570 | * Kint::dump( debug_backtrace() ); |
||
| 571 | * |
||
| 572 | * |
||
| 573 | * @param mixed $data |
||
| 574 | * |
||
| 575 | * @return void|string |
||
| 576 | */ |
||
| 577 | 2 | public static function dump($data = null) |
|
| 578 | { |
||
| 579 | 2 | if (!self::enabled()) { |
|
| 580 | return ''; |
||
| 581 | } |
||
| 582 | |||
| 583 | 2 | $stash = self::settings(); |
|
| 584 | |||
| 585 | 2 | list($names, $modifiers, $callee, $previousCaller, $miniTrace) = self::_getCalleeInfo( |
|
| 586 | 2 | defined('DEBUG_BACKTRACE_IGNORE_ARGS') |
|
| 587 | 2 | ? debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) |
|
| 588 | 2 | : debug_backtrace() |
|
| 589 | ); |
||
| 590 | |||
| 591 | # set mode for current run |
||
| 592 | 2 | $mode = self::enabled(); |
|
| 593 | 2 | if ($mode === true) { |
|
| 594 | $mode = (PHP_SAPI === 'cli' && self::$cliDetection === true) ? self::MODE_CLI : self::MODE_RICH; |
||
| 595 | } |
||
| 596 | 2 | self::enabled($mode); |
|
| 597 | |||
| 598 | 2 | if (strpos($modifiers, '~') !== false) { |
|
| 599 | self::enabled(self::MODE_WHITESPACE); |
||
| 600 | } |
||
| 601 | |||
| 602 | 2 | switch (self::enabled()) { |
|
| 603 | 2 | case self::MODE_RICH: |
|
| 604 | 1 | $decorator = 'kint\decorators\Kint_Decorators_Rich'; |
|
| 605 | 1 | break; |
|
| 606 | 1 | case self::MODE_JS: |
|
| 607 | $decorator = 'kint\decorators\Kint_Decorators_JS'; |
||
| 608 | break; |
||
| 609 | default: |
||
| 610 | 1 | case self::MODE_PLAIN: |
|
| 611 | 1 | $decorator = 'kint\decorators\Kint_Decorators_Plain'; |
|
| 612 | 1 | break; |
|
| 613 | } |
||
| 614 | |||
| 615 | /* @var Kint_Decorators $decorator */ |
||
| 616 | |||
| 617 | 2 | $firstRunOldValue = $decorator::$firstRun; |
|
| 618 | |||
| 619 | # process modifiers: @, +, ! and - |
||
| 620 | 2 | if (strpos($modifiers, '-') !== false) { |
|
| 621 | $decorator::$firstRun = true; |
||
| 622 | while (ob_get_level()) { |
||
| 623 | ob_end_clean(); |
||
| 624 | } |
||
| 625 | } |
||
| 626 | |||
| 627 | 2 | if (strpos($modifiers, '!') !== false) { |
|
| 628 | self::$expandedByDefault = true; |
||
| 629 | } |
||
| 630 | |||
| 631 | 2 | if (strpos($modifiers, '+') !== false) { |
|
| 632 | self::$maxLevels = false; |
||
| 633 | } |
||
| 634 | |||
| 635 | 2 | if (strpos($modifiers, '@') !== false) { |
|
| 636 | self::$returnOutput = true; |
||
| 637 | $decorator::$firstRun = true; |
||
| 638 | } |
||
| 639 | |||
| 640 | 2 | $output = ''; |
|
| 641 | 2 | if ($decorator::$firstRun) { |
|
| 642 | 1 | $output .= call_user_func(array($decorator, 'init')); |
|
| 643 | } |
||
| 644 | |||
| 645 | 2 | $trace = false; |
|
| 646 | 2 | $tmpFuncNumArgs = func_num_args(); |
|
| 647 | if ( |
||
| 648 | 2 | $data === 1 |
|
| 649 | && |
||
| 650 | 2 | $tmpFuncNumArgs === 1 |
|
| 651 | && |
||
| 652 | 2 | $names === array(null) |
|
| 653 | ) { |
||
| 654 | |||
| 655 | # Kint::dump(1) shorthand |
||
| 656 | $trace = debug_backtrace(true); |
||
| 657 | |||
| 658 | } elseif ( |
||
| 659 | 2 | $tmpFuncNumArgs === 1 |
|
| 660 | && |
||
| 661 | 2 | is_array($data) |
|
| 662 | ) { |
||
| 663 | |||
| 664 | 2 | $trace = $data; # test if the single parameter is result of debug_backtrace() |
|
| 665 | |||
| 666 | } |
||
| 667 | 2 | $trace and $trace = self::_parseTrace($trace); |
|
| 668 | |||
| 669 | 2 | $output .= call_user_func(array($decorator, 'wrapStart')); |
|
| 670 | 2 | if ($trace) { |
|
| 671 | $output .= call_user_func(array($decorator, 'decorateTrace'), $trace); |
||
| 672 | } else { |
||
| 673 | 2 | $data = $tmpFuncNumArgs === 0 ? array('[[no arguments passed]]') : func_get_args(); |
|
| 674 | |||
| 675 | 2 | foreach ($data as $k => $argument) { |
|
| 676 | 2 | KintParser::reset(); |
|
| 677 | # when the dump arguments take long to generate output, user might have changed the file and |
||
| 678 | # Kint might not parse the arguments correctly, so check if names are set and while the |
||
| 679 | # displayed names might be wrong, at least don't throw an error |
||
| 680 | 2 | $output .= call_user_func( |
|
| 681 | 2 | array($decorator, 'decorate'), |
|
| 682 | 2 | KintParser::factory($argument, isset($names[$k]) ? $names[$k] : '') |
|
| 683 | ); |
||
| 684 | } |
||
| 685 | } |
||
| 686 | |||
| 687 | 2 | $output .= call_user_func(array($decorator, 'wrapEnd'), $callee, $miniTrace, $previousCaller); |
|
| 688 | |||
| 689 | 2 | $decorator::$firstRun = false; |
|
| 690 | |||
| 691 | 2 | if (strpos($modifiers, '@') !== false) { |
|
| 692 | $decorator::$firstRun = $firstRunOldValue; |
||
| 693 | } |
||
| 694 | |||
| 695 | 2 | if (self::$returnOutput) { |
|
| 696 | self::settings($stash); |
||
| 697 | |||
| 698 | return $output; |
||
| 699 | } |
||
| 700 | |||
| 701 | 2 | if (self::$delayedMode) { |
|
| 702 | self::settings($stash); |
||
| 703 | register_shutdown_function('printf', '%s', $output); |
||
| 704 | |||
| 705 | return ''; |
||
| 706 | } |
||
| 707 | |||
| 708 | 2 | self::settings($stash); |
|
| 709 | 2 | echo $output; |
|
| 710 | |||
| 711 | 2 | return ''; |
|
| 712 | } |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Enables or disables Kint, can globally enforce the rendering mode. If called without parameters, returns the |
||
| 716 | * current mode. |
||
| 717 | * |
||
| 718 | * @param mixed $forceMode |
||
| 719 | * null or void - return current mode |
||
| 720 | * false - disable (no output) |
||
| 721 | * true - enable and detect cli automatically |
||
| 722 | * Kint::MODE_* - enable and force selected mode disregarding detection and function |
||
| 723 | * shorthand (s()/d()), note that you can still override this |
||
| 724 | * with the "~" modifier |
||
| 725 | * |
||
| 726 | * @return mixed previously set value if a new one is passed |
||
| 727 | */ |
||
| 728 | 2 | public static function enabled($forceMode = null) |
|
| 741 | |||
| 742 | /** |
||
| 743 | * Stashes or sets all settings at once |
||
| 744 | * |
||
| 745 | * @param array|null $settings Array of all settings to be set or null to set none |
||
| 746 | * |
||
| 747 | * @return array Current settings |
||
| 748 | */ |
||
| 749 | 2 | public static function settings(array $settings = null) |
|
| 750 | { |
||
| 751 | 2 | static $keys = array( |
|
| 752 | 'delayedMode', |
||
| 753 | '_enabledMode', |
||
| 754 | 'aliases', |
||
| 755 | 'appRootDirs', |
||
| 756 | 'cliColors', |
||
| 757 | 'displayCalledFrom', |
||
| 758 | 'expandedByDefault', |
||
| 759 | 'fileLinkFormat', |
||
| 760 | 'maxLevels', |
||
| 761 | 'maxStrLength', |
||
| 762 | 'returnOutput', |
||
| 763 | 'theme', |
||
| 764 | ); |
||
| 765 | |||
| 766 | 2 | $out = array(); |
|
| 767 | 2 | foreach ($keys as $key) { |
|
| 768 | /** @noinspection PhpVariableVariableInspection */ |
||
| 769 | 2 | $out[$key] = self::$$key; |
|
| 770 | } |
||
| 771 | |||
| 772 | 2 | if ($settings !== null) { |
|
| 773 | 2 | $in = array_intersect_key($settings, array_flip($keys)); |
|
| 774 | 2 | foreach ($in as $key => $val) { |
|
| 775 | /** @noinspection PhpVariableVariableInspection */ |
||
| 776 | 2 | self::$$key = $val; |
|
| 777 | } |
||
| 778 | } |
||
| 779 | |||
| 780 | 2 | return $out; |
|
| 781 | } |
||
| 782 | |||
| 783 | /** |
||
| 784 | * @param string $file |
||
| 785 | * @param int $line |
||
| 786 | * |
||
| 787 | * @return mixed |
||
| 788 | */ |
||
| 789 | public static function getIdeLink($file, $line) |
||
| 793 | |||
| 794 | /** |
||
| 795 | * generic path display callback, can be configured in the settings; purpose is to show relevant path info and hide |
||
| 796 | * as much of the path as possible. |
||
| 797 | * |
||
| 798 | * @param string $file |
||
| 799 | * |
||
| 800 | * @return string |
||
| 801 | */ |
||
| 802 | public static function shortenPath($file) |
||
| 803 | { |
||
| 804 | $file = str_replace('\\', '/', $file); |
||
| 805 | $shortenedName = $file; |
||
| 806 | $replaced = false; |
||
| 807 | if (is_array(self::$appRootDirs)) { |
||
| 808 | foreach (self::$appRootDirs as $path => $replaceString) { |
||
| 809 | if (empty($path)) { |
||
| 810 | continue; |
||
| 811 | } |
||
| 812 | |||
| 813 | $path = str_replace('\\', '/', $path); |
||
| 814 | |||
| 815 | if (strpos($file, $path) === 0) { |
||
| 816 | $shortenedName = $replaceString . substr($file, strlen($path)); |
||
| 817 | $replaced = true; |
||
| 818 | break; |
||
| 819 | } |
||
| 820 | } |
||
| 821 | } |
||
| 822 | |||
| 823 | # fallback to find common path with Kint dir |
||
| 824 | if (!$replaced) { |
||
| 825 | $pathParts = explode('/', str_replace('\\', '/', KINT_DIR)); |
||
| 826 | $fileParts = explode('/', $file); |
||
| 827 | $i = 0; |
||
| 828 | foreach ($fileParts as $i => $filePart) { |
||
| 829 | if (!isset($pathParts[$i]) || $pathParts[$i] !== $filePart) { |
||
| 830 | break; |
||
| 831 | } |
||
| 832 | } |
||
| 833 | |||
| 834 | $shortenedName = ($i ? '.../' : '') . implode('/', array_slice($fileParts, $i)); |
||
| 835 | } |
||
| 836 | |||
| 837 | return $shortenedName; |
||
| 838 | } |
||
| 839 | |||
| 840 | /** |
||
| 841 | * Prints a debug backtrace, same as Kint::dump(1) |
||
| 842 | * |
||
| 843 | * @param array $trace [OPTIONAL] you can pass your own trace, otherwise, `debug_backtrace` will be called |
||
| 844 | * |
||
| 845 | * @return mixed |
||
| 846 | */ |
||
| 847 | public static function trace($trace = null) |
||
| 855 | } |
||
| 856 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.