Test Failed
Push — main ( 330702...6ea9a3 )
by Rafael
14:31
created

Debug::getCaller()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/* Copyright (C) 2024      Rafael San José      <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace Alxarafe\Tools;
20
21
use Alxarafe\Tools\DebugBarCollector\PhpCollector;
22
use Alxarafe\Tools\DebugBarCollector\TranslatorCollector;
23
use DebugBar\DataCollector\DataCollectorInterface;
24
use DebugBar\DebugBar;
25
use DebugBar\DebugBarException;
26
use DebugBar\JavascriptRenderer;
27
use DebugBar\StandardDebugBar;
28
29
abstract class Debug
30
{
31
    /**
32
     * Private render instance
33
     *
34
     * @var JavascriptRenderer
35
     */
36
    private static JavascriptRenderer $render;
37
38
    /**
39
     * DebugBar instance
40
     *
41
     * @var StandardDebugBar
42
     */
43
    private static StandardDebugBar $debugBar;
44
45
    /**
46
     * Initializes the Debug
47
     *
48
     * @return bool
49
     * @throws DebugBarException
50
     */
51
    public static function initialize()
52
    {
53
        if (isset(self::$debugBar)) {
54
            return true;
55
        }
56
57
        return self::load();
58
    }
59
60
    /**
61
     * Gets the necessary calls to include the debug bar in the page header
62
     *
63
     * @return string
64
     * @throws DebugBarException
65
     */
66
    public static function getRenderHeader(): string
67
    {
68
        $result = "\n<!-- getRenderHeader -->\n";
69
        if (!isset(self::$render)) {
70
            return $result . '<!-- self::$render is not defined -->';
71
        }
72
73
        return $result . self::$render->renderHead();
74
    }
75
76
    /**
77
     * Load the debugbar and collectors.
78
     *
79
     * @return bool
80
     * @throws DebugBarException
81
     */
82
    private static function load(): bool
83
    {
84
        self::$debugBar = new StandardDebugBar();
85
86
        $shortName = 'Debug';
87
        self::startTimer($shortName, $shortName . ' DebugTool Constructor');
88
89
        self::addCollector(new PhpCollector());
90
        self::addCollector(new TranslatorCollector());
91
92
        $baseUrl = constant('BASE_URL') . '/alxarafe/assets/debugbar';
93
        $basePath = realpath(constant('BASE_PATH') . '/..') . '/';
94
95
        self::$render = self::getDebugBar()->getJavascriptRenderer($baseUrl, $basePath);
96
97
        self::stopTimer($shortName);
98
99
        return isset(self::$render);
100
    }
101
102
    /**
103
     * Initialize the timer
104
     *
105
     * @param string $name
106
     * @param string $message
107
     * @throws DebugBarException
108
     */
109
    public static function startTimer(string $name, string $message): void
110
    {
111
        if (!isset(self::$debugBar)) {
112
            return;
113
        }
114
        self::$debugBar['time']->startMeasure($name, $message);
0 ignored issues
show
Bug introduced by
The method startMeasure() does not exist on DebugBar\DataCollector\DataCollectorInterface. It seems like you code against a sub-type of DebugBar\DataCollector\DataCollectorInterface such as DebugBar\DataCollector\TimeDataCollector. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
        self::$debugBar['time']->/** @scrutinizer ignore-call */ 
115
                                 startMeasure($name, $message);
Loading history...
115
    }
116
117
    /**
118
     * Add a new debugbar collector
119
     *
120
     * @param DataCollectorInterface $collector
121
     * @return DebugBar
122
     * @throws DebugBarException
123
     */
124
    public static function addCollector(DataCollectorInterface $collector): DebugBar
125
    {
126
        return self::$debugBar->addCollector($collector);
127
    }
128
129
    /**
130
     * Return the internal debug instance for get the html code.
131
     *
132
     * @return StandardDebugBar|null
133
     */
134
    public static function getDebugBar(): ?StandardDebugBar
135
    {
136
        if (!isset(self::$debugBar)) {
137
            return null;
138
        }
139
        return self::$debugBar;
140
    }
