Passed
Pull Request — master (#1486)
by Michael
08:32
created

RichRenderer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2013 Jonathan Vollebregt ([email protected]), Rokas Šleinius ([email protected])
9
 *
10
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
11
 * this software and associated documentation files (the "Software"), to deal in
12
 * the Software without restriction, including without limitation the rights to
13
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
14
 * the Software, and to permit persons to whom the Software is furnished to do so,
15
 * subject to the following conditions:
16
 *
17
 * The above copyright notice and this permission notice shall be included in all
18
 * copies or substantial portions of the Software.
19
 *
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
22
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
23
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
 */
27
28
namespace Kint\Renderer;
29
30
use Kint\Renderer\Rich\TabPluginInterface;
31
use Kint\Renderer\Rich\ValuePluginInterface;
32
use Kint\Utils;
33
use Kint\Value\AbstractValue;
34
use Kint\Value\Context\ClassDeclaredContext;
35
use Kint\Value\Context\ContextInterface;
36
use Kint\Value\Context\PropertyContext;
37
use Kint\Value\InstanceValue;
38
use Kint\Value\Representation;
39
use Kint\Value\Representation\ContainerRepresentation;
40
use Kint\Value\Representation\RepresentationInterface;
41
use Kint\Value\Representation\StringRepresentation;
42
use Kint\Value\Representation\ValueRepresentation;
43
use Kint\Value\StringValue;
44
45
/**
46
 * @psalm-import-type Encoding from StringValue
47
 */
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">&rlarr;</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">&mapstodown;</span>';
231
                }
232
                $out .= '<span class="kint-search-trigger" title="Show search box">&telrec;</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 = '&amp;'.$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())) {
0 ignored issues
show
introduced by
The condition null !== $s = $v->getDisplayValue() is always false.
Loading history...
Bug introduced by
Are you sure the assignment to $s is correct as $v->getDisplayValue() targeting Kint\Value\AbstractValue::getDisplayValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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
358
    {
359
        $output = '';
360
361
        if ($this->shouldPreRender()) {
362
            foreach (self::$pre_render_sources as $type => $values) {
363
                $contents = '';
364
                foreach ($values as $v) {
365
                    $contents .= \call_user_func($v, $this);
366
                }
367
368
                if (!\strlen($contents)) {
369
                    continue;
370
                }
371
372
                switch ($type) {
373
                    case 'script':
374
                        $output .= '<script class="kint-rich-script"';
375
                        if (null !== self::$js_nonce) {
376
                            $output .= ' nonce="'.\htmlspecialchars(self::$js_nonce).'"';
377
                        }
378
                        $output .= '>'.$contents.'</script>';
379
                        break;
380
                    case 'style':
381
                        $output .= '<style class="kint-rich-style"';
382
                        if (null !== self::$css_nonce) {
383
                            $output .= ' nonce="'.\htmlspecialchars(self::$css_nonce).'"';
384
                        }
385
                        $output .= '>'.$contents.'</style>';
386
                        break;
387
                    default:
388
                        $output .= $contents;
389
                }
390
            }
391
392
            // Don't pre-render on every dump
393
            if (!$this->force_pre_render) {
394
                self::$needs_pre_render = false;
395
            }
396
        }
397
398
        $output .= '<div class="kint-rich';
399
400
        if ($this->use_folder) {
401
            $output .= ' kint-file';
402
        }
403
404
        $output .= '">';
405
406
        return $output;
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">&mapstodown;</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
598
    {
599
        $hint = $r->getHint();
600
601
        if (null === $hint || !isset(self::$tab_plugins[$hint])) {
602
            return null;
603
        }
604
605
        $plugin = self::$tab_plugins[$hint];
606
607
        if (!\is_a($plugin, TabPluginInterface::class, true)) {
608
            return null;
609
        }
610
611
        if (!isset($this->plugin_objs[$plugin])) {
612
            $this->plugin_objs[$plugin] = new $plugin($this);
613
        }
614
615
        return $this->plugin_objs[$plugin];
616
    }
617
}
618