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

Plugin::__construct()   C

Complexity

Conditions 8
Paths 1

Size

Total Lines 120

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 120
rs 6.7555
c 0
b 0
f 0
cc 8
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
		$this['silence_errors_in_paths.pattern'] = null;
107
		$this['silence_errors_in_paths.levels'] = 10240; // E_STRICT | E_DEPRECATED.
108
109
		$defaults['run'] = function ( $plugin ) {
110
			$run = new Whoops_Run_Composite( $plugin['whoops_system_facade'] );
111
112
			if( true === $plugin['skip_all_notices_and_warnings'] ) {
113
				$run->skipAllNoticesAndWarnings();
114
			}
115
			$run->watchSpecificPlugins( $plugin['watch_specific_plugins'] );
116
			$run->watchSpecificThemes( $plugin['watch_specific_themes'] );
117
118
			if( ! is_null( $plugin['silence_errors_in_paths.pattern'] ) ) {
119
				$run->silenceErrorsInPaths(
120
					$plugin['silence_errors_in_paths.pattern'],
121
					$plugin['silence_errors_in_paths.levels']
122
				);
123
			}
124
125
			$run->pushHandler( $plugin['handler.pretty'] );
126
			$run->pushHandler( $plugin['handler.json'] );
127
			$run->pushHandler( $plugin['handler.rest'] );
128
129
			if ( \Whoops\Util\Misc::isCommandLine() ) {
130
				$run->pushHandler( $plugin['handler.text'] );
131
			}
132
133
			return $run;
134
		};
135
136
		parent::__construct( array_merge( $defaults, $values ) );
137
	}
138
139
	/**
140
	 * @return bool
141
	 */
142
	public function is_debug() {
143
144
		return defined( 'WP_DEBUG' ) && WP_DEBUG;
145
	}
146
147
	/**
148
	 * @return bool
149
	 */
150
	public function is_debug_display() {
151
152
		return defined( 'WP_DEBUG_DISPLAY' ) && false !== WP_DEBUG_DISPLAY;
153
	}
154
155
	/**
156
	 * Skip Notices and Warnings occurring while program execution
157
	 *
158
	 * @param Except $except Plugins & Themes to be excepted from this privilege.
159
	 * @return void
160
	 */
161
	public function skipNoticesAndWarnings(Except $except) {
162
		if( $except->empty() ) {
163
			$this['skip_all_notices_and_warnings'] = true;
164
			return;
165
		}
166
167
		if( ! $except->emptyPlugins() ) {
168
			$this['watch_specific_plugins'] = $except->pluginsDirectories;
169
		}
170
171
		if( ! $except->emptyThemes() ) {
172
			$this['watch_specific_themes'] = $except->themesDirectories;
173
		}
174
	}
175
176
	/**
177
     * Silence particular errors in particular files
178
     * @param  array|string $patterns List or a single regex pattern to match
179
     * @param  int          $levels   Defaults to E_STRICT | E_DEPRECATED
180
     * @return \Whoops\Run
181
     */
182
    public function silenceErrorsInPaths($patterns, $levels = 10240) {
183
		$this['silence_errors_in_paths.pattern'] = $patterns;
184
		$this['silence_errors_in_paths.levels'] = $levels;
185
	}
186
	
187
	/**
188
	 * Execute run conditionally on debug configuration.
189
	 */
190
	public function run() {
191
192
		if ( ! $this->is_debug() || ! $this->is_debug_display() ) {
193
			return;
194
		}
195
196
		/** @var Run $run */
197
		$run = $this['run'];
198
		$run->register();
199
		ob_start(); // Or we are going to be spitting out WP markup before whoops.
200
	}
201
}
202