Test Failed
Push — main ( 82933d...ebe982 )
by Rafael
02:28
created

Debug::addCollector()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 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 DebugBar\DataCollector\DataCollectorInterface;
0 ignored issues
show
Bug introduced by
The type DebugBar\DataCollector\DataCollectorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use DebugBar\DebugBar;
0 ignored issues
show
Bug introduced by
The type DebugBar\DebugBar was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use DebugBar\DebugBarException;
0 ignored issues
show
Bug introduced by
The type DebugBar\DebugBarException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use DebugBar\JavascriptRenderer;
0 ignored issues
show
Bug introduced by
The type DebugBar\JavascriptRenderer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use DebugBar\StandardDebugBar;
0 ignored issues
show
Bug introduced by
The type DebugBar\StandardDebugBar was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
28
abstract class Debug
29
{
30
    /**
31
     * Private render instance
32
     *
33
     * @var JavascriptRenderer
34
     */
35
    private static JavascriptRenderer $render;
36
    /**
37
     * DebugBar instance
38
     *
39
     * @var StandardDebugBar
40
     */
41
    private static StandardDebugBar $debugBar;
42
43
    /**
44
     * DebugTool constructor.
45
     *
46
     * @throws DebugBarException
47
     */
48
    public static function load()
49
    {
50
        $shortName = 'Debug';
51
52
        self::$debugBar = new StandardDebugBar();
53
        self::startTimer($shortName, $shortName . ' DebugTool Constructor');
54
55
        self::addCollector(new PhpCollector());
56
        // self::addCollector(new MessagesCollector('Deprecated'));
57
        // self::addCollector(new MonologCollector(Logger::getLogger()));
58
        // self::addCollector(new TranslatorCollector());
59
60
61
        $baseUrl = constant('BASE_URL') . '/Templates/DebugBar/Resources';
62
        self::$render = self::getDebugBar()->getJavascriptRenderer($baseUrl, constant('BASE_PATH'));
63
64
        self::stopTimer($shortName);
65
    }
66
67
    /**
68
     * Initialize the timer
69
     *
70
     * @param string $name
71
     * @param string $message
72
     */
73
    public static function startTimer(string $name, string $message): void
74
    {
75
        if (!isset(self::$debugBar)) {
76
            self::$debugBar = new StandardDebugBar();
77
        }
78
        self::$debugBar['time']->startMeasure($name, $message);
79
    }
80
81
    public static function addCollector(DataCollectorInterface $collector): DebugBar
82
    {
83
        return self::$debugBar->addCollector($collector);
84
    }
85
86
    /**
87
     * Return the internal debug instance for get the html code.
88
     *
89
     * @return StandardDebugBar
90
     * @throws DebugBarException
91
     */
92
    public static function getDebugBar(): ?StandardDebugBar
93
    {
94
        if (!isset(self::$debugBar)) {
95
            return null;
96
        }
97
        return self::$debugBar;
98
    }
99
100
    /**
101
     * Stop the timer
102
     *
103
     * @param string $name
104
     */
105
    public static function stopTimer(string $name): void
106
    {
107
        self::$debugBar['time']->stopMeasure($name);
108
    }
109
110
    /**
111
     * Gets the necessary calls to include the debug bar in the page header
112
     *
113
     * @return string
114
     */
115
    public static function getRenderHeader(): string
116
    {
117
        return self::$render->renderHead();
118
        if (constant('DEBUG') !== true) {
0 ignored issues
show
Unused Code introduced by
IfNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
119
            return '<!-- Debug is disabled -->';
120
        }
121
        return self::$render->renderHead();
122
    }
123
124
    /**
125
     * Gets the necessary calls to include the debug bar in the page footer
126
     *
127
     * @return string
128
     */
129
    public static function getRenderFooter(): string
130
    {
131
        return self::$render->render();
132
        if (constant('DEBUG') !== true) {
0 ignored issues
show
Unused Code introduced by
IfNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
133
            return '<!-- Debug is disabled -->';
134
        }
135
        return self::$render->render();
136
    }
137
138
    /**
139
     * Add an exception to the exceptions tab of the debug bar.
140
     *
141
     * TODO: addException is deprecated!
142
     *
143
     * @param $exception
144
     */
145
    public static function addException($exception): void
146
    {
147
        if (constant('DEBUG') !== true) {
148
            return;
149
        }
150
        $caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[0];
151
        $caller['file'] = substr($caller['file'], strlen(BASE_PATH) - 7);
0 ignored issues
show
Bug introduced by
The constant Alxarafe\Tools\BASE_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
152
        self::$debugBar['exceptions']->addException($exception); // Use addThrowable instead!
153
        // Logger::info('Exception: ' . $exception->getMessage());
154
    }
155
156
    public static function message(string $message): void
157
    {
158
        self::addMessage('messages', $message);
159
    }
160
161
    /**
162
     * Write a message in a channel (tab) of the debug bar.
163
     *
164
     * @param string $channel
165
     * @param string $message
166
     */
167
    private static function addMessage(string $channel, string $message): void
168
    {
169
        if (!isset(self::$debugBar[$channel])) {
170
            return;
171
        }
172
173
//        if (!defined('DEBUG') || constant('DEBUG') !== true) {
174
//            return;
175
//        }
176
        $caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1];
177
        $caller['file'] = substr($caller['file'], strlen(BASE_PATH) - 7);
0 ignored issues
show
Bug introduced by
The constant Alxarafe\Tools\BASE_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
178
        self::$debugBar[$channel]->addMessage($caller['file'] . ' (' . $caller['line'] . '): ' . $message);
179
    }
180
181
    public static function sqlMessage(string $message): void
182
    {
183
        self::addMessage('SQL', $message);
184
    }
185
}
186