Rest_Api_Handler   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 45
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isRestRequest() 0 13 5
A handle() 0 22 3
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 for REST API.
11
 */
12
class Rest_Api_Handler extends JsonResponseHandler {
13
14
	/**
15
	 * @return bool
16
	 */
17
	private function isRestRequest() {
18
19
		if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
20
			return true;
21
		}
22
23
		// This is dirty, but no better way to detect before parse_request.
24
		if ( ! empty( $_SERVER['REQUEST_URI'] ) && false !== stripos( $_SERVER['REQUEST_URI'], rest_get_url_prefix() ) ) {
25
			return true;
26
		}
27
28
		return false;
29
	}
30
31
	/**
32
	 * @return int
33
	 */
34
	public function handle() {
35
36
		if ( ! $this->isRestRequest() ) {
37
			return Handler::DONE;
38
		}
39
40
		$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\Rest_Api_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...
41
		$response = array(
42
			'code'    => $data['type'],
43
			'message' => $data['message'],
44
			'data'    => $data,
45
		);
46
47
		if ( Misc::canSendHeaders() ) {
48
			status_header( 500 );
49
			header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
50
		}
51
52
		echo wp_json_encode( $response, JSON_PRETTY_PRINT );
53
54
		return Handler::QUIT;
55
	}
56
}
57