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 0
Metric Value
cc 1
eloc 9
c 0
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
	 * @see \Requests_Utility_CaseInsensitiveDictionary
69
	 *
70
	 * @return \Requests_Utility_CaseInsensitiveDictionary Map of header name to header value.
71
	 */
72
	public function get_headers() {
73
		// Ensure headers remain case-insensitive.
74
		$converted = new Requests_Utility_CaseInsensitiveDictionary();
75
76
		foreach ( $this->response->headers->getAll() as $key => $value ) {
77
			if ( count( $value ) === 1 ) {
78
				$converted[ $key ] = $value[0];
79
			} else {
80
				$converted[ $key ] = $value;
81
			}
82
		}
83
84
		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...
85
	}
86
87
	/**
88
	 * Sets all header values.
89
	 *
90
	 * @since 4.6.0
91
	 * @access public
92
	 *
93
	 * @param array $headers Map of header name to header value.
94
	 */
95
	public function set_headers( $headers ) {
96
		$this->response->headers = new Requests_Response_Headers( $headers );
97
	}
98
99
	/**
100
	 * Sets a single HTTP header.
101
	 *
102
	 * @since 4.6.0
103
	 * @access public
104
	 *
105
	 * @param string $key     Header name.
106
	 * @param string $value   Header value.
107
	 * @param bool   $replace Optional. Whether to replace an existing header of the same name.
108
	 *                        Default true.
109
	 */
110
	public function header( $key, $value, $replace = true ) {
111
		if ( $replace ) {
112
			unset( $this->response->headers[ $key ] );
113
		}
114
115
		$this->response->headers[ $key ] = $value;
116
	}
117
118
	/**
119
	 * Retrieves the HTTP return code for the response.
120
	 *
121
	 * @since 4.6.0
122
	 * @access public
123
	 *
124
	 * @return int The 3-digit HTTP status code.
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
125
	 */
126
	public function get_status() {
127
		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 127 which is incompatible with the return type of the parent method WP_HTTP_Response::get_status of type integer.
Loading history...
128
	}
129
130
	/**
131
	 * Sets the 3-digit HTTP status code.
132
	 *
133
	 * @since 4.6.0
134
	 * @access public
135
	 *
136
	 * @param int $code HTTP status.
137
	 */
138
	public function set_status( $code ) {
139
		$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...
140
	}
141
142
	/**
143
	 * Retrieves the response data.
144
	 *
145
	 * @since 4.6.0
146
	 * @access public
147
	 *
148
	 * @return mixed Response data.
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
149
	 */
150
	public function get_data() {
151
		return $this->response->body;
152
	}
153
154
	/**
155
	 * Sets the response data.
156
	 *
157
	 * @since 4.6.0
158
	 * @access public
159
	 *
160
	 * @param mixed $data Response data.
161
	 */
162
	public function set_data( $data ) {
163
		$this->response->body = $data;
164
	}
165
166
	/**
167
	 * Retrieves cookies from the response.
168
	 *
169
	 * @since 4.6.0
170
	 * @access public
171
	 *
172
	 * @return WP_HTTP_Cookie[] List of cookie objects.
173
	 */
174
	public function get_cookies() {
175
		$cookies = array();
176
		foreach ( $this->response->cookies as $cookie ) {
177
			$cookies[] = new WP_Http_Cookie( array(
178
				'name'    => $cookie->name,
179
				'value'   => urldecode( $cookie->value ),
180
				'expires' => isset( $cookie->attributes['expires'] ) ? $cookie->attributes['expires'] : null,
181
				'path'    => isset( $cookie->attributes['path'] ) ? $cookie->attributes['path'] : null,
182
				'domain'  => isset( $cookie->attributes['domain'] ) ? $cookie->attributes['domain'] : null,
183
			));
184
		}
185
186
		return $cookies;
187
	}
188
189
	/**
190
	 * Converts the object to a WP_Http response array.
191
	 *
192
	 * @since 4.6.0
193
	 * @access public
194
	 *
195
	 * @return array WP_Http response array, per WP_Http::request().
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,Requests_Ut...|WP_Http_Cookie[]|null>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
196
	 */
197
	public function to_array() {
198
		return array(
199
			'headers' => $this->get_headers(),
200
			'body' => $this->get_data(),
201
			'response' => array(
202
				'code'    => $this->get_status(),
203
				'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...
204
			),
205
			'cookies' => $this->get_cookies(),
206
			'filename' => $this->filename,
207
		);
208
	}
209
}
210