Completed
Push — master ( 979ecb...d86fa2 )
by smiley
01:38
created

HTTPRequestTrait::request()   B

Complexity

Conditions 9
Paths 16

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 16
nop 5
dl 0
loc 31
rs 8.0555
c 0
b 0
f 0
1
<?php
2
/**
3
 * Trait HTTPRequestTrait
4
 *
5
 * @filesource   HTTPRequestTrait.php
6
 * @created      02.09.2018
7
 * @package      chillerlan\HTTP
8
 * @author       smiley <[email protected]>
9
 * @copyright    2018 smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\HTTP;
14
15
use Psr\Http\Message\ResponseInterface;
16
17
/**
18
 * @property \Psr\Http\Message\RequestFactoryInterface $requestFactory
19
 * @property \Psr\Http\Message\StreamFactoryInterface  $streamFactory
20
 * @method   sendRequest(\Psr\Http\Message\RequestInterface $request):\Psr\Http\Message\ResponseInterface
21
 */
22
trait HTTPRequestTrait{
23
24
	/**
25
	 * @todo: files, content-type
26
	 *
27
	 * @param string      $uri
28
	 * @param string|null $method
29
	 * @param array|null  $query
30
	 * @param mixed|null  $body
31
	 * @param array|null  $headers
32
	 *
33
	 * @return \Psr\Http\Message\ResponseInterface
34
	 */
35
	public function request(string $uri, string $method = null, array $query = null, $body = null, array $headers = null):ResponseInterface{
36
		$method    = strtoupper($method ?? 'GET');
37
		$headers   = Psr7\normalize_request_headers($headers);
0 ignored issues
show
Bug introduced by
It seems like $headers can also be of type null; however, chillerlan\HTTP\Psr7\normalize_request_headers() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
38
		$request   = $this->requestFactory->createRequest($method, Psr7\merge_query($uri, $query ?? []));
39
40
		if(in_array($method, ['DELETE', 'PATCH', 'POST', 'PUT'], true) && $body !== null){
41
42
			if(is_array($body) || is_object($body)){
43
44
				if(!isset($headers['Content-type'])){
45
					$headers['Content-type'] = 'application/x-www-form-urlencoded';
46
				}
47
48
				if($headers['Content-type'] === 'application/x-www-form-urlencoded'){
49
					$body = http_build_query($body, '', '&', PHP_QUERY_RFC1738);
50
				}
51
				elseif($headers['Content-type'] === 'application/json'){
52
					$body = json_encode($body);
53
				}
54
55
			}
56
57
			$request = $request->withBody($this->streamFactory->createStream((string)$body));
58
		}
59
60
		foreach($headers as $header => $value){
61
			$request = $request->withAddedHeader($header, $value);
62
		}
63
64
		return $this->sendRequest($request);
65
	}
66
67
}
68