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 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 |
||
| 12 | class Kint |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var mixed Kint mode |
||
| 16 | * |
||
| 17 | * false: Disabled |
||
| 18 | * true: Enabled, default mode selection |
||
| 19 | * other: Manual mode selection |
||
| 20 | */ |
||
| 21 | public static $enabled_mode = true; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Default mode. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | public static $mode_default = self::MODE_RICH; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Default mode in CLI with cli_detection on. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | public static $mode_default_cli = self::MODE_CLI; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var bool Return output instead of echoing |
||
| 39 | */ |
||
| 40 | public static $return; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string format of the link to the source file in trace entries. |
||
| 44 | * |
||
| 45 | * Use %f for file path, %l for line number. |
||
| 46 | * |
||
| 47 | * [!] EXAMPLE (works with for phpStorm and RemoteCall Plugin): |
||
| 48 | * |
||
| 49 | * Kint::$file_link_format = 'http://localhost:8091/?message=%f:%l'; |
||
| 50 | */ |
||
| 51 | public static $file_link_format = ''; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var bool whether to display where kint was called from |
||
| 55 | */ |
||
| 56 | public static $display_called_from = true; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array base directories of your application that will be displayed instead of the full path. |
||
| 60 | * |
||
| 61 | * Keys are paths, values are replacement strings |
||
| 62 | * |
||
| 63 | * [!] EXAMPLE (for Laravel 5): |
||
| 64 | * |
||
| 65 | * Kint::$app_root_dirs = [ |
||
| 66 | * base_path() => '<BASE>', |
||
| 67 | * app_path() => '<APP>', |
||
| 68 | * config_path() => '<CONFIG>', |
||
| 69 | * database_path() => '<DATABASE>', |
||
| 70 | * public_path() => '<PUBLIC>', |
||
| 71 | * resource_path() => '<RESOURCE>', |
||
| 72 | * storage_path() => '<STORAGE>', |
||
| 73 | * ]; |
||
| 74 | * |
||
| 75 | * Defaults to [$_SERVER['DOCUMENT_ROOT'] => '<ROOT>'] |
||
| 76 | */ |
||
| 77 | public static $app_root_dirs = array(); |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var int max array/object levels to go deep, if zero no limits are applied |
||
| 81 | */ |
||
| 82 | public static $max_depth = 6; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var bool expand all trees by default for rich view |
||
| 86 | */ |
||
| 87 | public static $expanded = false; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var bool enable detection when Kint is command line. |
||
| 91 | * |
||
| 92 | * Formats output with whitespace only; does not HTML-escape it |
||
| 93 | */ |
||
| 94 | public static $cli_detection = true; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var array Kint aliases. Add debug functions in Kint wrappers here to fix modifiers and backtraces |
||
| 98 | */ |
||
| 99 | public static $aliases = array( |
||
| 100 | array('Kint\\Kint', 'dump'), |
||
| 101 | array('Kint\\Kint', 'trace'), |
||
| 102 | array('Kint\\Kint', 'dumpArray'), |
||
| 103 | ); |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var array Kint\Renderer\Renderer descendants. Add to array to extend. |
||
| 107 | */ |
||
| 108 | public static $renderers = array( |
||
| 109 | self::MODE_RICH => 'Kint\\Renderer\\RichRenderer', |
||
| 110 | self::MODE_PLAIN => 'Kint\\Renderer\\PlainRenderer', |
||
| 111 | self::MODE_TEXT => 'Kint\\Renderer\\TextRenderer', |
||
| 112 | self::MODE_CLI => 'Kint\\Renderer\\CliRenderer', |
||
| 113 | ); |
||
| 114 | |||
| 115 | const MODE_RICH = 'r'; |
||
| 116 | const MODE_TEXT = 't'; |
||
| 117 | const MODE_CLI = 'c'; |
||
| 118 | const MODE_PLAIN = 'p'; |
||
| 119 | |||
| 120 | public static $plugins = array( |
||
| 121 | 'Kint\\Parser\\ArrayObjectPlugin', |
||
| 122 | 'Kint\\Parser\\Base64Plugin', |
||
| 123 | 'Kint\\Parser\\BlacklistPlugin', |
||
| 124 | 'Kint\\Parser\\ClassMethodsPlugin', |
||
| 125 | 'Kint\\Parser\\ClassStaticsPlugin', |
||
| 126 | 'Kint\\Parser\\ClosurePlugin', |
||
| 127 | 'Kint\\Parser\\ColorPlugin', |
||
| 128 | 'Kint\\Parser\\DateTimePlugin', |
||
| 129 | 'Kint\\Parser\\FsPathPlugin', |
||
| 130 | 'Kint\\Parser\\IteratorPlugin', |
||
| 131 | 'Kint\\Parser\\JsonPlugin', |
||
| 132 | 'Kint\\Parser\\MicrotimePlugin', |
||
| 133 | 'Kint\\Parser\\SimpleXMLElementPlugin', |
||
| 134 | 'Kint\\Parser\\SplFileInfoPlugin', |
||
| 135 | 'Kint\\Parser\\SplObjectStoragePlugin', |
||
| 136 | 'Kint\\Parser\\StreamPlugin', |
||
| 137 | 'Kint\\Parser\\TablePlugin', |
||
| 138 | 'Kint\\Parser\\ThrowablePlugin', |
||
| 139 | 'Kint\\Parser\\TimestampPlugin', |
||
| 140 | 'Kint\\Parser\\ToStringPlugin', |
||
| 141 | 'Kint\\Parser\\TracePlugin', |
||
| 142 | 'Kint\\Parser\\XmlPlugin', |
||
| 143 | ); |
||
| 144 | |||
| 145 | protected static $plugin_pool = array(); |
||
| 146 | |||
| 147 | protected $parser; |
||
| 148 | protected $renderer; |
||
| 149 | |||
| 150 | public function __construct(Parser $p, Renderer $r) |
||
| 155 | |||
| 156 | public function setParser(Parser $p) |
||
| 160 | |||
| 161 | public function getParser() |
||
| 165 | |||
| 166 | public function setRenderer(Renderer $r) |
||
| 170 | |||
| 171 | public function getRenderer() |
||
| 175 | |||
| 176 | public function setStatesFromStatics(array $statics) |
||
| 209 | |||
| 210 | public function setStatesFromCallInfo(array $info) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Renders a list of vars including the pre and post renders. |
||
| 233 | * |
||
| 234 | * @param array $vars Data to dump |
||
| 235 | * @param BasicObject[] $base Base objects |
||
| 236 | * |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | public function dumpAll(array $vars, array $base) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Dumps and renders a var. |
||
| 265 | * |
||
| 266 | * @param mixed $var Data to dump |
||
| 267 | * @param BasicObject $base Base object |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | public function dumpVar(&$var, BasicObject $base) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Gets all static settings at once. |
||
| 280 | * |
||
| 281 | * @return array Current static settings |
||
| 282 | */ |
||
| 283 | public static function getStatics() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Creates a Kint instances based on static settings. |
||
| 304 | * |
||
| 305 | * Also calls setStatesFromStatics for you |
||
| 306 | * |
||
| 307 | * @param array $statics array of statics as returned by getStatics |
||
| 308 | * |
||
| 309 | * @return Kint|null |
||
| 310 | */ |
||
| 311 | public static function createFromStatics(array $statics) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Creates base objects given parameter info. |
||
| 344 | * |
||
| 345 | * @param array $params Parameters as returned from getCallInfo |
||
| 346 | * @param int $argc Number of arguments the helper was called with |
||
| 347 | * |
||
| 348 | * @return BasicObject[] Base objects for the arguments |
||
| 349 | */ |
||
| 350 | public static function getBasesFromParamInfo(array $params, $argc) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Gets call info from the backtrace, alias, and argument count. |
||
| 404 | * |
||
| 405 | * Aliases must be normalized beforehand (Utils::normalizeAliases) |
||
| 406 | * |
||
| 407 | * @param array $aliases Call aliases as found in Kint::$aliases |
||
| 408 | * @param array $trace Backtrace |
||
| 409 | * @param int $argc Number of arguments |
||
| 410 | * |
||
| 411 | * @return array[5] Call info |
||
| 412 | */ |
||
| 413 | public static function getCallInfo(array $aliases, array $trace, $argc) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Returns specific function call info from a stack trace frame, or null if no match could be found. |
||
| 467 | * |
||
| 468 | * @param array $frame The stack trace frame in question |
||
| 469 | * @param int $argc The amount of arguments received |
||
| 470 | * |
||
| 471 | * @return array|null params and modifiers, or null if a specific call could not be determined |
||
| 472 | */ |
||
| 473 | protected static function getSingleCall(array $frame, $argc) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Dumps a backtrace. |
||
| 534 | * |
||
| 535 | * Functionally equivalent to Kint::dump(1) or Kint::dump(debug_backtrace(true)) |
||
| 536 | * |
||
| 537 | * @return string|int |
||
| 538 | */ |
||
| 539 | public static function trace() |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Dumps some data. |
||
| 592 | * |
||
| 593 | * Functionally equivalent to Kint::dump(1) or Kint::dump(debug_backtrace(true)) |
||
| 594 | * |
||
| 595 | * @return string|int |
||
| 596 | */ |
||
| 597 | public static function dump() |
||
| 659 | |||
| 660 | /** |
||
| 661 | * generic path display callback, can be configured in app_root_dirs; purpose is |
||
| 662 | * to show relevant path info and hide as much of the path as possible. |
||
| 663 | * |
||
| 664 | * @param string $file |
||
| 665 | * |
||
| 666 | * @return string |
||
| 667 | */ |
||
| 668 | public static function shortenPath($file) |
||
| 705 | |||
| 706 | public static function getIdeLink($file, $line) |
||
| 710 | } |
||
| 711 |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.