Total Complexity | 92 |
Total Lines | 620 |
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 |
||
51 | class Kint implements FacadeInterface |
||
52 | { |
||
53 | public const MODE_RICH = 'r'; |
||
54 | public const MODE_TEXT = 't'; |
||
55 | public const MODE_CLI = 'c'; |
||
56 | public const MODE_PLAIN = 'p'; |
||
57 | |||
58 | /** |
||
59 | * @var mixed Kint mode |
||
60 | * |
||
61 | * false: Disabled |
||
62 | * true: Enabled, default mode selection |
||
63 | * other: Manual mode selection |
||
64 | * |
||
65 | * @psalm-var KintMode |
||
66 | */ |
||
67 | public static $enabled_mode = true; |
||
68 | |||
69 | /** |
||
70 | * Default mode. |
||
71 | * |
||
72 | * @psalm-var KintMode |
||
73 | */ |
||
74 | public static $mode_default = self::MODE_RICH; |
||
75 | |||
76 | /** |
||
77 | * Default mode in CLI with cli_detection on. |
||
78 | * |
||
79 | * @psalm-var KintMode |
||
80 | */ |
||
81 | public static $mode_default_cli = self::MODE_CLI; |
||
82 | |||
83 | /** |
||
84 | * @var bool enable detection when Kint is command line. |
||
85 | * |
||
86 | * Formats output with whitespace only; does not HTML-escape it |
||
87 | */ |
||
88 | public static bool $cli_detection = true; |
||
89 | |||
90 | /** |
||
91 | * @var bool Return output instead of echoing |
||
92 | */ |
||
93 | public static bool $return = false; |
||
94 | |||
95 | /** |
||
96 | * @var int depth limit for array/object traversal. 0 for no limit |
||
97 | */ |
||
98 | public static int $depth_limit = 7; |
||
99 | |||
100 | /** |
||
101 | * @var bool expand all trees by default for rich view |
||
102 | */ |
||
103 | public static bool $expanded = false; |
||
104 | |||
105 | /** |
||
106 | * @var bool whether to display where kint was called from |
||
107 | */ |
||
108 | public static bool $display_called_from = true; |
||
109 | |||
110 | /** |
||
111 | * @var array Kint aliases. Add debug functions in Kint wrappers here to fix modifiers and backtraces |
||
112 | */ |
||
113 | public static array $aliases = [ |
||
114 | [self::class, 'dump'], |
||
115 | [self::class, 'trace'], |
||
116 | [self::class, 'dumpAll'], |
||
117 | ]; |
||
118 | |||
119 | /** |
||
120 | * @psalm-var array<RendererInterface|class-string<ConstructableRendererInterface>> |
||
121 | * |
||
122 | * Array of modes to renderer class names |
||
123 | */ |
||
124 | public static array $renderers = [ |
||
125 | self::MODE_RICH => Renderer\RichRenderer::class, |
||
126 | self::MODE_PLAIN => Renderer\PlainRenderer::class, |
||
127 | self::MODE_TEXT => TextRenderer::class, |
||
128 | self::MODE_CLI => Renderer\CliRenderer::class, |
||
129 | ]; |
||
130 | |||
131 | /** |
||
132 | * @psalm-var array<PluginInterface|class-string<ConstructablePluginInterface>> |
||
133 | */ |
||
134 | public static array $plugins = [ |
||
135 | \Kint\Parser\ArrayLimitPlugin::class, |
||
136 | \Kint\Parser\ArrayObjectPlugin::class, |
||
137 | \Kint\Parser\Base64Plugin::class, |
||
138 | \Kint\Parser\BinaryPlugin::class, |
||
139 | \Kint\Parser\BlacklistPlugin::class, |
||
140 | \Kint\Parser\ClassHooksPlugin::class, |
||
141 | \Kint\Parser\ClassMethodsPlugin::class, |
||
142 | \Kint\Parser\ClassStaticsPlugin::class, |
||
143 | \Kint\Parser\ClassStringsPlugin::class, |
||
144 | \Kint\Parser\ClosurePlugin::class, |
||
145 | \Kint\Parser\ColorPlugin::class, |
||
146 | \Kint\Parser\DateTimePlugin::class, |
||
147 | \Kint\Parser\DomPlugin::class, |
||
148 | \Kint\Parser\EnumPlugin::class, |
||
149 | \Kint\Parser\FsPathPlugin::class, |
||
150 | \Kint\Parser\HtmlPlugin::class, |
||
151 | \Kint\Parser\IteratorPlugin::class, |
||
152 | \Kint\Parser\JsonPlugin::class, |
||
153 | \Kint\Parser\MicrotimePlugin::class, |
||
154 | \Kint\Parser\MysqliPlugin::class, |
||
155 | // \Kint\Parser\SerializePlugin::class, |
||
156 | \Kint\Parser\SimpleXMLElementPlugin::class, |
||
157 | \Kint\Parser\SplFileInfoPlugin::class, |
||
158 | \Kint\Parser\StreamPlugin::class, |
||
159 | \Kint\Parser\TablePlugin::class, |
||
160 | \Kint\Parser\ThrowablePlugin::class, |
||
161 | \Kint\Parser\TimestampPlugin::class, |
||
162 | \Kint\Parser\ToStringPlugin::class, |
||
163 | \Kint\Parser\TracePlugin::class, |
||
164 | \Kint\Parser\XmlPlugin::class, |
||
165 | ]; |
||
166 | |||
167 | protected Parser $parser; |
||
168 | protected RendererInterface $renderer; |
||
169 | |||
170 | public function __construct(Parser $p, RendererInterface $r) |
||
171 | { |
||
172 | $this->parser = $p; |
||
173 | $this->renderer = $r; |
||
174 | } |
||
175 | |||
176 | public function setParser(Parser $p): void |
||
177 | { |
||
178 | $this->parser = $p; |
||
179 | } |
||
180 | |||
181 | public function getParser(): Parser |
||
182 | { |
||
183 | return $this->parser; |
||
184 | } |
||
185 | |||
186 | public function setRenderer(RendererInterface $r): void |
||
187 | { |
||
188 | $this->renderer = $r; |
||
189 | } |
||
190 | |||
191 | public function getRenderer(): RendererInterface |
||
192 | { |
||
193 | return $this->renderer; |
||
194 | } |
||
195 | |||
196 | public function setStatesFromStatics(array $statics): void |
||
197 | { |
||
198 | $this->renderer->setStatics($statics); |
||
199 | |||
200 | $this->parser->setDepthLimit($statics['depth_limit'] ?? 0); |
||
201 | $this->parser->clearPlugins(); |
||
202 | |||
203 | if (!isset($statics['plugins'])) { |
||
204 | return; |
||
205 | } |
||
206 | |||
207 | $plugins = []; |
||
208 | |||
209 | foreach ($statics['plugins'] as $plugin) { |
||
210 | if ($plugin instanceof PluginInterface) { |
||
211 | $plugins[] = $plugin; |
||
212 | } elseif (\is_string($plugin) && \is_a($plugin, ConstructablePluginInterface::class, true)) { |
||
213 | $plugins[] = new $plugin($this->parser); |
||
214 | } |
||
215 | } |
||
216 | |||
217 | $plugins = $this->renderer->filterParserPlugins($plugins); |
||
218 | |||
219 | foreach ($plugins as $plugin) { |
||
220 | try { |
||
221 | $this->parser->addPlugin($plugin); |
||
222 | } catch (InvalidArgumentException $e) { |
||
223 | \trigger_error( |
||
224 | 'Plugin '.Utils::errorSanitizeString(\get_class($plugin)).' could not be added to a Kint parser: '.Utils::errorSanitizeString($e->getMessage()), |
||
225 | E_USER_WARNING |
||
226 | ); |
||
227 | } |
||
228 | } |
||
229 | } |
||
230 | |||
231 | public function setStatesFromCallInfo(array $info): void |
||
240 | } |
||
241 | |||
242 | public function dumpAll(array $vars, array $base): string |
||
243 | { |
||
244 | if (\array_keys($vars) !== \array_keys($base)) { |
||
245 | throw new InvalidArgumentException('Kint::dumpAll requires arrays of identical size and keys as arguments'); |
||
246 | } |
||
247 | |||
248 | if ([] === $vars) { |
||
249 | return $this->dumpNothing(); |
||
250 | } |
||
251 | |||
252 | $output = $this->renderer->preRender(); |
||
253 | |||
254 | foreach ($vars as $key => $_) { |
||
255 | if (!$base[$key] instanceof ContextInterface) { |
||
256 | throw new InvalidArgumentException('Kint::dumpAll requires all elements of the second argument to be ContextInterface instances'); |
||
257 | } |
||
258 | $output .= $this->dumpVar($vars[$key], $base[$key]); |
||
259 | } |
||
260 | |||
261 | $output .= $this->renderer->postRender(); |
||
262 | |||
263 | return $output; |
||
264 | } |
||
265 | |||
266 | protected function dumpNothing(): string |
||
267 | { |
||
268 | $output = $this->renderer->preRender(); |
||
269 | $output .= $this->renderer->render(new UninitializedValue(new BaseContext('No argument'))); |
||
270 | $output .= $this->renderer->postRender(); |
||
271 | |||
272 | return $output; |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Dumps and renders a var. |
||
277 | * |
||
278 | * @param mixed &$var Data to dump |
||
279 | */ |
||
280 | protected function dumpVar(&$var, ContextInterface $c): string |
||
281 | { |
||
282 | return $this->renderer->render( |
||
283 | $this->parser->parse($var, $c) |
||
284 | ); |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Gets all static settings at once. |
||
289 | * |
||
290 | * @return array Current static settings |
||
291 | */ |
||
292 | public static function getStatics(): array |
||
293 | { |
||
294 | return [ |
||
295 | 'aliases' => static::$aliases, |
||
296 | 'cli_detection' => static::$cli_detection, |
||
297 | 'depth_limit' => static::$depth_limit, |
||
298 | 'display_called_from' => static::$display_called_from, |
||
299 | 'enabled_mode' => static::$enabled_mode, |
||
300 | 'expanded' => static::$expanded, |
||
301 | 'mode_default' => static::$mode_default, |
||
302 | 'mode_default_cli' => static::$mode_default_cli, |
||
303 | 'plugins' => static::$plugins, |
||
304 | 'renderers' => static::$renderers, |
||
305 | 'return' => static::$return, |
||
306 | ]; |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Creates a Kint instance based on static settings. |
||
311 | * |
||
312 | * @param array $statics array of statics as returned by getStatics |
||
313 | */ |
||
314 | public static function createFromStatics(array $statics): ?FacadeInterface |
||
315 | { |
||
316 | $mode = false; |
||
317 | |||
318 | if (isset($statics['enabled_mode'])) { |
||
319 | $mode = $statics['enabled_mode']; |
||
320 | |||
321 | if (true === $mode && isset($statics['mode_default'])) { |
||
322 | $mode = $statics['mode_default']; |
||
323 | |||
324 | if (PHP_SAPI === 'cli' && !empty($statics['cli_detection']) && isset($statics['mode_default_cli'])) { |
||
325 | $mode = $statics['mode_default_cli']; |
||
326 | } |
||
327 | } |
||
328 | } |
||
329 | |||
330 | if (false === $mode) { |
||
331 | return null; |
||
332 | } |
||
333 | |||
334 | $renderer = null; |
||
335 | if (isset($statics['renderers'][$mode])) { |
||
336 | if ($statics['renderers'][$mode] instanceof RendererInterface) { |
||
337 | $renderer = $statics['renderers'][$mode]; |
||
338 | } |
||
339 | |||
340 | if (\is_a($statics['renderers'][$mode], ConstructableRendererInterface::class, true)) { |
||
341 | $renderer = new $statics['renderers'][$mode](); |
||
342 | } |
||
343 | } |
||
344 | |||
345 | $renderer ??= new TextRenderer(); |
||
346 | |||
347 | return new static(new Parser(), $renderer); |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * Creates base contexts given parameter info. |
||
352 | * |
||
353 | * @psalm-param list<CallParameter> $params |
||
354 | * |
||
355 | * @return BaseContext[] Base contexts for the arguments |
||
356 | */ |
||
357 | public static function getBasesFromParamInfo(array $params, int $argc): array |
||
358 | { |
||
359 | $bases = []; |
||
360 | |||
361 | for ($i = 0; $i < $argc; ++$i) { |
||
362 | $param = $params[$i] ?? null; |
||
363 | |||
364 | if (!empty($param['literal'])) { |
||
365 | $name = 'literal'; |
||
366 | } else { |
||
367 | $name = $param['name'] ?? '$'.$i; |
||
368 | } |
||
369 | |||
370 | if (isset($param['path'])) { |
||
371 | $access_path = $param['path']; |
||
372 | |||
373 | if ($param['expression']) { |
||
374 | $access_path = '('.$access_path.')'; |
||
375 | } elseif ($param['new_without_parens']) { |
||
376 | $access_path .= '()'; |
||
377 | } |
||
378 | } else { |
||
379 | $access_path = '$'.$i; |
||
380 | } |
||
381 | |||
382 | $base = new BaseContext($name); |
||
383 | $base->access_path = $access_path; |
||
384 | $bases[] = $base; |
||
385 | } |
||
386 | |||
387 | return $bases; |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * Gets call info from the backtrace, alias, and argument count. |
||
392 | * |
||
393 | * Aliases must be normalized beforehand (Utils::normalizeAliases) |
||
394 | * |
||
395 | * @param array $aliases Call aliases as found in Kint::$aliases |
||
396 | * @param array[] $trace Backtrace |
||
397 | * @param array $args Arguments |
||
398 | * |
||
399 | * @return array Call info |
||
400 | * |
||
401 | * @psalm-param list<non-empty-array> $trace |
||
402 | */ |
||
403 | public static function getCallInfo(array $aliases, array $trace, array $args): array |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * Dumps a backtrace. |
||
457 | * |
||
458 | * Functionally equivalent to Kint::dump(1) or Kint::dump(debug_backtrace(true)) |
||
459 | * |
||
460 | * @return int|string |
||
461 | */ |
||
462 | public static function trace() |
||
463 | { |
||
464 | if (false === static::$enabled_mode) { |
||
465 | return 0; |
||
466 | } |
||
467 | |||
468 | static::$aliases = Utils::normalizeAliases(static::$aliases); |
||
469 | |||
470 | $call_info = static::getCallInfo(static::$aliases, \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), []); |
||
471 | |||
472 | $statics = static::getStatics(); |
||
473 | |||
474 | if (\in_array('~', $call_info['modifiers'], true)) { |
||
475 | $statics['enabled_mode'] = static::MODE_TEXT; |
||
476 | } |
||
477 | |||
478 | $kintstance = static::createFromStatics($statics); |
||
479 | if (!$kintstance) { |
||
|
|||
480 | return 0; |
||
481 | } |
||
482 | |||
483 | if (\in_array('-', $call_info['modifiers'], true)) { |
||
484 | while (\ob_get_level()) { |
||
485 | \ob_end_clean(); |
||
486 | } |
||
487 | } |
||
488 | |||
489 | $kintstance->setStatesFromStatics($statics); |
||
490 | $kintstance->setStatesFromCallInfo($call_info); |
||
491 | |||
492 | $trimmed_trace = []; |
||
493 | $trace = \debug_backtrace(); |
||
494 | |||
495 | foreach ($trace as $frame) { |
||
496 | if (Utils::traceFrameIsListed($frame, static::$aliases)) { |
||
497 | $trimmed_trace = []; |
||
498 | } |
||
499 | |||
500 | $trimmed_trace[] = $frame; |
||
501 | } |
||
502 | |||
503 | \array_shift($trimmed_trace); |
||
504 | |||
505 | $base = new BaseContext('Kint\\Kint::trace()'); |
||
506 | $base->access_path = 'debug_backtrace()'; |
||
507 | $output = $kintstance->dumpAll([$trimmed_trace], [$base]); |
||
508 | |||
509 | if (static::$return || \in_array('@', $call_info['modifiers'], true)) { |
||
510 | return $output; |
||
511 | } |
||
512 | |||
513 | echo $output; |
||
514 | |||
515 | if (\in_array('-', $call_info['modifiers'], true)) { |
||
516 | \flush(); // @codeCoverageIgnore |
||
517 | } |
||
518 | |||
519 | return 0; |
||
520 | } |
||
521 | |||
522 | /** |
||
523 | * Dumps some data. |
||
524 | * |
||
525 | * Functionally equivalent to Kint::dump(1) or Kint::dump(debug_backtrace()) |
||
526 | * |
||
527 | * @psalm-param mixed ...$args |
||
528 | * |
||
529 | * @return int|string |
||
530 | */ |
||
531 | public static function dump(...$args) |
||
575 | } |
||
576 | |||
577 | /** |
||
578 | * Returns specific function call info from a stack trace frame, or null if no match could be found. |
||
579 | * |
||
580 | * @param array $frame The stack trace frame in question |
||
581 | * @param array $args The arguments |
||
582 | * |
||
583 | * @return ?array params and modifiers, or null if a specific call could not be determined |
||
584 | */ |
||
585 | protected static function getSingleCall(array $frame, array $args): ?array |
||
671 | } |
||
672 | } |
||
673 |