| Total Complexity | 112 |
| Total Lines | 568 |
| 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->use_folder) { |
||
| 418 | $output .= '<span class="kint-folder-trigger" title="Move to folder">↧</span>'; |
||
| 419 | } |
||
| 420 | |||
| 421 | if (!empty($this->trace) && \count($this->trace) > 1) { |
||
| 422 | $output .= '<nav></nav>'; |
||
| 423 | } |
||
| 424 | |||
| 425 | $output .= $this->calledFrom(); |
||
| 426 | |||
| 427 | if (!empty($this->trace) && \count($this->trace) > 1) { |
||
| 428 | $output .= '<ol>'; |
||
| 429 | foreach ($this->trace as $index => $step) { |
||
| 430 | if (!$index) { |
||
| 431 | continue; |
||
| 432 | } |
||
| 433 | |||
| 434 | $output .= '<li>'.$this->ideLink($step['file'], $step['line']); // closing tag not required |
||
| 435 | if (isset($step['function']) && |
||
| 436 | !\in_array($step['function'], ['include', 'include_once', 'require', 'require_once'], true) |
||
| 437 | ) { |
||
| 438 | $output .= ' ['; |
||
| 439 | $output .= $step['class'] ?? ''; |
||
| 440 | $output .= $step['type'] ?? ''; |
||
| 441 | $output .= $step['function'].'()]'; |
||
| 442 | } |
||
| 443 | } |
||
| 444 | $output .= '</ol>'; |
||
| 445 | } |
||
| 446 | |||
| 447 | $output .= '</footer></div>'; |
||
| 448 | |||
| 449 | return $output; |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @psalm-param Encoding $encoding |
||
| 454 | */ |
||
| 455 | public function escape(string $string, $encoding = false): string |
||
| 456 | { |
||
| 457 | if (false === $encoding) { |
||
| 458 | $encoding = Utils::detectEncoding($string); |
||
| 459 | } |
||
| 460 | |||
| 461 | $original_encoding = $encoding; |
||
| 462 | |||
| 463 | if (false === $encoding || 'ASCII' === $encoding) { |
||
| 464 | $encoding = 'UTF-8'; |
||
| 465 | } |
||
| 466 | |||
| 467 | $string = \htmlspecialchars($string, ENT_NOQUOTES, $encoding); |
||
| 468 | |||
| 469 | // this call converts all non-ASCII characters into numeirc htmlentities |
||
| 470 | if (\function_exists('mb_encode_numericentity') && 'ASCII' !== $original_encoding) { |
||
| 471 | $string = \mb_encode_numericentity($string, [0x80, 0xFFFF, 0, 0xFFFF], $encoding); |
||
| 472 | } |
||
| 473 | |||
| 474 | return $string; |
||
| 475 | } |
||
| 476 | |||
| 477 | public function ideLink(string $file, int $line): string |
||
| 478 | { |
||
| 479 | $path = $this->escape(Utils::shortenPath($file)).':'.$line; |
||
| 480 | $ideLink = self::getFileLink($file, $line); |
||
| 481 | |||
| 482 | if (null === $ideLink) { |
||
| 483 | return $path; |
||
| 484 | } |
||
| 485 | |||
| 486 | return '<a href="'.$this->escape($ideLink).'">'.$path.'</a>'; |
||
| 487 | } |
||
| 488 | |||
| 489 | protected function calledFrom(): string |
||
| 490 | { |
||
| 491 | $output = ''; |
||
| 492 | |||
| 493 | if (isset($this->callee['file'])) { |
||
| 494 | $output .= ' '.$this->ideLink( |
||
| 495 | $this->callee['file'], |
||
| 496 | $this->callee['line'] |
||
| 497 | ); |
||
| 498 | } |
||
| 499 | |||
| 500 | if ( |
||
| 501 | isset($this->callee['function']) && |
||
| 502 | ( |
||
| 503 | !empty($this->callee['class']) || |
||
| 504 | !\in_array( |
||
| 505 | $this->callee['function'], |
||
| 506 | ['include', 'include_once', 'require', 'require_once'], |
||
| 507 | true |
||
| 508 | ) |
||
| 509 | ) |
||
| 510 | ) { |
||
| 511 | $output .= ' ['; |
||
| 512 | $output .= $this->callee['class'] ?? ''; |
||
| 513 | $output .= $this->callee['type'] ?? ''; |
||
| 514 | $output .= $this->callee['function'].'()]'; |
||
| 515 | } |
||
| 516 | |||
| 517 | if ('' !== $output) { |
||
| 518 | $output = 'Called from'.$output; |
||
| 519 | } |
||
| 520 | |||
| 521 | if (null !== self::$timestamp) { |
||
| 522 | $output .= ' '.\date(self::$timestamp); |
||
| 523 | } |
||
| 524 | |||
| 525 | return $output; |
||
| 526 | } |
||
| 527 | |||
| 528 | protected function renderTab(AbstractValue $v, RepresentationInterface $rep): string |
||
| 529 | { |
||
| 530 | if ($plugin = $this->getTabPlugin($rep)) { |
||
| 531 | $output = $plugin->renderTab($rep, $v); |
||
| 532 | if (null !== $output) { |
||
| 533 | return $output; |
||
| 534 | } |
||
| 535 | } |
||
| 536 | |||
| 537 | if ($rep instanceof ValueRepresentation) { |
||
| 538 | return $this->render($rep->getValue()); |
||
| 539 | } |
||
| 540 | |||
| 541 | if ($rep instanceof ContainerRepresentation) { |
||
| 542 | $output = ''; |
||
| 543 | |||
| 544 | foreach ($rep->getContents() as $obj) { |
||
| 545 | $output .= $this->render($obj); |
||
| 546 | } |
||
| 547 | |||
| 548 | return $output; |
||
| 549 | } |
||
| 550 | |||
| 551 | if ($rep instanceof StringRepresentation) { |
||
| 552 | // If we're dealing with the content representation |
||
| 553 | if ($v instanceof StringValue && $rep->getValue() === $v->getValue()) { |
||
| 554 | // Only show the contents if: |
||
| 555 | if (\preg_match('/(:?[\\r\\n\\t\\f\\v]| {2})/', $rep->getValue())) { |
||
| 556 | // We have unrepresentable whitespace (Without whitespace preservation) |
||
| 557 | $show_contents = true; |
||
| 558 | } elseif (self::$strlen_max && Utils::strlen($v->getDisplayValue()) > self::$strlen_max) { |
||
| 559 | // We had to truncate getDisplayValue |
||
| 560 | $show_contents = true; |
||
| 561 | } else { |
||
| 562 | $show_contents = false; |
||
| 563 | } |
||
| 564 | } else { |
||
| 565 | $show_contents = true; |
||
| 566 | } |
||
| 567 | |||
| 568 | if ($show_contents) { |
||
| 569 | return '<pre>'.$this->escape($rep->getValue())."\n</pre>"; |
||
| 570 | } |
||
| 571 | } |
||
| 572 | |||
| 573 | return ''; |
||
| 574 | } |
||
| 575 | |||
| 576 | protected function getValuePlugin(AbstractValue $v): ?ValuePluginInterface |
||
| 577 | { |
||
| 578 | $hint = $v->getHint(); |
||
| 579 | |||
| 580 | if (null === $hint || !isset(self::$value_plugins[$hint])) { |
||
| 581 | return null; |
||
| 582 | } |
||
| 583 | |||
| 584 | $plugin = self::$value_plugins[$hint]; |
||
| 585 | |||
| 586 | if (!\is_a($plugin, ValuePluginInterface::class, true)) { |
||
| 587 | return null; |
||
| 588 | } |
||
| 589 | |||
| 590 | if (!isset($this->plugin_objs[$plugin])) { |
||
| 591 | $this->plugin_objs[$plugin] = new $plugin($this); |
||
| 592 | } |
||
| 593 | |||
| 594 | return $this->plugin_objs[$plugin]; |
||
| 595 | } |
||
| 596 | |||
| 597 | protected function getTabPlugin(RepresentationInterface $r): ?TabPluginInterface |
||
| 616 | } |
||
| 617 | } |
||
| 618 |