DebuggerDump   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 0
loc 161
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setReturn() 0 5 1
A setCollapse() 0 5 1
A setMaxDepth() 0 5 1
A setMaxLength() 0 5 1
A getOptions() 0 8 3
A dump() 0 13 3
A bar() 0 5 1
A log() 0 5 1
A dumpReturn() 0 6 1
A __toString() 0 4 1
1
<?php
2
/**
3
 * Webino (http://webino.sk/)
4
 *
5
 * @link        https://github.com/webino/WebinoDebug/ for the canonical source repository
6
 * @copyright   Copyright (c) 2018 Webino, s. r. o. (http://webino.sk/)
7
 * @license     BSD-3-Clause
8
 */
9
10
namespace WebinoDebug\Debugger;
11
12
use Tracy\Debugger as Tracy;
13
use Tracy\Dumper as Dumper;
14
15
/**
16
 * Class DebuggerDump
17
 */
18
class DebuggerDump
19
{
20
    /**
21
     * @var mixed Variable to dump
22
     */
23
    protected $var;
24
25
    /**
26
     * Dump variable to debugger bar
27
     *
28
     * @var bool
29
     */
30
    protected $toBar = false;
31
32
    /**
33
     * Return dump as string
34
     *
35
     * @var bool
36
     */
37
    protected $return = false;
38
39
    /**
40
     * Collapse structure with length above defined
41
     *
42
     * @var int
43
     */
44
    protected $collapse = 1;
45
46
    /**
47
     * Maximum structure depth
48
     *
49
     * @var int
50
     */
51
    protected $maxDepth;
52
53
    /**
54
     * Maximum data length
55
     *
56
     * @var int
57
     */
58
    protected $maxLength;
59
60
    /**
61
     * @param mixed $var Variable to dump
62
     */
63
    public function __construct($var)
64
    {
65
        $this->var = $var;
66
    }
67
68
    /**
69
     * Return as string
70
     *
71
     * @param bool $return
72
     * @return $this
73
     */
74
    public function setReturn($return = true)
75
    {
76
        $this->return = (bool) $return;
77
        return $this;
78
    }
79
80
    /**
81
     * @param int $collapse
82
     * @return $this
83
     */
84
    public function setCollapse(int $collapse = 1)
85
    {
86
        $this->collapse = $collapse;
87
        return $this;
88
    }
89
90
    /**
91
     * @param int $maxDepth
92
     * @return $this
93
     */
94
    public function setMaxDepth(int $maxDepth)
95
    {
96
        $this->maxDepth = $maxDepth;
97
        return $this;
98
    }
99
100
    /**
101
     * @param int $maxLength
102
     * @return $this
103
     */
104
    public function setMaxLength(int $maxLength)
105
    {
106
        $this->maxLength = $maxLength;
107
        return $this;
108
    }
109
110
    protected function getOptions()
111
    {
112
        return [
113
            Dumper::DEPTH    => $this->maxDepth ?: Tracy::$maxDepth,
114
            Dumper::TRUNCATE => $this->maxLength ?: Tracy::$maxLength,
115
            Dumper::COLLAPSE => $this->collapse,
116
        ];
117
    }
118
119
    /**
120
     * Return or output a variable dump
121
     *
122
     * @return string|null
123
     */
124
    public function dump() : ?string
125
    {
126
        if ($this->return) {
127
            return $this->dumpReturn();
128
129
        } elseif (!Tracy::$productionMode) {
130
            Dumper::dump($this->var, $this->getOptions() + [
131
                Dumper::LOCATION => Tracy::$showLocation,
132
            ]);
133
        }
134
135
        return null;
136
    }
137
138
    /**
139
     * Dump to debugger bar
140
     *
141
     * @param string|null $title Dump title
142
     * @return $this
143
     */
144
    public function bar(string $title = null)
145
    {
146
        Tracy::barDump($this->var, $title, $this->getOptions());
147
        return $this;
148
    }
149
150
    /**
151
     * Dump to PHP error log
152
     *
153
     * @return $this
154
     */
155
    public function log()
156
    {
157
        error_log(print_r($this->var, true));
158
        return $this;
159
    }
160
161
    /**
162
     * @return string
163
     */
164
    protected function dumpReturn()
165
    {
166
        ob_start();
167
        Dumper::dump($this->var, $this->getOptions());
168
        return ob_get_clean();
169
    }
170
171
    /**
172
     * @return string
173
     */
174
    public function __toString() : string
175
    {
176
        return (string) $this->dumpReturn();
177
    }
178
}
179