Total Complexity | 110 |
Total Lines | 720 |
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 |
||
35 | class Kint |
||
36 | { |
||
37 | const MODE_RICH = 'r'; |
||
38 | const MODE_TEXT = 't'; |
||
39 | const MODE_CLI = 'c'; |
||
40 | const MODE_PLAIN = 'p'; |
||
41 | |||
42 | /** |
||
43 | * @var mixed Kint mode |
||
44 | * |
||
45 | * false: Disabled |
||
46 | * true: Enabled, default mode selection |
||
47 | * other: Manual mode selection |
||
48 | */ |
||
49 | public static $enabled_mode = true; |
||
50 | |||
51 | /** |
||
52 | * Default mode. |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | public static $mode_default = self::MODE_RICH; |
||
57 | |||
58 | /** |
||
59 | * Default mode in CLI with cli_detection on. |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | public static $mode_default_cli = self::MODE_CLI; |
||
64 | |||
65 | /** |
||
66 | * @var bool Return output instead of echoing |
||
67 | */ |
||
68 | public static $return; |
||
69 | |||
70 | /** |
||
71 | * @var string format of the link to the source file in trace entries. |
||
72 | * |
||
73 | * Use %f for file path, %l for line number. |
||
74 | * |
||
75 | * [!] EXAMPLE (works with for phpStorm and RemoteCall Plugin): |
||
76 | * |
||
77 | * Kint::$file_link_format = 'http://localhost:8091/?message=%f:%l'; |
||
78 | */ |
||
79 | public static $file_link_format = ''; |
||
80 | |||
81 | /** |
||
82 | * @var bool whether to display where kint was called from |
||
83 | */ |
||
84 | public static $display_called_from = true; |
||
85 | |||
86 | /** |
||
87 | * @var array base directories of your application that will be displayed instead of the full path. |
||
88 | * |
||
89 | * Keys are paths, values are replacement strings |
||
90 | * |
||
91 | * [!] EXAMPLE (for Laravel 5): |
||
92 | * |
||
93 | * Kint::$app_root_dirs = [ |
||
94 | * base_path() => '<BASE>', |
||
95 | * app_path() => '<APP>', |
||
96 | * config_path() => '<CONFIG>', |
||
97 | * database_path() => '<DATABASE>', |
||
98 | * public_path() => '<PUBLIC>', |
||
99 | * resource_path() => '<RESOURCE>', |
||
100 | * storage_path() => '<STORAGE>', |
||
101 | * ]; |
||
102 | * |
||
103 | * Defaults to [$_SERVER['DOCUMENT_ROOT'] => '<ROOT>'] |
||
104 | */ |
||
105 | public static $app_root_dirs = array(); |
||
106 | |||
107 | /** |
||
108 | * @var int max array/object levels to go deep, if zero no limits are applied |
||
109 | */ |
||
110 | public static $max_depth = 6; |
||
111 | |||
112 | /** |
||
113 | * @var bool expand all trees by default for rich view |
||
114 | */ |
||
115 | public static $expanded = false; |
||
116 | |||
117 | /** |
||
118 | * @var bool enable detection when Kint is command line. |
||
119 | * |
||
120 | * Formats output with whitespace only; does not HTML-escape it |
||
121 | */ |
||
122 | public static $cli_detection = true; |
||
123 | |||
124 | /** |
||
125 | * @var array Kint aliases. Add debug functions in Kint wrappers here to fix modifiers and backtraces |
||
126 | */ |
||
127 | public static $aliases = array( |
||
128 | array('Kint\\Kint', 'dump'), |
||
129 | array('Kint\\Kint', 'trace'), |
||
130 | array('Kint\\Kint', 'dumpArray'), |
||
131 | ); |
||
132 | |||
133 | /** |
||
134 | * @var array<mixed, string> Array of modes to renderer class names |
||
135 | */ |
||
136 | public static $renderers = array( |
||
137 | self::MODE_RICH => 'Kint\\Renderer\\RichRenderer', |
||
138 | self::MODE_PLAIN => 'Kint\\Renderer\\PlainRenderer', |
||
139 | self::MODE_TEXT => 'Kint\\Renderer\\TextRenderer', |
||
140 | self::MODE_CLI => 'Kint\\Renderer\\CliRenderer', |
||
141 | ); |
||
142 | |||
143 | public static $plugins = array( |
||
144 | 'Kint\\Parser\\ArrayObjectPlugin', |
||
145 | 'Kint\\Parser\\Base64Plugin', |
||
146 | 'Kint\\Parser\\BlacklistPlugin', |
||
147 | 'Kint\\Parser\\ClassMethodsPlugin', |
||
148 | 'Kint\\Parser\\ClassStaticsPlugin', |
||
149 | 'Kint\\Parser\\ClosurePlugin', |
||
150 | 'Kint\\Parser\\ColorPlugin', |
||
151 | 'Kint\\Parser\\DateTimePlugin', |
||
152 | 'Kint\\Parser\\FsPathPlugin', |
||
153 | 'Kint\\Parser\\IteratorPlugin', |
||
154 | 'Kint\\Parser\\JsonPlugin', |
||
155 | 'Kint\\Parser\\MicrotimePlugin', |
||
156 | 'Kint\\Parser\\SimpleXMLElementPlugin', |
||
157 | 'Kint\\Parser\\SplFileInfoPlugin', |
||
158 | 'Kint\\Parser\\SplObjectStoragePlugin', |
||
159 | 'Kint\\Parser\\StreamPlugin', |
||
160 | 'Kint\\Parser\\TablePlugin', |
||
161 | 'Kint\\Parser\\ThrowablePlugin', |
||
162 | 'Kint\\Parser\\TimestampPlugin', |
||
163 | 'Kint\\Parser\\TracePlugin', |
||
164 | 'Kint\\Parser\\XmlPlugin', |
||
165 | ); |
||
166 | |||
167 | protected static $plugin_pool = array(); |
||
168 | |||
169 | protected $parser; |
||
170 | protected $renderer; |
||
171 | |||
172 | public function __construct(Parser $p, Renderer $r) |
||
173 | { |
||
174 | $this->parser = $p; |
||
175 | $this->renderer = $r; |
||
176 | } |
||
177 | |||
178 | public function setParser(Parser $p) |
||
179 | { |
||
180 | $this->parser = $p; |
||
181 | } |
||
182 | |||
183 | public function getParser() |
||
184 | { |
||
185 | return $this->parser; |
||
186 | } |
||
187 | |||
188 | public function setRenderer(Renderer $r) |
||
189 | { |
||
190 | $this->renderer = $r; |
||
191 | } |
||
192 | |||
193 | public function getRenderer() |
||
194 | { |
||
195 | return $this->renderer; |
||
196 | } |
||
197 | |||
198 | public function setStatesFromStatics(array $statics) |
||
199 | { |
||
200 | $this->renderer->setStatics($statics); |
||
201 | |||
202 | $this->parser->setDepthLimit(isset($statics['max_depth']) ? $statics['max_depth'] : false); |
||
203 | $this->parser->clearPlugins(); |
||
204 | |||
205 | if (!isset($statics['plugins'])) { |
||
206 | return; |
||
207 | } |
||
208 | |||
209 | $plugins = array(); |
||
210 | |||
211 | foreach ($statics['plugins'] as $plugin) { |
||
212 | if ($plugin instanceof Plugin) { |
||
213 | $plugins[] = $plugin; |
||
214 | } elseif (\is_string($plugin) && \is_subclass_of($plugin, 'Kint\\Parser\\Plugin')) { |
||
215 | if (!isset(self::$plugin_pool[$plugin])) { |
||
216 | $p = new $plugin(); |
||
217 | self::$plugin_pool[$plugin] = $p; |
||
218 | } |
||
219 | $plugins[] = self::$plugin_pool[$plugin]; |
||
220 | } |
||
221 | } |
||
222 | |||
223 | $plugins = $this->renderer->filterParserPlugins($plugins); |
||
224 | |||
225 | foreach ($plugins as $plugin) { |
||
226 | $this->parser->addPlugin($plugin); |
||
227 | } |
||
228 | } |
||
229 | |||
230 | public function setStatesFromCallInfo(array $info) |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Renders a list of vars including the pre and post renders. |
||
243 | * |
||
244 | * @param array $vars Data to dump |
||
245 | * @param BasicObject[] $base Base objects |
||
246 | * |
||
247 | * @return string |
||
248 | */ |
||
249 | public function dumpAll(array $vars, array $base) |
||
250 | { |
||
251 | if (\array_keys($vars) !== \array_keys($base)) { |
||
252 | throw new InvalidArgumentException('Kint::dumpAll requires arrays of identical size and keys as arguments'); |
||
253 | } |
||
254 | |||
255 | $output = $this->renderer->preRender(); |
||
256 | |||
257 | if ($vars === array()) { |
||
258 | $output .= $this->renderer->renderNothing(); |
||
259 | } |
||
260 | |||
261 | foreach ($vars as $key => $arg) { |
||
262 | if (!$base[$key] instanceof BasicObject) { |
||
263 | throw new InvalidArgumentException('Kint::dumpAll requires all elements of the second argument to be BasicObject instances'); |
||
264 | } |
||
265 | $output .= $this->dumpVar($arg, $base[$key]); |
||
266 | } |
||
267 | |||
268 | $output .= $this->renderer->postRender(); |
||
269 | |||
270 | return $output; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Dumps and renders a var. |
||
275 | * |
||
276 | * @param mixed $var Data to dump |
||
277 | * @param BasicObject $base Base object |
||
278 | * |
||
279 | * @return string |
||
280 | */ |
||
281 | public function dumpVar(&$var, BasicObject $base) |
||
282 | { |
||
283 | return $this->renderer->render( |
||
284 | $this->parser->parse($var, $base) |
||
285 | ); |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Gets all static settings at once. |
||
290 | * |
||
291 | * @return array Current static settings |
||
292 | */ |
||
293 | public static function getStatics() |
||
294 | { |
||
295 | return array( |
||
296 | 'aliases' => self::$aliases, |
||
297 | 'app_root_dirs' => self::$app_root_dirs, |
||
298 | 'cli_detection' => self::$cli_detection, |
||
299 | 'display_called_from' => self::$display_called_from, |
||
300 | 'enabled_mode' => self::$enabled_mode, |
||
301 | 'expanded' => self::$expanded, |
||
302 | 'file_link_format' => self::$file_link_format, |
||
303 | 'max_depth' => self::$max_depth, |
||
304 | 'mode_default' => self::$mode_default, |
||
305 | 'mode_default_cli' => self::$mode_default_cli, |
||
306 | 'plugins' => self::$plugins, |
||
307 | 'renderers' => self::$renderers, |
||
308 | 'return' => self::$return, |
||
309 | ); |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Creates a Kint instances based on static settings. |
||
314 | * |
||
315 | * Also calls setStatesFromStatics for you |
||
316 | * |
||
317 | * @param array $statics array of statics as returned by getStatics |
||
318 | * |
||
319 | * @return null|\Kint\Kint |
||
320 | */ |
||
321 | public static function createFromStatics(array $statics) |
||
322 | { |
||
323 | $mode = false; |
||
324 | |||
325 | if (isset($statics['enabled_mode'])) { |
||
326 | $mode = $statics['enabled_mode']; |
||
327 | |||
328 | if (true === $statics['enabled_mode'] && isset($statics['mode_default'])) { |
||
329 | $mode = $statics['mode_default']; |
||
330 | |||
331 | if (PHP_SAPI === 'cli' && !empty($statics['cli_detection']) && isset($statics['mode_default_cli'])) { |
||
332 | $mode = $statics['mode_default_cli']; |
||
333 | } |
||
334 | } |
||
335 | } |
||
336 | |||
337 | if (!$mode) { |
||
338 | return null; |
||
339 | } |
||
340 | |||
341 | if (!isset($statics['renderers'][$mode])) { |
||
342 | $renderer = new TextRenderer(); |
||
343 | } else { |
||
344 | /** @var Renderer */ |
||
345 | $renderer = new $statics['renderers'][$mode](); |
||
346 | } |
||
347 | |||
348 | return new self(new Parser(), $renderer); |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * Creates base objects given parameter info. |
||
353 | * |
||
354 | * @param array $params Parameters as returned from getCallInfo |
||
355 | * @param int $argc Number of arguments the helper was called with |
||
356 | * |
||
357 | * @return BasicObject[] Base objects for the arguments |
||
358 | */ |
||
359 | public static function getBasesFromParamInfo(array $params, $argc) |
||
360 | { |
||
361 | static $blacklist = array( |
||
362 | 'null', |
||
363 | 'true', |
||
364 | 'false', |
||
365 | 'array(...)', |
||
366 | 'array()', |
||
367 | '[...]', |
||
368 | '[]', |
||
369 | '(...)', |
||
370 | '()', |
||
371 | '"..."', |
||
372 | 'b"..."', |
||
373 | "'...'", |
||
374 | "b'...'", |
||
375 | ); |
||
376 | |||
377 | $params = \array_values($params); |
||
378 | $bases = array(); |
||
379 | |||
380 | for ($i = 0; $i < $argc; ++$i) { |
||
381 | if (isset($params[$i])) { |
||
382 | $param = $params[$i]; |
||
383 | } else { |
||
384 | $param = null; |
||
385 | } |
||
386 | |||
387 | if (!isset($param['name']) || \is_numeric($param['name'])) { |
||
388 | $name = null; |
||
389 | } elseif (\in_array(\strtolower($param['name']), $blacklist, true)) { |
||
390 | $name = null; |
||
391 | } else { |
||
392 | $name = $param['name']; |
||
393 | } |
||
394 | |||
395 | if (isset($param['path'])) { |
||
396 | $access_path = $param['path']; |
||
397 | |||
398 | if (!empty($param['expression'])) { |
||
399 | $access_path = '('.$access_path.')'; |
||
400 | } |
||
401 | } else { |
||
402 | $access_path = '$'.$i; |
||
403 | } |
||
404 | |||
405 | $bases[] = BasicObject::blank($name, $access_path); |
||
406 | } |
||
407 | |||
408 | return $bases; |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * Gets call info from the backtrace, alias, and argument count. |
||
413 | * |
||
414 | * Aliases must be normalized beforehand (Utils::normalizeAliases) |
||
415 | * |
||
416 | * @param array $aliases Call aliases as found in Kint::$aliases |
||
417 | * @param array[] $trace Backtrace |
||
418 | * @param int $argc Number of arguments |
||
419 | * |
||
420 | * @return array{params:null|array, modifiers:array, callee:null|array, caller:null|array, trace:array[]} Call info |
||
421 | */ |
||
422 | public static function getCallInfo(array $aliases, array $trace, $argc) |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * Dumps a backtrace. |
||
478 | * |
||
479 | * Functionally equivalent to Kint::dump(1) or Kint::dump(debug_backtrace(true)) |
||
480 | * |
||
481 | * @return int|string |
||
482 | */ |
||
483 | public static function trace() |
||
484 | { |
||
485 | if (!self::$enabled_mode) { |
||
486 | return 0; |
||
487 | } |
||
488 | |||
489 | Utils::normalizeAliases(self::$aliases); |
||
490 | |||
491 | $args = \func_get_args(); |
||
492 | |||
493 | $call_info = self::getCallInfo(self::$aliases, \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), \count($args)); |
||
494 | |||
495 | $statics = self::getStatics(); |
||
496 | |||
497 | if (\in_array('~', $call_info['modifiers'], true)) { |
||
498 | $statics['enabled_mode'] = self::MODE_TEXT; |
||
499 | } |
||
500 | |||
501 | $kintstance = self::createFromStatics($statics); |
||
502 | if (!$kintstance) { |
||
503 | // Should never happen |
||
504 | return 0; // @codeCoverageIgnore |
||
505 | } |
||
506 | |||
507 | if (\in_array('-', $call_info['modifiers'], true)) { |
||
508 | while (\ob_get_level()) { |
||
509 | \ob_end_clean(); |
||
510 | } |
||
511 | } |
||
512 | |||
513 | $kintstance->setStatesFromStatics($statics); |
||
514 | $kintstance->setStatesFromCallInfo($call_info); |
||
515 | |||
516 | $trimmed_trace = array(); |
||
517 | $trace = \debug_backtrace(true); |
||
|
|||
518 | |||
519 | foreach ($trace as $frame) { |
||
520 | if (Utils::traceFrameIsListed($frame, self::$aliases)) { |
||
521 | $trimmed_trace = array(); |
||
522 | } |
||
523 | |||
524 | $trimmed_trace[] = $frame; |
||
525 | } |
||
526 | |||
527 | $output = $kintstance->dumpAll( |
||
528 | array($trimmed_trace), |
||
529 | array(BasicObject::blank('Kint\\Kint::trace()', 'debug_backtrace(true)')) |
||
530 | ); |
||
531 | |||
532 | if (self::$return || \in_array('@', $call_info['modifiers'], true)) { |
||
533 | return $output; |
||
534 | } |
||
535 | |||
536 | echo $output; |
||
537 | |||
538 | if (\in_array('-', $call_info['modifiers'], true)) { |
||
539 | \flush(); // @codeCoverageIgnore |
||
540 | } |
||
541 | |||
542 | return 0; |
||
543 | } |
||
544 | |||
545 | /** |
||
546 | * Dumps some data. |
||
547 | * |
||
548 | * Functionally equivalent to Kint::dump(1) or Kint::dump(debug_backtrace(true)) |
||
549 | * |
||
550 | * @return int|string |
||
551 | */ |
||
552 | public static function dump() |
||
629 | } |
||
630 | |||
631 | /** |
||
632 | * generic path display callback, can be configured in app_root_dirs; purpose is |
||
633 | * to show relevant path info and hide as much of the path as possible. |
||
634 | * |
||
635 | * @param string $file |
||
636 | * |
||
637 | * @return string |
||
638 | */ |
||
639 | public static function shortenPath($file) |
||
640 | { |
||
641 | $file = \array_values(\array_filter(\explode('/', \str_replace('\\', '/', $file)), 'strlen')); |
||
642 | |||
643 | $longest_match = 0; |
||
644 | $match = '/'; |
||
645 | |||
646 | foreach (self::$app_root_dirs as $path => $alias) { |
||
647 | if (empty($path)) { |
||
648 | continue; |
||
649 | } |
||
650 | |||
651 | $path = \array_values(\array_filter(\explode('/', \str_replace('\\', '/', $path)), 'strlen')); |
||
652 | |||
653 | if (\array_slice($file, 0, \count($path)) === $path && \count($path) > $longest_match) { |
||
654 | $longest_match = \count($path); |
||
655 | $match = $alias; |
||
656 | } |
||
657 | } |
||
658 | |||
659 | if ($longest_match) { |
||
660 | $file = \array_merge(array($match), \array_slice($file, $longest_match)); |
||
661 | |||
662 | return \implode('/', $file); |
||
663 | } |
||
664 | |||
665 | // fallback to find common path with Kint dir |
||
666 | $kint = \array_values(\array_filter(\explode('/', \str_replace('\\', '/', KINT_DIR)), 'strlen')); |
||
667 | |||
668 | foreach ($file as $i => $part) { |
||
669 | if (!isset($kint[$i]) || $kint[$i] !== $part) { |
||
670 | return ($i ? '.../' : '/').\implode('/', \array_slice($file, $i)); |
||
671 | } |
||
672 | } |
||
673 | |||
674 | return '/'.\implode('/', $file); |
||
675 | } |
||
676 | |||
677 | public static function getIdeLink($file, $line) |
||
678 | { |
||
679 | return \str_replace(array('%f', '%l'), array($file, $line), self::$file_link_format); |
||
680 | } |
||
681 | |||
682 | /** |
||
683 | * Returns specific function call info from a stack trace frame, or null if no match could be found. |
||
684 | * |
||
685 | * @param array $frame The stack trace frame in question |
||
686 | * @param int $argc The amount of arguments received |
||
687 | * |
||
688 | * @return null|array{parameters:array, modifiers:array} params and modifiers, or null if a specific call could not be determined |
||
689 | */ |
||
690 | protected static function getSingleCall(array $frame, $argc) |
||
755 | } |
||
756 | } |
||
757 |