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

Plugin::skipNoticesAndWarnings()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 4
nc 5
nop 1
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
		// Plugins to watch for Notices and Warnings. If blank, then watch for
99
		// notices and warnings of all plugins.
100
		$defaults['watch_specific_plugins'] = [];
101
102
		// Themes to watch for Notices and Warnings. If blank, then watch for
103
		// notices and warnings of whichever theme is active.
104
		$defaults['watch_specific_themes'] = [];
105
106
		$defaults['run'] = function ( $plugin ) {
107
			$run = new Whoops_Run_Composite( $plugin['whoops_system_facade'] );
108
109
			if( true === $plugin['skip_all_notices_and_warnings'] ) {
110
				$run->skipAllNoticesAndWarnings();
111
			}
112
			$run->watchSpecificPlugins( $plugin['watch_specific_plugins'] );
113
			$run->watchSpecificThemes( $plugin['watch_specific_themes'] );
114
115
			$run->pushHandler( $plugin['handler.pretty'] );
116
			$run->pushHandler( $plugin['handler.json'] );
117
			$run->pushHandler( $plugin['handler.rest'] );
118
119
			if ( \Whoops\Util\Misc::isCommandLine() ) {
120
				$run->pushHandler( $plugin['handler.text'] );
121
			}
122
123
			return $run;
124
		};
125
126
		parent::__construct( array_merge( $defaults, $values ) );
127
	}
128
129
	/**
130
	 * @return bool
131
	 */
132
	public function is_debug() {
133
134
		return defined( 'WP_DEBUG' ) && WP_DEBUG;
135
	}
136
137
	/**
138
	 * @return bool
139
	 */
140
	public function is_debug_display() {
141
142
		return defined( 'WP_DEBUG_DISPLAY' ) && false !== WP_DEBUG_DISPLAY;
143
	}
144
145
	/**
146
	 * Skip Notices and Warnings occurring while program execution
147
	 *
148
	 * @param Except $except Plugins & Themes to be excepted from this privilege.
149
	 * @return void
150
	 */
151
	public function skipNoticesAndWarnings(Except $except) {
152
		if( $except->empty() ) {
153
			$this['skip_all_notices_and_warnings'] = true;
154
			return;
155
		}
156
157
		if( ! $except->emptyPlugins() ) {
158
			$this['watch_specific_plugins'] = $except->pluginsDirectories;
159
		}
160
161
		if( ! $except->emptyThemes() ) {
162
			$this['watch_specific_themes'] = $except->themesDirectories;
163
		}
164
	}
165
166
	/**
167
	 * Execute run conditionally on debug configuration.
168
	 */
169
	public function run() {
170
171
		if ( ! $this->is_debug() || ! $this->is_debug_display() ) {
172
			return;
173
		}
174
175
		/** @var Run $run */
176
		$run = $this['run'];
177
		$run->register();
178
		ob_start(); // Or we are going to be spitting out WP markup before whoops.
179
	}
180
}
181