Debugger   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 20
lcom 2
cbo 7
dl 0
loc 160
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 35 7
A getOptions() 0 4 1
A getBarPanels() 0 4 1
A getBarPanel() 0 6 1
A setBarPanel() 0 5 2
A setBarInfo() 0 7 2
A dump() 0 4 1
A timer() 0 4 1
A createDebuggerTimer() 0 9 2
A dumpStr() 0 4 1
A barDump() 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) 2014-2018 Webino, s. r. o. (http://webino.sk/)
7
 * @license     BSD-3-Clause
8
 */
9
10
namespace WebinoDebug\Service;
11
12
use Tracy\Debugger as Tracy;
13
use WebinoDebug\Debugger\PanelInterface;
14
use WebinoDebug\Debugger\DebuggerBarInterface;
15
use WebinoDebug\Debugger\DebuggerDump;
16
use WebinoDebug\Debugger\DebuggerInterface;
17
use WebinoDebug\Debugger\DebuggerTimer;
18
use WebinoDebug\Options\DebuggerOptions;
19
use Zend\Stdlib\ArrayUtils;
20
21
/**
22
 * Class Debugger
23
 */
24
class Debugger implements
25
    DebuggerInterface,
26
    DebuggerBarInterface
27
{
28
    /**
29
     * @var DebuggerOptions
30
     */
31
    private $options;
32
33
    /**
34
     * @var array
35
     */
36
    protected $barPanels = [];
37
38
    /**
39
     * @var \WebinoDebug\Debugger\DebuggerTimer
40
     */
41
    protected $timerPrototype;
42
43
    /**
44
     * @param array|DebuggerOptions $options
45
     */
46
    public function __construct($options = null)
47
    {
48
        $this->options = ($options instanceof DebuggerOptions)
49
                       ? $options
50
                       : new DebuggerOptions((array) $options);
51
52
        if ($this->options->isDisabled() || Tracy::isEnabled()) {
53
            return;
54
        }
55
56
        Tracy::$customCssFiles = $this->options->getCssFiles();
57
        Tracy::$customJsFiles  = $this->options->getJsFiles();
58
59
        $showBar = $this->options->hasBar();
60
        if ($showBar) {
61
62
            $options->hasBarNoLogo()
0 ignored issues
show
Bug introduced by
It seems like $options is not always an object, but can also be of type array|null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
63
                and Tracy::$customCssFiles[] = __DIR__ . '/../../../data/assets/Debugger/no-logo.css';
64
65
            $options->hasBarNoClose()
66
                and Tracy::$customCssFiles[] = __DIR__ . '/../../../data/assets/Debugger/no-close.css';
67
        }
68
69
        Tracy::$showBar = $showBar;
70
        Tracy::$strictMode = $this->options->getStrict();
71
        Tracy::$maxDepth = $this->options->getMaxDepth();
72
        Tracy::$maxLength = $this->options->getMaxLength();
73
        Tracy::$showFireLogger = $this->options->hasFireLogger();
74
75
        Tracy::enable(
76
            $this->options->getMode(),
77
            $this->options->getLog(),
78
            $this->options->getEmail()
79
        );
80
    }
81
82
    /**
83
     * @return DebuggerOptions
84
     */
85
    public function getOptions()
86
    {
87
        return $this->options;
88
    }
89
90
    /**
91
     * @return PanelInterface[]
92
     */
93
    public function getBarPanels()
94
    {
95
        return $this->barPanels;
96
    }
97
98
    /**
99
     * @param string $id
100
     * @return PanelInterface|null
101
     */
102
    public function getBarPanel($id)
103
    {
104
        /** @var PanelInterface $panel */
105
        $panel = Tracy::getBar()->getPanel($id);
106
        return $panel;
107
    }
108
109
    /**
110
     * Set debugger bar panel
111
     *
112
     * @param object|PanelInterface|null $panel Panel object
113
     * @param string $id Panel id
114
     * @return $this
115
     */
116
    public function setBarPanel(PanelInterface $panel = null, $id = null)
117
    {
118
        $panel and Tracy::getBar()->addPanel($this->barPanels[$id] = $panel, $id);
119
        return $this;
120
    }
121
122
    /**
123
     * Set debugger bar info
124
     *
125
     * @param string $label Info label
126
     * @param string|int $value Info value
127
     * @return $this
128
     */
129
    public function setBarInfo(string $label, $value)
130
    {
131
        /** @var \Tracy\DefaultBarPanel $panel */
132
        $panel = $this->getBarPanel('Tracy:info');
133
        $panel and $panel->data = ArrayUtils::merge((array) $panel->data, [$label => (string) $value]);
134
        return $this;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function dump($subject)
141
    {
142
        return (new DebuggerDump($subject));
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function timer($name = null)
149
    {
150
        return $this->createDebuggerTimer()->setName($name)->start();
151
    }
152
153
    /**
154
     * @return DebuggerTimer
155
     */
156
    private function createDebuggerTimer()
157
    {
158
        if (!$this->timerPrototype) {
159
            /** @var \WebinoDebug\Debugger\TimerPanel $timerPanel */
160
            $timerPanel = $this->getBarPanel('WebinoDebug:timer');
161
            $this->timerPrototype = new DebuggerTimer($timerPanel);
162
        }
163
        return clone $this->timerPrototype;
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     * @deprecated use dump() instead, now it returns stringable object
169
     */
170
    public function dumpStr($subject)
171
    {
172
        return $this->dump($subject);
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     * @deprecated use dump() instead, now it returns object
178
     */
179
    public function barDump($subject, $title = null, array $options = null)
180
    {
181
        return $this->dump($subject)->bar($title);
182
    }
183
}
184