Total Complexity | 113 |
Total Lines | 574 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like RichRenderer 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 RichRenderer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
48 | class RichRenderer extends AbstractRenderer |
||
49 | { |
||
50 | use AssetRendererTrait; |
||
51 | |||
52 | /** |
||
53 | * RichRenderer value plugins should implement ValuePluginInterface. |
||
54 | * |
||
55 | * @psalm-var class-string<ValuePluginInterface>[] |
||
56 | */ |
||
57 | public static array $value_plugins = [ |
||
58 | 'array_limit' => Rich\LockPlugin::class, |
||
59 | 'blacklist' => Rich\LockPlugin::class, |
||
60 | 'callable' => Rich\CallablePlugin::class, |
||
61 | 'color' => Rich\ColorPlugin::class, |
||
62 | 'depth_limit' => Rich\LockPlugin::class, |
||
63 | 'recursion' => Rich\LockPlugin::class, |
||
64 | 'trace_frame' => Rich\TraceFramePlugin::class, |
||
65 | ]; |
||
66 | |||
67 | /** |
||
68 | * RichRenderer tab plugins should implement TabPluginInterface. |
||
69 | * |
||
70 | * @psalm-var array<string, class-string<TabPluginInterface>> |
||
71 | */ |
||
72 | public static array $tab_plugins = [ |
||
73 | 'binary' => Rich\BinaryPlugin::class, |
||
74 | 'callable' => Rich\CallableDefinitionPlugin::class, |
||
75 | 'color' => Rich\ColorPlugin::class, |
||
76 | 'microtime' => Rich\MicrotimePlugin::class, |
||
77 | 'profiling' => Rich\ProfilePlugin::class, |
||
78 | 'source' => Rich\SourcePlugin::class, |
||
79 | 'table' => Rich\TablePlugin::class, |
||
80 | ]; |
||
81 | |||
82 | public static array $pre_render_sources = [ |
||
83 | 'script' => [ |
||
84 | [self::class, 'renderJs'], |
||
85 | ], |
||
86 | 'style' => [ |
||
87 | [self::class, 'renderCss'], |
||
88 | ], |
||
89 | 'raw' => [], |
||
90 | ]; |
||
91 | |||
92 | /** |
||
93 | * The maximum length of a string before it is truncated. |
||
94 | * |
||
95 | * Falsey to disable |
||
96 | */ |
||
97 | public static int $strlen_max = 80; |
||
98 | |||
99 | /** |
||
100 | * Timestamp to print in footer in date() format. |
||
101 | */ |
||
102 | public static ?string $timestamp = null; |
||
103 | |||
104 | /** |
||
105 | * Whether or not to render access paths. |
||
106 | * |
||
107 | * Access paths can become incredibly heavy with very deep and wide |
||
108 | * structures. Given mostly public variables it will typically make |
||
109 | * up one quarter of the output HTML size. |
||
110 | * |
||
111 | * If this is an unacceptably large amount and your browser is groaning |
||
112 | * under the weight of the access paths - your first order of buisiness |
||
113 | * should be to get a new browser. Failing that, use this to turn them off. |
||
114 | */ |
||
115 | public static bool $access_paths = true; |
||
116 | |||
117 | /** |
||
118 | * Assume types and sizes don't need to be escaped. |
||
119 | * |
||
120 | * Turn this off if you use anything but ascii in your class names, |
||
121 | * but it'll cause a slowdown of around 10% |
||
122 | */ |
||
123 | public static bool $escape_types = false; |
||
124 | |||
125 | /** |
||
126 | * Move all dumps to a folder at the bottom of the body. |
||
127 | */ |
||
128 | public static bool $folder = false; |
||
129 | |||
130 | public static bool $needs_pre_render = true; |
||
131 | public static bool $always_pre_render = false; |
||
132 | |||
133 | protected array $plugin_objs = []; |
||
134 | protected bool $expand = false; |
||
135 | protected bool $force_pre_render = false; |
||
136 | protected bool $use_folder = false; |
||
137 | |||
138 | public function __construct() |
||
139 | { |
||
140 | parent::__construct(); |
||
141 | self::$theme ??= 'original.css'; |
||
142 | $this->use_folder = self::$folder; |
||
143 | $this->force_pre_render = self::$always_pre_render; |
||
144 | } |
||
145 | |||
146 | public function setCallInfo(array $info): void |
||
147 | { |
||
148 | parent::setCallInfo($info); |
||
149 | |||
150 | if (\in_array('!', $info['modifiers'], true)) { |
||
151 | $this->expand = true; |
||
152 | $this->use_folder = false; |
||
153 | } |
||
154 | |||
155 | if (\in_array('@', $info['modifiers'], true)) { |
||
156 | $this->force_pre_render = true; |
||
157 | } |
||
158 | } |
||
159 | |||
160 | public function setStatics(array $statics): void |
||
161 | { |
||
162 | parent::setStatics($statics); |
||
163 | |||
164 | if (!empty($statics['expanded'])) { |
||
165 | $this->expand = true; |
||
166 | } |
||
167 | |||
168 | if (!empty($statics['return'])) { |
||
169 | $this->force_pre_render = true; |
||
170 | } |
||
171 | } |
||
172 | |||
173 | public function shouldPreRender(): bool |
||
174 | { |
||
175 | return $this->force_pre_render || self::$needs_pre_render; |
||
176 | } |
||
177 | |||
178 | public function render(AbstractValue $v): string |
||
179 | { |
||
180 | $render_spl_ids_stash = $this->render_spl_ids; |
||
181 | |||
182 | if ($this->render_spl_ids && $v->flags & AbstractValue::FLAG_GENERATED) { |
||
183 | $this->render_spl_ids = false; |
||
184 | } |
||
185 | |||
186 | if ($plugin = $this->getValuePlugin($v)) { |
||
187 | $output = $plugin->renderValue($v); |
||
188 | if (null !== $output && \strlen($output)) { |
||
189 | if (!$this->render_spl_ids && $render_spl_ids_stash) { |
||
190 | $this->render_spl_ids = true; |
||
191 | } |
||
192 | |||
193 | return $output; |
||
194 | } |
||
195 | } |
||
196 | |||
197 | $children = $this->renderChildren($v); |
||
198 | $header = $this->renderHeaderWrapper($v->getContext(), (bool) \strlen($children), $this->renderHeader($v)); |
||
199 | |||
200 | if (!$this->render_spl_ids && $render_spl_ids_stash) { |
||
201 | $this->render_spl_ids = true; |
||
202 | } |
||
203 | |||
204 | return '<dl>'.$header.$children.'</dl>'; |
||
205 | } |
||
206 | |||
207 | public function renderHeaderWrapper(ContextInterface $c, bool $has_children, string $contents): string |
||
208 | { |
||
209 | $out = '<dt'; |
||
210 | |||
211 | if ($has_children) { |
||
212 | $out .= ' class="kint-parent'; |
||
213 | |||
214 | if ($this->expand) { |
||
215 | $out .= ' kint-show'; |
||
216 | } |
||
217 | |||
218 | $out .= '"'; |
||
219 | } |
||
220 | |||
221 | $out .= '>'; |
||
222 | |||
223 | if (self::$access_paths && $c->getDepth() > 0 && null !== ($ap = $c->getAccessPath())) { |
||
224 | $out .= '<span class="kint-access-path-trigger" title="Show access path"></span>'; |
||
225 | } |
||
226 | |||
227 | if ($has_children) { |
||
228 | if (0 === $c->getDepth()) { |
||
229 | if (!$this->use_folder) { |
||
230 | $out .= '<span class="kint-folder-trigger" title="Move to folder"></span>'; |
||
231 | } |
||
232 | $out .= '<span class="kint-search-trigger" title="Show search box"></span>'; |
||
233 | $out .= '<input type="text" class="kint-search" value="">'; |
||
234 | } |
||
235 | |||
236 | $out .= '<nav></nav>'; |
||
237 | } |
||
238 | |||
239 | $out .= $contents; |
||
240 | |||
241 | if (!empty($ap)) { |
||
242 | $out .= '<div class="access-path">'.$this->escape($ap).'</div>'; |
||
243 | } |
||
244 | |||
245 | return $out.'</dt>'; |
||
246 | } |
||
247 | |||
248 | public function renderHeader(AbstractValue $v): string |
||
249 | { |
||
250 | $c = $v->getContext(); |
||
251 | |||
252 | $output = ''; |
||
253 | |||
254 | if ($c instanceof ClassDeclaredContext) { |
||
255 | $output .= '<var>'.$c->getModifiers().'</var> '; |
||
256 | } |
||
257 | |||
258 | $output .= '<dfn>'.$this->escape($v->getDisplayName()).'</dfn> '; |
||
259 | |||
260 | if ($c instanceof PropertyContext && null !== ($s = $c->getHooks())) { |
||
261 | $output .= '<var>'.$this->escape($s).'</var> '; |
||
262 | } |
||
263 | |||
264 | if (null !== ($s = $c->getOperator())) { |
||
265 | $output .= $this->escape($s, 'ASCII').' '; |
||
266 | } |
||
267 | |||
268 | $s = $v->getDisplayType(); |
||
269 | if (self::$escape_types) { |
||
270 | $s = $this->escape($s); |
||
271 | } |
||
272 | |||
273 | if ($c->isRef()) { |
||
274 | $s = '&'.$s; |
||
275 | } |
||
276 | |||
277 | $output .= '<var>'.$s.'</var>'; |
||
278 | |||
279 | if ($v instanceof InstanceValue && $this->shouldRenderObjectIds()) { |
||
280 | $output .= '#'.$v->getSplObjectId(); |
||
281 | } |
||
282 | |||
283 | $output .= ' '; |
||
284 | |||
285 | if (null !== ($s = $v->getDisplaySize())) { |
||
286 | if (self::$escape_types) { |
||
287 | $s = $this->escape($s); |
||
288 | } |
||
289 | $output .= '('.$s.') '; |
||
290 | } |
||
291 | |||
292 | if (null !== ($s = $v->getDisplayValue())) { |
||
293 | $s = \preg_replace('/\\s+/', ' ', $s); |
||
294 | |||
295 | if (self::$strlen_max) { |
||
296 | $s = Utils::truncateString($s, self::$strlen_max); |
||
297 | } |
||
298 | |||
299 | $output .= $this->escape($s); |
||
300 | } |
||
301 | |||
302 | return \trim($output); |
||
303 | } |
||
304 | |||
305 | public function renderChildren(AbstractValue $v): string |
||
306 | { |
||
307 | $contents = []; |
||
308 | $tabs = []; |
||
309 | |||
310 | foreach ($v->getRepresentations() as $rep) { |
||
311 | $result = $this->renderTab($v, $rep); |
||
312 | if (\strlen($result)) { |
||
313 | $contents[] = $result; |
||
314 | $tabs[] = $rep; |
||
315 | } |
||
316 | } |
||
317 | |||
318 | if (empty($tabs)) { |
||
319 | return ''; |
||
320 | } |
||
321 | |||
322 | $output = '<dd>'; |
||
323 | |||
324 | if (1 === \count($tabs) && $tabs[0]->labelIsImplicit()) { |
||
325 | $output .= \reset($contents); |
||
326 | } else { |
||
327 | $output .= '<ul class="kint-tabs">'; |
||
328 | |||
329 | foreach ($tabs as $i => $tab) { |
||
330 | if (0 === $i) { |
||
331 | $output .= '<li class="kint-active-tab">'; |
||
332 | } else { |
||
333 | $output .= '<li>'; |
||
334 | } |
||
335 | |||
336 | $output .= $this->escape($tab->getLabel()).'</li>'; |
||
337 | } |
||
338 | |||
339 | $output .= '</ul><ul class="kint-tab-contents">'; |
||
340 | |||
341 | foreach ($contents as $i => $tab) { |
||
342 | if (0 === $i) { |
||
343 | $output .= '<li class="kint-show">'; |
||
344 | } else { |
||
345 | $output .= '<li>'; |
||
346 | } |
||
347 | |||
348 | $output .= $tab.'</li>'; |
||
349 | } |
||
350 | |||
351 | $output .= '</ul>'; |
||
352 | } |
||
353 | |||
354 | return $output.'</dd>'; |
||
355 | } |
||
356 | |||
357 | public function preRender(): string |
||
407 | } |
||
408 | |||
409 | public function postRender(): string |
||
410 | { |
||
411 | if (!$this->show_trace) { |
||
412 | return '</div>'; |
||
413 | } |
||
414 | |||
415 | $output = '<footer'; |
||
416 | |||
417 | if ($this->expand) { |
||
418 | $output .= ' class="kint-show"'; |
||
419 | } |
||
420 | |||
421 | $output .= '>'; |
||
422 | |||
423 | if (!$this->use_folder) { |
||
424 | $output .= '<span class="kint-folder-trigger" title="Move to folder">↧</span>'; |
||
425 | } |
||
426 | |||
427 | if (!empty($this->trace) && \count($this->trace) > 1) { |
||
428 | $output .= '<nav></nav>'; |
||
429 | } |
||
430 | |||
431 | $output .= $this->calledFrom(); |
||
432 | |||
433 | if (!empty($this->trace) && \count($this->trace) > 1) { |
||
434 | $output .= '<ol>'; |
||
435 | foreach ($this->trace as $index => $step) { |
||
436 | if (!$index) { |
||
437 | continue; |
||
438 | } |
||
439 | |||
440 | $output .= '<li>'.$this->ideLink($step['file'], $step['line']); // closing tag not required |
||
441 | if (isset($step['function']) && |
||
442 | !\in_array($step['function'], ['include', 'include_once', 'require', 'require_once'], true) |
||
443 | ) { |
||
444 | $output .= ' ['; |
||
445 | $output .= $step['class'] ?? ''; |
||
446 | $output .= $step['type'] ?? ''; |
||
447 | $output .= $step['function'].'()]'; |
||
448 | } |
||
449 | } |
||
450 | $output .= '</ol>'; |
||
451 | } |
||
452 | |||
453 | $output .= '</footer></div>'; |
||
454 | |||
455 | return $output; |
||
456 | } |
||
457 | |||
458 | /** |
||
459 | * @psalm-param Encoding $encoding |
||
460 | */ |
||
461 | public function escape(string $string, $encoding = false): string |
||
462 | { |
||
463 | if (false === $encoding) { |
||
464 | $encoding = Utils::detectEncoding($string); |
||
465 | } |
||
466 | |||
467 | $original_encoding = $encoding; |
||
468 | |||
469 | if (false === $encoding || 'ASCII' === $encoding) { |
||
470 | $encoding = 'UTF-8'; |
||
471 | } |
||
472 | |||
473 | $string = \htmlspecialchars($string, ENT_NOQUOTES, $encoding); |
||
474 | |||
475 | // this call converts all non-ASCII characters into numeirc htmlentities |
||
476 | if (\function_exists('mb_encode_numericentity') && 'ASCII' !== $original_encoding) { |
||
477 | $string = \mb_encode_numericentity($string, [0x80, 0xFFFF, 0, 0xFFFF], $encoding); |
||
478 | } |
||
479 | |||
480 | return $string; |
||
481 | } |
||
482 | |||
483 | public function ideLink(string $file, int $line): string |
||
484 | { |
||
485 | $path = $this->escape(Utils::shortenPath($file)).':'.$line; |
||
486 | $ideLink = self::getFileLink($file, $line); |
||
487 | |||
488 | if (null === $ideLink) { |
||
489 | return $path; |
||
490 | } |
||
491 | |||
492 | return '<a href="'.$this->escape($ideLink).'">'.$path.'</a>'; |
||
493 | } |
||
494 | |||
495 | protected function calledFrom(): string |
||
496 | { |
||
497 | $output = ''; |
||
498 | |||
499 | if (isset($this->callee['file'])) { |
||
500 | $output .= ' '.$this->ideLink( |
||
501 | $this->callee['file'], |
||
502 | $this->callee['line'] |
||
503 | ); |
||
504 | } |
||
505 | |||
506 | if ( |
||
507 | isset($this->callee['function']) && |
||
508 | ( |
||
509 | !empty($this->callee['class']) || |
||
510 | !\in_array( |
||
511 | $this->callee['function'], |
||
512 | ['include', 'include_once', 'require', 'require_once'], |
||
513 | true |
||
514 | ) |
||
515 | ) |
||
516 | ) { |
||
517 | $output .= ' ['; |
||
518 | $output .= $this->callee['class'] ?? ''; |
||
519 | $output .= $this->callee['type'] ?? ''; |
||
520 | $output .= $this->callee['function'].'()]'; |
||
521 | } |
||
522 | |||
523 | if ('' !== $output) { |
||
524 | $output = 'Called from'.$output; |
||
525 | } |
||
526 | |||
527 | if (null !== self::$timestamp) { |
||
528 | $output .= ' '.\date(self::$timestamp); |
||
529 | } |
||
530 | |||
531 | return $output; |
||
532 | } |
||
533 | |||
534 | protected function renderTab(AbstractValue $v, RepresentationInterface $rep): string |
||
535 | { |
||
536 | if ($plugin = $this->getTabPlugin($rep)) { |
||
537 | $output = $plugin->renderTab($rep, $v); |
||
538 | if (null !== $output) { |
||
539 | return $output; |
||
540 | } |
||
541 | } |
||
542 | |||
543 | if ($rep instanceof ValueRepresentation) { |
||
544 | return $this->render($rep->getValue()); |
||
545 | } |
||
546 | |||
547 | if ($rep instanceof ContainerRepresentation) { |
||
548 | $output = ''; |
||
549 | |||
550 | foreach ($rep->getContents() as $obj) { |
||
551 | $output .= $this->render($obj); |
||
552 | } |
||
553 | |||
554 | return $output; |
||
555 | } |
||
556 | |||
557 | if ($rep instanceof StringRepresentation) { |
||
558 | // If we're dealing with the content representation |
||
559 | if ($v instanceof StringValue && $rep->getValue() === $v->getValue()) { |
||
560 | // Only show the contents if: |
||
561 | if (\preg_match('/(:?[\\r\\n\\t\\f\\v]| {2})/', $rep->getValue())) { |
||
562 | // We have unrepresentable whitespace (Without whitespace preservation) |
||
563 | $show_contents = true; |
||
564 | } elseif (self::$strlen_max && Utils::strlen($v->getDisplayValue()) > self::$strlen_max) { |
||
565 | // We had to truncate getDisplayValue |
||
566 | $show_contents = true; |
||
567 | } else { |
||
568 | $show_contents = false; |
||
569 | } |
||
570 | } else { |
||
571 | $show_contents = true; |
||
572 | } |
||
573 | |||
574 | if ($show_contents) { |
||
575 | return '<pre>'.$this->escape($rep->getValue())."\n</pre>"; |
||
576 | } |
||
577 | } |
||
578 | |||
579 | return ''; |
||
580 | } |
||
581 | |||
582 | protected function getValuePlugin(AbstractValue $v): ?ValuePluginInterface |
||
583 | { |
||
584 | $hint = $v->getHint(); |
||
585 | |||
586 | if (null === $hint || !isset(self::$value_plugins[$hint])) { |
||
587 | return null; |
||
588 | } |
||
589 | |||
590 | $plugin = self::$value_plugins[$hint]; |
||
591 | |||
592 | if (!\is_a($plugin, ValuePluginInterface::class, true)) { |
||
593 | return null; |
||
594 | } |
||
595 | |||
596 | if (!isset($this->plugin_objs[$plugin])) { |
||
597 | $this->plugin_objs[$plugin] = new $plugin($this); |
||
598 | } |
||
599 | |||
600 | return $this->plugin_objs[$plugin]; |
||
601 | } |
||
602 | |||
603 | protected function getTabPlugin(RepresentationInterface $r): ?TabPluginInterface |
||
622 | } |
||
623 | } |
||
624 |