WP_HTTP_Response::get_data()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * HTTP API: WP_HTTP_Response class
4
 *
5
 * @package WordPress
6
 * @subpackage HTTP
7
 * @since 4.4.0
8
 */
9
10
/**
11
 * Core class used to prepare HTTP responses.
12
 *
13
 * @since 4.4.0
14
 */
15
class WP_HTTP_Response {
16
17
	/**
18
	 * Response data.
19
	 *
20
	 * @since 4.4.0
21
	 * @access public
22
	 * @var mixed
23
	 */
24
	public $data;
25
26
	/**
27
	 * Response headers.
28
	 *
29
	 * @since 4.4.0
30
	 * @access public
31
	 * @var array
32
	 */
33
	public $headers;
34
35
	/**
36
	 * Response status.
37
	 *
38
	 * @since 4.4.0
39
	 * @access public
40
	 * @var int
41
	 */
42
	public $status;
43
44
	/**
45
	 * Constructor.
46
	 *
47
	 * @since 4.4.0
48
	 * @access public
49
	 *
50
	 * @param mixed $data    Response data. Default null.
51
	 * @param int   $status  Optional. HTTP status code. Default 200.
52
	 * @param array $headers Optional. HTTP header map. Default empty array.
53
	 */
54
	public function __construct( $data = null, $status = 200, $headers = array() ) {
55
		$this->data = $data;
56
		$this->set_status( $status );
57
		$this->set_headers( $headers );
58
	}
59
60
	/**
61
	 * Retrieves headers associated with the response.
62
	 *
63
	 * @since 4.4.0
64
	 * @access public
65
	 *
66
	 * @return array Map of header name to header value.
67
	 */
68
	public function get_headers() {
69
		return $this->headers;
70
	}
71
72
	/**
73
	 * Sets all header values.
74
	 *
75
	 * @since 4.4.0
76
	 * @access public
77
	 *
78
	 * @param array $headers Map of header name to header value.
79
	 */
80
	public function set_headers( $headers ) {
81
		$this->headers = $headers;
82
	}
83
84
	/**
85
	 * Sets a single HTTP header.
86
	 *
87
	 * @since 4.4.0
88
	 * @access public
89
	 *
90
	 * @param string $key     Header name.
91
	 * @param string $value   Header value.
92
	 * @param bool   $replace Optional. Whether to replace an existing header of the same name.
93
	 *                        Default true.
94
	 */
95
	public function header( $key, $value, $replace = true ) {
96
		if ( $replace || ! isset( $this->headers[ $key ] ) ) {
97
			$this->headers[ $key ] = $value;
98
		} else {
99
			$this->headers[ $key ] .= ', ' . $value;
100
		}
101
	}
102
103
	/**
104
	 * Retrieves the HTTP return code for the response.
105
	 *
106
	 * @since 4.4.0
107
	 * @access public
108
	 *
109
	 * @return int The 3-digit HTTP status code.
110
	 */
111
	public function get_status() {
112
		return $this->status;
113
	}
114
115
	/**
116
	 * Sets the 3-digit HTTP status code.
117
	 *
118
	 * @since 4.4.0
119
	 * @access public
120
	 *
121
	 * @param int $code HTTP status.
122
	 */
123
	public function set_status( $code ) {
124
		$this->status = absint( $code );
0 ignored issues
show
Documentation Bug introduced by
It seems like absint($code) can also be of type double. However, the property $status is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
125
	}
126
127
	/**
128
	 * Retrieves the response data.
129
	 *
130
	 * @since 4.4.0
131
	 * @access public
132
	 *
133
	 * @return mixed Response data.
134
	 */
135
	public function get_data() {
136
		return $this->data;
137
	}
138
139
	/**
140
	 * Sets the response data.
141
	 *
142
	 * @since 4.4.0
143
	 * @access public
144
	 *
145
	 * @param mixed $data Response data.
146
	 */
147
	public function set_data( $data ) {
148
		$this->data = $data;
149
	}
150
151
	/**
152
	 * Retrieves the response data for JSON serialization.
153
	 *
154
	 * It is expected that in most implementations, this will return the same as get_data(),
155
	 * however this may be different if you want to do custom JSON data handling.
156
	 *
157
	 * @since 4.4.0
158
	 * @access public
159
	 *
160
	 * @return mixed Any JSON-serializable value.
161
	 */
162
	public function jsonSerialize() {
163
		return $this->get_data();
164
	}
165
}
166