WP_HTTP_Requests_Response::to_array()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 9.4285
1
<?php
2
/**
3
 * HTTP API: WP_HTTP_Requests_Response class
4
 *
5
 * @package WordPress
6
 * @subpackage HTTP
7
 * @since 4.6.0
8
 */
9
10
/**
11
 * Core wrapper object for a Requests_Response for standardisation.
12
 *
13
 * @since 4.6.0
14
 *
15
 * @see WP_HTTP_Response
16
 */
17
class WP_HTTP_Requests_Response extends WP_HTTP_Response {
18
	/**
19
	 * Requests Response object.
20
	 *
21
	 * @since 4.6.0
22
	 * @access protected
23
	 * @var Requests_Response
24
	 */
25
	protected $response;
26
27
	/**
28
	 * Filename the response was saved to.
29
	 *
30
	 * @since 4.6.0
31
	 * @access protected
32
	 * @var string|null
33
	 */
34
	protected $filename;
35
36
	/**
37
	 * Constructor.
38
	 *
39
	 * @since 4.6.0
40
	 * @access public
41
	 *
42
	 * @param Requests_Response $response HTTP response.
43
	 * @param string            $filename Optional. File name. Default empty.
44
	 */
45
	public function __construct( Requests_Response $response, $filename = '' ) {
46
		$this->response = $response;
47
		$this->filename = $filename;
48
	}
49
50
	/**
51
	 * Retrieves the response object for the request.
52
	 *
53
	 * @since 4.6.0
54
	 * @access public
55
	 *
56
	 * @return Requests_Response HTTP response.
57
	 */
58
	public function get_response_object() {
59
		return $this->response;
60
	}
61
62
	/**
63
	 * Retrieves headers associated with the response.
64
	 *
65
	 * @since 4.6.0
66
	 * @access public
67
	 *
68
	 * @return array Map of header name to header value.
69
	 */
70
	public function get_headers() {
71
		// Ensure headers remain case-insensitive
72
		$converted = new Requests_Utility_CaseInsensitiveDictionary();
73
74
		foreach ( $this->response->headers->getAll() as $key => $value ) {
75
			if ( count( $value ) === 1 ) {
76
				$converted[ $key ] = $value[0];
77
			}
78
			else {
79
				$converted[ $key ] = $value;
80
			}
81
		}
82
83
		return $converted;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $converted; (Requests_Utility_CaseInsensitiveDictionary) is incompatible with the return type of the parent method WP_HTTP_Response::get_headers of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
84
	}
85
86
	/**
87
	 * Sets all header values.
88
	 *
89
	 * @since 4.6.0
90
	 * @access public
91
	 *
92
	 * @param array $headers Map of header name to header value.
93
	 */
94
	public function set_headers( $headers ) {
95
		$this->response->headers = new Requests_Response_Headers( $headers );
96
	}
97
98
	/**
99
	 * Sets a single HTTP header.
100
	 *
101
	 * @since 4.6.0
102
	 * @access public
103
	 *
104
	 * @param string $key     Header name.
105
	 * @param string $value   Header value.
106
	 * @param bool   $replace Optional. Whether to replace an existing header of the same name.
107
	 *                        Default true.
108
	 */
109
	public function header( $key, $value, $replace = true ) {
110
		if ( $replace ) {
111
			unset( $this->response->headers[ $key ] );
112
		}
113
114
		$this->response->headers[ $key ] = $value;
115
	}
116
117
	/**
118
	 * Retrieves the HTTP return code for the response.
119
	 *
120
	 * @since 4.6.0
121
	 * @access public
122
	 *
123
	 * @return int The 3-digit HTTP status code.
124
	 */
125
	public function get_status() {
126
		return $this->response->status_code;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->response->status_code; of type integer|boolean adds the type boolean to the return on line 126 which is incompatible with the return type of the parent method WP_HTTP_Response::get_status of type integer.
Loading history...
127
	}
128
129
	/**
130
	 * Sets the 3-digit HTTP status code.
131
	 *
132
	 * @since 4.6.0
133
	 * @access public
134
	 *
135
	 * @param int $code HTTP status.
136
	 */
137
	public function set_status( $code ) {
138
		$this->response->status_code = 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_code is declared as type integer|boolean. 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...
139
	}
140
141
	/**
142
	 * Retrieves the response data.
143
	 *
144
	 * @since 4.6.0
145
	 * @access public
146
	 *
147
	 * @return mixed Response data.
148
	 */
149
	public function get_data() {
150
		return $this->response->body;
151
	}
152
153
	/**
154
	 * Sets the response data.
155
	 *
156
	 * @since 4.6.0
157
	 * @access public
158
	 *
159
	 * @param mixed $data Response data.
160
	 */
161
	public function set_data( $data ) {
162
		$this->response->body = $data;
163
	}
164
165
	/**
166
	 * Retrieves cookies from the response.
167
	 *
168
	 * @since 4.6.0
169
	 * @access public
170
	 *
171
	 * @return WP_HTTP_Cookie[] List of cookie objects.
172
	 */
173
	public function get_cookies() {
174
		$cookies = array();
175
		foreach ( $this->response->cookies as $cookie ) {
176
			$cookies[] = new WP_Http_Cookie( array(
177
				'name'    => $cookie->name,
178
				'value'   => urldecode( $cookie->value ),
179
				'expires' => isset( $cookie->attributes['expires'] ) ? $cookie->attributes['expires'] : null,
180
				'path'    => isset( $cookie->attributes['path'] ) ? $cookie->attributes['path'] : null,
181
				'domain'  => isset( $cookie->attributes['domain'] ) ? $cookie->attributes['domain'] : null,
182
			));
183
		}
184
185
		return $cookies;
186
	}
187
188
	/**
189
	 * Converts the object to a WP_Http response array.
190
	 *
191
	 * @since 4.6.0
192
	 * @access public
193
	 *
194
	 * @return array WP_Http response array, per WP_Http::request().
195
	 */
196
	public function to_array() {
197
		return array(
198
			'headers' => $this->get_headers(),
199
			'body' => $this->get_data(),
200
			'response' => array(
201
				'code'    => $this->get_status(),
202
				'message' => get_status_header_desc( $this->get_status() ),
0 ignored issues
show
Bug introduced by
It seems like $this->get_status() targeting WP_HTTP_Requests_Response::get_status() can also be of type boolean; however, get_status_header_desc() does only seem to accept integer, 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...
203
			),
204
			'cookies' => $this->get_cookies(),
205
			'filename' => $this->filename,
206
		);
207
	}
208
}
209