Test Failed
Push — main ( a8a1f8...d105a1 )
by Rafael
11:47
created

Debug   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 155
rs 10
c 0
b 0
f 0
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 25 2
A startTimer() 0 6 2
A addCollector() 0 3 1
A getDebugBar() 0 3 1
A addException() 0 9 2
A getRenderHeader() 0 6 2
A getRenderFooter() 0 6 2
A stopTimer() 0 3 1
A addMessage() 0 8 2
1
<?php
2
/**
3
 * Copyright (C) 2022-2023  Rafael San José Tovar   <[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
 * (at your option) 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\Core\Singletons;
20
21
use Alxarafe\Core\Singletons\DebugBarCollectors\PhpCollector;
22
use DebugBar\DataCollector\DataCollectorInterface;
23
use DebugBar\DataCollector\MessagesCollector;
24
use DebugBar\DebugBar;
25
use DebugBar\JavascriptRenderer;
26
use DebugBar\StandardDebugBar;
27
28
class Debug
29
{
30
    /**
31
     * Private logger instance
32
     *
33
     * @var Logger
34
     */
35
    //    public static Logger $logger;
36
    /**
37
     * Private render instance
38
     *
39
     * @var JavascriptRenderer
40
     */
41
    private static JavascriptRenderer $render;
42
    /**
43
     * DebugBar instance
44
     *
45
     * @var StandardDebugBar
46
     */
47
    private static StandardDebugBar $debugBar;
48
49
    /**
50
     * DebugTool constructor.
51
     *
52
     * @throws DebugBarException
53
     */
54
    public function __construct()
55
    {
56
        //        self::$logger = Logger::getInstance();
57
58
        //        $shortName = ClassUtils::getShortName($this, $this);
59
        if (!defined('DEBUG')) {
60
            define('DEBUG', false);
61
        }
62
        $shortName = 'Debug';
63
64
        self::$debugBar = new StandardDebugBar();
65
        $this->startTimer($shortName, $shortName . ' DebugTool Constructor');
66
67
        self::addCollector(new MessagesCollector('SQL'));
68
        self::addCollector(new PhpCollector());
69
        self::addCollector(new MessagesCollector('Deprecated'));
70
        //        self::addCollector(new MonologCollector(self::$logger->getLogger()));
71
72
        //        $translator = Translator::getInstance();
73
        //        self::addCollector(new TranslatorCollector($translator));
74
75
        $baseUrl = VENDOR_URI . '/maximebf/debugbar/src/DebugBar/Resources';
76
        self::$render = self::getDebugBar()->getJavascriptRenderer($baseUrl, constant('BASE_FOLDER'));
77
78
        $this->stopTimer($shortName);
79
    }
80
81
    public static function addCollector(DataCollectorInterface $collector): DebugBar
82
    {
83
        return self::$debugBar->addCollector($collector);
84
    }
85
86
    /**
87
     * Initialize the timer
88
     *
89
     * @param string $name
90
     * @param string $message
91
     */
92
    public static function startTimer(string $name, string $message): void
93
    {
94
        if (!isset(self::$debugBar)) {
95
            self::$debugBar = new StandardDebugBar();
96
        }
97
        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

97
        self::$debugBar['time']->/** @scrutinizer ignore-call */ 
98
                                 startMeasure($name, $message);
Loading history...
98
    }
99
100
    /**
101
     * Return the internal debug instance for get the html code.
102
     *
103
     * TODO: Analizar qué funciones harían falta para el html y retornar el html.
104
     * Tal y como está ahora mismo sería dependiente de DebugBar. DebugBar debería
105
     * de quedar TOTALMENTE encapsulado en esta clase.
106
     *
107
     * @return StandardDebugBar
108
     * @throws DebugBarException
109
     */
110
    public static function getDebugBar(): StandardDebugBar
111
    {
112
        return self::$debugBar;
113
    }
114
115
    /**
116
     * Stop the timer
117
     *
118
     * @param string $name
119
     */
120
    public static function stopTimer(string $name): void
121
    {
122
        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

122
        self::$debugBar['time']->/** @scrutinizer ignore-call */ 
123
                                 stopMeasure($name);
Loading history...
123
    }
124
125
    /**
126
     * Gets the necessary calls to include the debug bar in the page header
127
     *
128
     * @return string
129
     */
130
    public static function getRenderHeader(): string
131
    {
132
        if (constant('DEBUG') !== true) {
133
            return '';
134
        }
135
        return self::$render->renderHead();
136
    }
137
138
    /**
139
     * Gets the necessary calls to include the debug bar in the page footer
140
     *
141
     * @return string
142
     */
143
    public static function getRenderFooter(): string
144
    {
145
        if (constant('DEBUG') !== true) {
146
            return '';
147
        }
148
        return self::$render->render();
149
    }
150
151
    /**
152
     * Add an exception to the exceptions tab of the debug bar.
153
     *
154
     * TODO: addException is deprecated!
155
     *
156
     * @param $exception
157
     */
158
    public static function addException($exception): void
159
    {
160
        if (constant('DEBUG') !== true) {
161
            return;
162
        }
163
        $caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[0];
164
        $caller['file'] = substr($caller['file'], strlen(BASE_FOLDER));
165
        self::$debugBar['exceptions']->addException($exception); // Use addThrowable instead!
0 ignored issues
show
Bug introduced by
The method addException() 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

165
        self::$debugBar['exceptions']->/** @scrutinizer ignore-call */ 
166
                                       addException($exception); // Use addThrowable instead!
Loading history...
166
        Logger::info('Exception: ' . $exception->getMessage());
0 ignored issues
show
Bug introduced by
The method info() does not exist on Alxarafe\Core\Singletons\Logger. ( Ignorable by Annotation )

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

166
        Logger::/** @scrutinizer ignore-call */ 
167
                info('Exception: ' . $exception->getMessage());

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...
167
    }
168
169
    /**
170
     * Write a message in a channel (tab) of the debug bar.
171
     *
172
     * @param string $channel
173
     * @param string $message
174
     */
175
    public static function addMessage(string $channel, string $message): void
176
    {
177
        if (constant('DEBUG') !== true) {
178
            return;
179
        }
180
        $caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[0];
181
        $caller['file'] = substr($caller['file'], strlen(BASE_FOLDER));
182
        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

182
        self::$debugBar[$channel]->/** @scrutinizer ignore-call */ 
183
                                   addMessage($caller['file'] . ' (' . $caller['line'] . '): ' . $message);
Loading history...
183
    }
184
}
185