Completed
Pull Request — master (#13)
by
unknown
01:15
created

Plugin::is_debug_display()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
namespace Rarst\wps;
3
4
use Pimple\Container;
5
use Whoops\Handler\PlainTextHandler;
6
use Whoops\Handler\PrettyPageHandler;
7
use Whoops\Run;
8
use Whoops\Util\SystemFacade;
9
10
/**
11
 * Main plugin's class.
12
 */
13
class Plugin extends Container {
14
15
	/**
16
	 * @param array $values Optional arguments for container.
17
	 */
18
	public function __construct( $values = array() ) {
19
20
		$defaults = array();
21
22
		$defaults['tables'] = array(
23
			'$wp'       => function () {
24
				global $wp;
25
26
				if ( ! $wp instanceof \WP ) {
0 ignored issues
show
Bug introduced by
The class WP does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
27
					return array();
28
				}
29
30
				$output = get_object_vars( $wp );
31
				unset( $output['private_query_vars'] );
32
				unset( $output['public_query_vars'] );
33
34
				return array_filter( $output );
35
			},
36
			'$wp_query' => function () {
37
				global $wp_query;
38
39
				if ( ! $wp_query instanceof \WP_Query ) {
0 ignored issues
show
Bug introduced by
The class WP_Query does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
40
					return array();
41
				}
42
43
				$output               = get_object_vars( $wp_query );
44
				$output['query_vars'] = array_filter( $output['query_vars'] );
45
				unset( $output['posts'] );
46
				unset( $output['post'] );
47
48
				return array_filter( $output );
49
			},
50
			'$post'     => function () {
51
				$post = get_post();
52
53
				if ( ! $post instanceof \WP_Post ) {
0 ignored issues
show
Bug introduced by
The class WP_Post does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
54
					return array();
55
				}
56
57
				return get_object_vars( $post );
58
			},
59
		);
60
61
		$defaults['handler.pretty'] = function ( $plugin ) {
62
			$handler = new PrettyPageHandler();
63
64
			foreach ( $plugin['tables'] as $name => $callback ) {
65
				$handler->addDataTableCallback( $name, $callback );
66
			}
67
68
			// Requires Remote Call plugin.
69
			$handler->addEditor( 'phpstorm-remote-call', 'http://localhost:8091?message=%file:%line' );
70
71
			return $handler;
72
		};
73
74
		$defaults['handler.json'] = function () {
75
			$handler = new Admin_Ajax_Handler();
76
			$handler->addTraceToOutput( true );
77
78
			return $handler;
79
		};
80
81
		$defaults['handler.rest'] = function () {
82
			$handler = new Rest_Api_Handler();
83
			$handler->addTraceToOutput( true );
84
85
			return $handler;
86
		};
87
88
		$defaults['handler.text'] = function () {
89
			return new PlainTextHandler();
90
		};
91
92
		$defaults['whoops_system_facade'] = function() {
93
			return new SystemFacade;
94
		};
95
96
		$defaults['skip_all_notices_and_warnings'] = false;
97
98
		// Files to watch for Notices and Warnings.
99
		$defaults['watch_files'] = null;
100
101
		$this['silence_errors_in_paths.pattern'] = null;
102
		$this['silence_errors_in_paths.levels'] = 10240; // E_STRICT | E_DEPRECATED.
103
104
		$defaults['run'] = function ( $plugin ) {
105
			$run = new Whoops_Run_Composite( $plugin['whoops_system_facade'] );
106
107
			if( true === $plugin['skip_all_notices_and_warnings'] ) {
108
				$run->skipAllNoticesAndWarnings();
109
			}
110
111
			if( ! is_null( $plugin['watch_files'] ) ) {
112
				$run->watchFilesWithPatterns( $plugin['watch_files'] );
113
			}
114
115
			if( ! is_null( $plugin['silence_errors_in_paths.pattern'] ) ) {
116
				$run->silenceErrorsInPaths(
117
					$plugin['silence_errors_in_paths.pattern'],
118
					$plugin['silence_errors_in_paths.levels']
119
				);
120
			}
121
122
			$run->pushHandler( $plugin['handler.pretty'] );
123
			$run->pushHandler( $plugin['handler.json'] );
124
			$run->pushHandler( $plugin['handler.rest'] );
125
126
			if ( \Whoops\Util\Misc::isCommandLine() ) {
127
				$run->pushHandler( $plugin['handler.text'] );
128
			}
129
130
			return $run;
131
		};
132
133
		parent::__construct( array_merge( $defaults, $values ) );
134
	}
135
136
	/**
137
	 * @return bool
138
	 */
139
	public function is_debug() {
140
141
		return defined( 'WP_DEBUG' ) && WP_DEBUG;
142
	}
143
144
	/**
145
	 * @return bool
146
	 */
147
	public function is_debug_display() {
148
149
		return defined( 'WP_DEBUG_DISPLAY' ) && false !== WP_DEBUG_DISPLAY;
150
	}
151
152
	/**
153
	 * Skip Notices and Warnings occurring while program execution
154
	 *
155
	 * @param Except $except Directories to be excepted from this privilege.
156
	 * @return void
157
	 */
158
	public function skipNoticesAndWarnings(Except $except) {
159
		if( $except->empty() ) {
160
			$this['skip_all_notices_and_warnings'] = true;
161
			return;
162
		}
163
164
		$this['watch_files'] = $except->pathPatterns();
165
	}
166
167
	/**
168
     * Silence particular errors in particular files
169
     * @param  array|string $patterns List or a single regex pattern to match
170
     * @param  int          $levels   Defaults to E_STRICT | E_DEPRECATED
171
     * @return \Whoops\Run
172
     */
173
    public function silenceErrorsInPaths($patterns, $levels = 10240) {
174
		$this['silence_errors_in_paths.pattern'] = $patterns;
175
		$this['silence_errors_in_paths.levels'] = $levels;
176
	}
177
	
178
	/**
179
	 * Execute run conditionally on debug configuration.
180
	 */
181
	public function run() {
182
183
		if ( ! $this->is_debug() || ! $this->is_debug_display() ) {
184
			add_action('admin_notices', function(){
185
				echo '<div class="notice notice-warning is-dismissible">
186
						<p><strong>wps</strong> plugin works only when <code>WP_DEBUG</code> & <code>WP_DEBUG_DISPLAY</code> constants are set to <code>true</code></p>
187
					</div>';
188
			});
189
			return;
190
		}
191
192
		/** @var Run $run */
193
		$run = $this['run'];
194
		$run->register();
195
		ob_start(); // Or we are going to be spitting out WP markup before whoops.
196
	}
197
}
198