Admin_Ajax_Handler::handle()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
namespace Rarst\wps;
3
4
use Whoops\Exception\Formatter;
5
use Whoops\Handler\Handler;
6
use Whoops\Handler\JsonResponseHandler;
7
use Whoops\Util\Misc;
8
9
/**
10
 * WordPress-specific version of Json handler.
11
 */
12
class Admin_Ajax_Handler extends JsonResponseHandler {
13
14
	/**
15
	 * @return bool
16
	 */
17
	private function isAjaxRequest() {
18
19
		return defined( 'DOING_AJAX' ) && DOING_AJAX;
20
	}
21
22
	/**
23
	 * @return int
24
	 */
25
	public function handle() {
26
27
		if ( ! $this->isAjaxRequest() ) {
28
			return Handler::DONE;
29
		}
30
31
		$response = array(
32
			'success' => false,
33
			'data'    => Formatter::formatExceptionAsDataArray( $this->getInspector(), $this->addTraceToOutput() ),
0 ignored issues
show
Bug introduced by
It seems like $this->addTraceToOutput() targeting Whoops\Handler\JsonRespo...ler::addTraceToOutput() can also be of type this<Rarst\wps\Admin_Ajax_Handler>; however, Whoops\Exception\Formatt...tExceptionAsDataArray() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
34
		);
35
36
		if ( Misc::canSendHeaders() ) {
37
			header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
38
		}
39
40
		echo wp_json_encode( $response, JSON_PRETTY_PRINT );
41
42
		return Handler::QUIT;
43
	}
44
}
45