Passed
Push — master ( 3eb4db...99d5ff )
by Kirill
03:12
created

ValueWrapper   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 33
c 1
b 0
f 0
dl 0
loc 82
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B wrap() 0 38 10
A getValues() 0 3 1
A __construct() 0 5 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Exceptions;
13
14
use Spiral\Debug\Dumper;
15
use Spiral\Debug\RendererInterface;
16
17
/**
18
 * Nicely colorize argument and argument values in stack trace.
19
 *
20
 * @internal
21
 */
22
class ValueWrapper
23
{
24
    /** @var RendererInterface */
25
    private $r;
26
27
    /** @var Dumper */
28
    private $dumper;
29
30
    /** @var int */
31
    private $verbosity;
32
33
    /**
34
     * Aggregated list of value dumps, only for DEBUG mode.
35
     *
36
     * @var array
37
     */
38
    private $values = [];
39
40
    /**
41
     * @param Dumper            $dumper
42
     * @param RendererInterface $renderer
43
     * @param int               $verbosity
44
     */
45
    public function __construct(Dumper $dumper, RendererInterface $renderer, int $verbosity)
46
    {
47
        $this->dumper = $dumper;
48
        $this->r = $renderer;
49
        $this->verbosity = $verbosity;
50
    }
51
52
    /**
53
     * @param array $args
54
     * @return array
55
     */
56
    public function wrap(array $args): array
57
    {
58
        $result = [];
59
        foreach ($args as $arg) {
60
            $display = $type = strtolower(gettype($arg));
61
62
            if (is_numeric($arg)) {
63
                $result[] = $this->r->apply($arg, 'value', $type);
64
                continue;
65
            } elseif (is_bool($arg)) {
66
                $result[] = $this->r->apply($arg ? 'true' : 'false', 'value', $type);
67
                continue;
68
            } elseif (is_null($arg)) {
69
                $result[] = $this->r->apply('null', 'value', $type);
70
                continue;
71
            }
72
73
            if (is_object($arg)) {
74
                $reflection = new \ReflectionClass($arg);
75
                $display = sprintf('<span title="%s">%s</span>', $reflection->getName(), $reflection->getShortName());
76
            }
77
78
            $type = $this->r->apply($display, 'value', $type);
79
80
            if ($this->verbosity < HandlerInterface::VERBOSITY_DEBUG) {
81
                $result[] = sprintf('<span>%s</span>', $type);
82
            } else {
83
                $hash = is_object($arg) ? spl_object_hash($arg) : md5(json_encode($arg));
84
85
                if (!isset($this->values[$hash])) {
86
                    $this->values[$hash] = $this->dumper->dump($arg, Dumper::RETURN);
87
                }
88
89
                $result[] = sprintf('<span onclick="_da(\'%s\')">%s</span>', $hash, $type);
90
            }
91
        }
92
93
        return $result;
94
    }
95
96
    /**
97
     * Get all aggregated values for later rendering on a page.
98
     *
99
     * @return array
100
     */
101
    public function getValues(): array
102
    {
103
        return $this->values;
104
    }
105
}
106