Passed
Push — master ( eccce4...83fadc )
by Paul
02:16
created

Application   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 317
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 317
rs 8.6
c 0
b 0
f 0
wmc 37

21 Methods

Rating   Name   Duplication   Size   Complexity  
A filterNoconflictStyles() 0 5 1
A filterNoconflictScripts() 0 4 1
A registerLanguages() 0 3 1
A render() 0 6 2
A filterBodyClasses() 0 3 1
A errorHandler() 0 23 3
A init() 0 14 1
A __construct() 0 4 1
A enqueueAssets() 0 5 1
A initProfiler() 0 4 2
A renderBar() 0 12 1
A convertToMiliseconds() 0 3 1
A getIncludedFiles() 0 11 2
A getTemplates() 0 9 3
A getErrorsLabel() 0 11 4
A path() 0 3 1
B getQueries() 0 29 2
A getQueriesLabel() 0 13 3
A getErrors() 0 20 4
A url() 0 3 1
A getProfilerLabel() 0 4 1
1
<?php
2
3
namespace GeminiLabs\BlackBar;
4
5
use GeminiLabs\BlackBar\Profiler;
6
7
final class Application
8
{
9
	const DEBUG = 'debug';
10
	const ID = 'blackbar';
11
	const LANG = '/languages/';
12
13
	protected $errors = array();
14
	protected $file;
15
	protected $profiler;
16
17
	public function __construct()
18
	{
19
		$this->file = realpath( dirname( __DIR__ ).'/'.static::ID.'.php' );
20
		$this->profiler = new Profiler;
21
	}
22
23
	/**
24
	 * @return void
25
	 * @action admin_enqueue_scripts
26
	 * @action wp_enqueue_scripts
27
	 */
28
	public function enqueueAssets()
29
	{
30
		wp_enqueue_script( static::ID, $this->url( 'assets/main.js' ));
31
		wp_enqueue_style( static::ID, $this->url( 'assets/main.css' ), array( 'dashicons' ));
32
		wp_enqueue_style( static::ID.'-syntax', $this->url( 'assets/syntax.css' ));
33
	}
34
35
	/**
36
	 * @param int $errno
37
	 * @param string $errstr
38
	 * @param string $errfile
39
	 * @param int $errline
40
	 * @return void
41
	 */
42
	public function errorHandler( $errno, $errstr, $errfile, $errline )
43
	{
44
		$errorCodes = array(
45
			2 => 'Warning',
46
			8 => 'Notice',
47
			2048 => 'Strict',
48
			8192 => 'Deprecated',
49
		);
50
		$errname = array_key_exists( $errno, $errorCodes )
51
			? $errorCodes[$errno]
52
			: 'Unknown';
53
		$hash = md5( $errno.$errstr.$errfile.$errline );
54
		if( array_key_exists( $hash, $this->errors )) {
55
			$this->errors[$hash]['count']++;
56
		}
57
		else {
58
			$this->errors[$hash] = array(
59
				"errno" => $errno,
60
				"message" => $errstr,
61
				"file" => $errfile,
62
				"name" => $errname,
63
				"line" => $errline,
64
				"count" => 0,
65
			);
66
		}
67
	}
68
69
	/**
70
	 * @param string $classes
71
	 * @return string
72
	 */
73
	public function filterBodyClasses( $classes )
74
	{
75
		return trim( $classes.' '.static::ID );
76
	}
77
78
	/**
79
	 * @return array
80
	 */
81
	public function filterNoconflictScripts( $scripts )
82
	{
83
		$scripts[] = static::ID;
84
		return $scripts;
85
	}
86
87
	/**
88
	 * @return array
89
	 */
90
	public function filterNoconflictStyles( $styles )
91
	{
92
		$styles[] = static::ID;
93
		$styles[] = static::ID.'-syntax';
94
		return $styles;
95
	}
96
97
	/**
98
	 * @return void
99
	 */
100
	public function init()
101
	{
102
		add_action( 'admin_enqueue_scripts',    array( $this, 'enqueueAssets' ));
103
		add_action( 'admin_footer',             array( $this, 'renderBar' ));
104
		add_action( 'plugins_loaded',           array( $this, 'registerLanguages' ));
105
		add_action( 'wp_enqueue_scripts',       array( $this, 'enqueueAssets' ));
106
		add_action( 'wp_footer',                array( $this, 'renderBar' ));
107
		add_filter( 'admin_body_class',         array( $this, 'filterBodyClasses' ));
108
		add_filter( 'all',                      array( $this, 'initProfiler' ));
109
		add_filter( 'gform_noconflict_scripts', array( $this, 'filterNoconflictScripts' ));
110
		add_filter( 'gform_noconflict_styles',  array( $this, 'filterNoconflictStyles' ));
111
		apply_filters( 'debug', 'Profiler Started' );
112
		apply_filters( 'debug', 'blackbar/profiler/noise' );
113
		set_error_handler( array( $this, 'errorHandler' ), E_ALL|E_STRICT );
114
	}
115
116
	/**
117
	 * @return void
118
	 * @action all
119
	 */
120
	public function initProfiler()
121
	{
122
		if( func_get_arg(0) != static::DEBUG )return;
123
		$this->profiler->trace( func_get_arg(1) );
124
	}
125
126
	/**
127
	 * @return void
128
	 */
129
	public function registerLanguages()
130
	{
131
		load_plugin_textdomain( static::ID, false, plugin_basename( $this->path() ).static::LANG );
132
	}
133
134
	/**
135
	 * Render a view and pass any provided data to the view
136
	 *
137
	 * @param string $view
138
	 * @return void
139
	 */
140
	public function render( $view, array $data = array() )
141
	{
142
		$file = $this->path( sprintf( 'views/%s.php', str_replace( '.php', '', $view )));
143
		if( !file_exists( $file ))return;
144
		extract( $data );
145
		include $file;
146
	}
147
148
	/**
149
	 * @return void
150
	 * @action admin_footer
151
	 * @action wp_footer
152
	 */
153
	public function renderBar()
154
	{
155
		apply_filters( 'debug', 'Profiler Stopped' );
156
		$this->render( 'debug-bar', array(
157
			'blackbar' => $this,
158
			'errors' => $this->getErrors(),
159
			'errorsLabel' => $this->getErrorsLabel(),
160
			'profiler' => $this->profiler,
161
			'profilerLabel' => $this->getProfilerLabel(),
162
			'queries' => $this->getQueries(),
163
			'queriesLabel' => $this->getQueriesLabel(),
164
			'templates' => $this->getTemplates(),
165
		));
166
	}
167
168
	/**
169
	 * @return string
170
	 */
171
	protected function convertToMiliseconds( $time, $decimals = 2 )
172
	{
173
		return number_format( $time * 1000, $decimals );
174
	}
175
176
	/**
177
	 * @return array
178
	 */
179
	protected function getErrors()
180
	{
181
		$errors = array();
182
		foreach( $this->errors as $error ) {
183
			if( $error['errno'] == E_WARNING ) {
184
				$error['name'] = '<span class="glbb-error">'.$error['name'].'</span>';
185
			}
186
			if( $error['count'] > 1 ) {
187
				$error['name'] .= ' ('.$error['count'].')';
188
			}
189
			$errors[] = array(
190
				'name' => $error['name'],
191
				'message' => sprintf( __( '%s on line %s in file %s', 'blackbar' ),
192
					$error['message'],
193
					$error['line'],
194
					$error['file']
195
				),
196
			);
197
		}
198
		return $errors;
199
	}
200
201
	/**
202
	 * @return string
203
	 */
204
	protected function getErrorsLabel()
205
	{
206
		$warningCount = 0;
207
		foreach( $this->errors as $error ) {
208
			if( $error['errno'] == E_WARNING ) {
209
				$warningCount++;
210
			}
211
		}
212
		$errorCount = count( $this->errors );
213
		$warnings = $warningCount > 0 ? sprintf( ', %d!', $warningCount ) : '';
214
		return sprintf( __( 'Errors (%s)', 'blackbar' ), $errorCount.$warnings );
215
	}
216
217
	/**
218
	 * @return array
219
	 */
220
	protected function getIncludedFiles()
221
	{
222
		$includes = array_values( array_filter( get_included_files(), function( $include ) {
223
			$bool = strpos( $include, '/themes/' ) !== false
224
				&& strpos( $include, '/functions.php' ) === false;
225
			return (bool)apply_filters( 'blackbar/templates/include', $bool, $include );
226
		}));
227
		return array_map( function( $key, $value ) {
228
			$value = str_replace( trailingslashit( WP_CONTENT_DIR ), '', $value );
229
			return sprintf( '[%s] => %s', $key, $value );
230
		}, array_keys( $includes ), $includes );
231
	}
232
233
	/**
234
	 * @return string
235
	 */
236
	protected function getProfilerLabel()
237
	{
238
		$profilerTime = $this->convertToMiliseconds( $this->profiler->getTotalTime(), 0 );
239
		return sprintf( __( 'Profiler (%s ms)', 'blackbar' ), $profilerTime );
240
	}
241
242
	/**
243
	 * @return array
244
	 */
245
	protected function getQueries()
246
	{
247
		global $wpdb;
248
		$queries = array();
249
		$search = array(
250
			'AND',
251
			'FROM',
252
			'GROUP BY',
253
			'INNER JOIN',
254
			'LIMIT',
255
			'ON DUPLICATE KEY UPDATE',
256
			'ORDER BY',
257
			'SET',
258
			'WHERE',
259
		);
260
		$replace = array_map( function( $value ) {
261
			return PHP_EOL.$value;
262
		}, $search );
263
		foreach( $wpdb->queries as $query ) {
264
			$miliseconds = number_format( round( $query[1] * 1000, 4 ), 4 );
265
			$sql = preg_replace( '/\s\s+/', ' ', trim( $query[0] ));
266
			$sql = str_replace( PHP_EOL, ' ', $sql );
267
			$sql = str_replace( $search, $replace, $sql );
268
			$queries[] = array(
269
				'ms' => $miliseconds,
270
				'sql' => $sql,
271
			);
272
		}
273
		return $queries;
274
	}
275
276
	/**
277
	 * @return string
278
	 */
279
	protected function getQueriesLabel()
280
	{
281
		if( !SAVEQUERIES ) {
282
			return __( 'SQL', 'blackbar' );
283
		}
284
		global $wpdb;
285
		$queryTime = 0;
286
		foreach( $wpdb->queries as $query ) {
287
			$queryTime += $query[1];
288
		}
289
		$queriesCount = '<span class="glbb-queries-count">'.count( $wpdb->queries ).'</span>';
290
		$queriesTime = '<span class="glbb-queries-time">'.$this->convertToMiliseconds( $queryTime ).'</span>';
291
		return sprintf( __( 'SQL (%s queries in %s ms)', 'blackbar' ), $queriesCount, $queriesTime );
292
	}
293
294
	/**
295
	 * @return void|string
296
	 */
297
	protected function getTemplates()
298
	{
299
		if( is_admin() )return;
300
		if( class_exists( '\GeminiLabs\Castor\Facades\Development' )) {
301
			ob_start();
302
			\GeminiLabs\Castor\Facades\Development::printTemplatePaths();
1 ignored issue
show
Bug introduced by
The type GeminiLabs\Castor\Facades\Development 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...
303
			return ob_get_clean();
304
		}
305
		return '<pre>'.implode( PHP_EOL, $this->getIncludedFiles() ).'</pre>';
306
	}
307
308
	/**
309
	 * @param string $file
310
	 * @return string
311
	 */
312
	protected function path( $file = '' )
313
	{
314
		return plugin_dir_path( $this->file ).ltrim( trim( $file ), '/' );
315
	}
316
317
	/**
318
	 * @param string $path
319
	 * @return string
320
	 */
321
	protected function url( $path = '' )
322
	{
323
		return esc_url( plugin_dir_url( $this->file ).ltrim( trim( $path ), '/' ));
324
	}
325
}
326