Debuger   A
last analyzed

Complexity

Total Complexity 40

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 40
eloc 81
dl 0
loc 168
ccs 77
cts 77
cp 1
rs 9.2
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isDebugBar() 0 3 1
B consoleIsActive() 0 10 8
D getBacktrace() 0 42 18
A init() 0 27 5
A getDebugBar() 0 3 1
A initConsole() 0 25 5
A addLogs() 0 4 2

How to fix   Complexity   

Complex Class

Complex classes like Debuger often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Debuger, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Debugger basic file.
4
 *
5
 * @package App
6
 *
7
 * @copyright YetiForce S.A.
8
 * @license   YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Mariusz Krzaczkowski <[email protected]>
10
 */
11
12
namespace App;
13
14
use DebugBar\DataCollector;
15
16
/**
17
 * Debuger basic class.
18
 */
19
class Debuger
20
{
21
	/**
22
	 * @var \DebugBar\Debuger
0 ignored issues
show
Bug introduced by
The type DebugBar\Debuger 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
	 */
24
	protected static $debugBar;
25
26
	/**
27
	 * Initiating debugging console.
28
	 *
29
	 * @return \App\DebugBar\Debuger
0 ignored issues
show
Bug introduced by
The type App\DebugBar\Debuger 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...
30
	 */
31
	public static function initConsole()
32 2
	{
33
		if (\App\Config::debug('DISPLAY_DEBUG_CONSOLE') && static::consoleIsActive()) {
34 2
			$debugbar = new Debug\DebugBar\DebugBar();
35 2
			$debugbar->addCollector(new DataCollector\PhpInfoCollector());
36 2
			$debugbar->addCollector(new DataCollector\ExceptionsCollector());
37 2
			$debugbar->addCollector(new DataCollector\RequestDataCollector());
38 2
			$debugbar->addCollector(new DataCollector\MemoryCollector());
39 2
			$debugbar->addCollector(new DataCollector\TimeDataCollector());
40 1
			if (\App\Config::debug('DISPLAY_LOGS_IN_CONSOLE')) {
41
				$debugbar->addCollector(new Debug\DebugBarLogs());
42 2
			}
43
			if (\App\Config::debug('DISPLAY_CONFIG_IN_CONSOLE')) {
44 2
				$debugbar->addCollector(new \DebugBar\DataCollector\ConfigCollector([
45
					'debug' => \App\Config::debug(),
46
					'developer' => \App\Config::developer(),
47
					'performance' => \App\Config::performance(),
48
					'api' => \App\Config::api(),
49
					'security' => \App\Config::security(),
50
					'search' => \App\Config::search(),
51
					'sounds' => \App\Config::sounds(),
52 1
					'relation' => \App\Config::relation(),
53
				]));
54 1
			}
55
			static::$debugBar = $debugbar;
0 ignored issues
show
Documentation Bug introduced by
It seems like $debugbar of type App\Debug\DebugBar\DebugBar is incompatible with the declared type DebugBar\Debuger of property $debugBar.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
56
		}
57
	}
58
59
	/**
60
	 * Get Debuger instance.
61
	 *
62 1
	 * @return \App\Debug\DebugBar\DebugBar
63
	 */
64 1
	public static function getDebugBar()
65
	{
66
		return static::$debugBar;
67
	}
68
69
	/**
70
	 * Checking is active debugging.
71
	 *
72 3
	 * @return bool
73
	 */
74 3
	public static function isDebugBar()
75
	{
76
		return isset(static::$debugBar);
77 1
	}
78
79 1
	public static function addLogs($message, $level, $traces)
80 1
	{
81
		if (isset(static::$debugBar['logs'])) {
82 1
			static::$debugBar['logs']->addMessage($message, $level, $traces);
83
		}
84
	}
85
86
	/**
87 1
	 * Initiating debugging.
88
	 */
89 1
	public static function init()
90 1
	{
91
		$targets = [];
92 1
		if (\App\Config::debug('LOG_TO_FILE')) {
93 1
			$levels = \App\Config::debug('LOG_LEVELS');
94 1
			$target = [
95
				'class' => 'App\Log\FileTarget',
96 1
			];
97
			if (false !== $levels) {
98 1
				$target['levels'] = $levels;
99 1
			}
100
			$targets['file'] = $target;
101 1
		}
102
		if (\App\Config::debug('LOG_TO_PROFILE')) {
103 1
			$levels = \App\Config::debug('LOG_LEVELS');
104 1
			$target = [
105
				'class' => 'App\Log\Profiling',
106 1
			];
107
			if (false !== $levels) {
108 1
				$target['levels'] = $levels;
109 1
			}
110
			$targets['profiling'] = $target;
111 1
		}
112
		\Yii::createObject([
113 1
			'class' => 'yii\log\Dispatcher',
114 1
			'traceLevel' => \App\Config::debug('LOG_TRACE_LEVEL'),
115 1
			'targets' => $targets,
116 1
		]);
117
	}
118 1
119
	/**
120
	 * Checking console is active.
121
	 *
122
	 * @return bool
123
	 */
124
	public static function consoleIsActive()
125 2
	{
126
		$ips = \Config\Debug::$DEBUG_CONSOLE_ALLOWED_IPS;
127 2
		if (false === $ips || (\is_string($ips) && RequestUtil::getRemoteIP(true) === $ips) || (\is_array($ips) && \in_array(RequestUtil::getRemoteIP(true), $ips))) {
128 2
			if (\Config\Debug::$DEBUG_CONSOLE_ALLOWED_USERS && !\in_array(\App\User::getCurrentUserRealId(), \Config\Debug::$DEBUG_CONSOLE_ALLOWED_USERS)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression Config\Debug::DEBUG_CONSOLE_ALLOWED_USERS of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
129 2
				return false;
130
			}
131 1
			return true;
132
		}
133
		return false;
134
	}
135
136
	/**
137
	 * Generates a backtrace.
138
	 *
139
	 * @param int    $minLevel
140
	 * @param int    $maxLevel
141
	 * @param string $sep
142
	 *
143 6389
	 * @return string
144
	 */
145 6389
	public static function getBacktrace($minLevel = 1, $maxLevel = 0, $sep = '#')
146 6389
	{
147 6389
		$trace = '';
148 6389
		foreach (debug_backtrace() as $k => $v) {
149
			if ($k < $minLevel) {
150 6389
				continue;
151 6389
			}
152 6389
			$l = $k - $minLevel;
153 6389
			$args = '';
154 6389
			if (isset($v['args'])) {
155 6389
				foreach ($v['args'] as &$arg) {
156 6006
					if (!\is_array($arg) && !\is_object($arg) && !\is_resource($arg)) {
157 150
						$args .= var_export($arg, true);
158 150
					} elseif (\is_array($arg)) {
159 147
						$args .= '[';
160 147
						foreach ($arg as &$a) {
161 35
							$val = $a;
162 35
							if (\is_array($a) || \is_object($a) || \is_resource($a)) {
163 9
								$val = \gettype($a);
164
								if (\is_object($a)) {
165
									$val .= '(' . \get_class($a) . ')';
166 147
								}
167
							}
168 150
							$args .= $val . ',';
169
						}
170 6389
						$args = rtrim($args, ',') . ']';
171
					}
172 6389
					$args .= ',';
173
				}
174 6389
				$args = rtrim($args, ',');
175 6389
			}
176 6389
			$trace .= "$sep$l";
177
			if (isset($v['line'])) {
178 6389
				$trace .= " {$v['file']}:{$v['line']}";
179 6389
			}
180 6389
			$trace .= '  >>  ' . (isset($v['class']) ? $v['class'] . '->' : '') . "{$v['function']}($args)" . PHP_EOL;
181 6388
			unset($args, $val, $v, $k, $a);
182
			if (0 !== $maxLevel && $l >= $maxLevel) {
183
				break;
184 6389
			}
185
		}
186
		return rtrim(str_replace(ROOT_DIRECTORY . \DIRECTORY_SEPARATOR, '', $trace), PHP_EOL);
187
	}
188
}
189