RequestBuilder::reply()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Muzzle;
4
5
use Exception;
6
use GuzzleHttp\Psr7\Request;
7
use GuzzleHttp\Psr7\Uri;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\StreamInterface;
10
use function GuzzleHttp\Psr7\build_query;
11
use function GuzzleHttp\Psr7\parse_query;
12
13
class RequestBuilder
14
{
15
16
    /**
17
     * @var HttpMethod
18
     */
19
    private $method;
20
    /**
21
     * @var Uri
22
     */
23
    private $uri;
24
    /**
25
     * @var array
26
     */
27
    private $headers;
28
    /**
29
     * @var string
30
     */
31
    private $body;
32
    /**
33
     * @var array
34
     */
35
    private $query = [];
36
    /**
37
     * @var ResponseBuilder|\Psr\Http\Message\ResponseInterface
38
     */
39
    private $reply;
40
41
42
    /**
43
     * @param HttpMethod|null $method                                      HttpStatus method
44
     * @param string|null $uri                                             URI
45
     * @param array $headers                                               Request headers
46
     * @param string|null|resource|\Psr\Http\Message\StreamInterface $body Request body
47
     */
48
    public function __construct(
49
        HttpMethod $method = null,
50
        ?string $uri = null,
51
        array $headers = [],
52
        $body = null
53
    ) {
54
55
        $this->setMethod($method ?: HttpMethod::GET());
56
        $this->setUri($uri);
57
        $this->setHeaders($headers);
58
        $this->setBody($body);
59
    }
60
61
    public function setMethod(HttpMethod $method) : RequestBuilder
62
    {
63
64
        $this->method = $method;
65
66
        return $this;
67
    }
68
69
    public function setUri(?string $uri) : RequestBuilder
70
    {
71
72
        $this->uri = new Uri($uri ?: '');
73
74
        return $this;
75
    }
76
77
    public function setHeaders(array $headers = []) : RequestBuilder
78
    {
79
80
        $this->headers = $headers;
81
82
        return $this;
83
    }
84
85
    /**
86
     * @param string|null|resource|StreamInterface $body
87
     * @return RequestBuilder
88
     */
89
    public function setBody($body) : RequestBuilder
90
    {
91
92
        $this->body = $body;
0 ignored issues
show
Documentation Bug introduced by
It seems like $body can also be of type resource. However, the property $body is declared as type string. 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...
93
94
        return $this;
95
    }
96
97
    public function setQuery(array $query = []) : RequestBuilder
98
    {
99
100
        $this->query = $query;
101
102
        return $this;
103
    }
104
105
    /**
106
     * @param ResponseBuilder|ResponseInterface|Exception $reply
107
     * @return RequestBuilder
108
     */
109
    public function replyWith($reply = null) : RequestBuilder
110
    {
111
112
        $this->reply = $reply ?: new ResponseBuilder;
0 ignored issues
show
Documentation Bug introduced by
It seems like $reply ?: new Muzzle\ResponseBuilder() can also be of type Exception. However, the property $reply is declared as type Muzzle\ResponseBuilder|P...ssage\ResponseInterface. 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...
113
114
        return $this;
115
    }
116
117
    /**
118
     * @return ResponseInterface|ResponseBuilder
119
     */
120
    public function reply()
121
    {
122
123
        return $this->reply ?: $this->replyWith()->reply();
124
    }
125
126
    public function build() : Request
127
    {
128
129
        $uri = $this->uri->withQuery(build_query(array_merge(parse_query($this->uri->getQuery()), $this->query)));
130
131
        return new Request($this->method->getValue(), $uri, $this->headers, $this->body);
132
    }
133
}
134