141
142
    /**
143
     * Stop the timer
144
     *
145
     * @param string $name
146
     * @throws DebugBarException
147
     */
148
    public static function stopTimer(string $name): void
149
    {
150
        if (!isset(self::$debugBar)) {
151
            return;
152
        }
153
        self::$debugBar['time']->stopMeasure($name);
0 ignored issues
show
Bug introduced by
The method stopMeasure() does not exist on DebugBar\DataCollector\DataCollectorInterface. It seems like you code against a sub-type of DebugBar\DataCollector\DataCollectorInterface such as DebugBar\DataCollector\TimeDataCollector. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

153
        self::$debugBar['time']->/** @scrutinizer ignore-call */ 
154
                                 stopMeasure($name);
Loading history...
154
    }
155
156
    /**
157
     * Gets the necessary calls to include the debug bar in the page footer
158
     *
159
     * @return string
160
     * @throws DebugBarException
161
     */
162
    public static function getRenderFooter(): string
163
    {
164
        $result = "\n<!-- getRenderFooter -->\n";
165
        if (!isset(self::$render)) {
166
            return $result . '<!-- self::$render is not defined -->';
167
        }
168
169
        return $result . self::$render->render();
170
    }
171
172
    /**
173
     * Add an exception to the exceptions tab of the debug bar.
174
     *
175
     * @param $exception
176
     * @throws DebugBarException
177
     */
178
    public static function addException($exception): void
179
    {
180
        if (!isset(self::$debugBar)) {
181
            return;
182
        }
183
        $caller = self::getCaller();
184
        self::$debugBar['exceptions']->addThrowable($caller['file'] . ' (' . $caller['line'] . '): ' . $exception);
0 ignored issues
show
Bug introduced by
The method addThrowable() does not exist on DebugBar\DataCollector\DataCollectorInterface. It seems like you code against a sub-type of DebugBar\DataCollector\DataCollectorInterface such as DebugBar\DataCollector\ExceptionsCollector. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

184
        self::$debugBar['exceptions']->/** @scrutinizer ignore-call */ 
185
                                       addThrowable($caller['file'] . ' (' . $caller['line'] . '): ' . $exception);
Loading history...
185
    }
186
187
    /**
188
     * Locate last error
189
     *
190
     * @return array
191
     */
192
    private static function getCaller():array
193
    {
194
        $caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[0];
195
        $caller['file'] = substr($caller['file'], strlen(constant('BASE_PATH')) - 7);
196
        return $caller;
197
    }
198
199
    public static function message(string $message): void
200
    {
201
        self::addMessage('messages', $message);
202
    }
203
204
    /**
205
     * Write a message in a channel (tab) of the debug bar.
206
     *
207
     * @param string $channel
208
     * @param string $message
209
     * @throws DebugBarException
210
     */
211
    private static function addMessage(string $channel, string $message): void
212
    {
213
        if (!isset(self::$debugBar)) {
214
            return;
215
        }
216
217
        if (!isset(self::$debugBar[$channel])) {
218
            self::$debugBar->addMessage('channel ' . $channel . ' does not exist. Message: ' . $message);
0 ignored issues
show
Bug introduced by
The method addMessage() does not exist on DebugBar\StandardDebugBar. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

218
            self::$debugBar->/** @scrutinizer ignore-call */ 
219
                             addMessage('channel ' . $channel . ' does not exist. Message: ' . $message);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
219
            return;
220
        }
221
222
        $caller = self::getCaller();
223
        self::$debugBar[$channel]->addMessage($caller['file'] . ' (' . $caller['line'] . '): ' . $message);
0 ignored issues
show
Bug introduced by
The method addMessage() does not exist on DebugBar\DataCollector\DataCollectorInterface. It seems like you code against a sub-type of DebugBar\DataCollector\DataCollectorInterface such as DebugBar\DataCollector\MessagesCollector. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

223
        self::$debugBar[$channel]->/** @scrutinizer ignore-call */ 
224
                                   addMessage($caller['file'] . ' (' . $caller['line'] . '): ' . $message);
Loading history...
224
    }
225
}
226