Completed
Pull Request — develop (#5)
by Adam
01:26
created

RequestBuilder::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 4
1
<?php
2
3
namespace IBM\Watson\Common;
4
5
use Http\Discovery\MessageFactoryDiscovery;
6
use Http\Message\RequestFactory;
7
use Psr\Http\Message\StreamInterface;
8
use Psr\Http\Message\UriInterface;
9
10
final class RequestBuilder
11
{
12
    /**
13
     * @var \Http\Message\MessageFactory
14
     */
15
    private $requestFactory;
16
17
    /**
18
     * RequestBuilder constructor.
19
     *
20
     * @param \Http\Message\RequestFactory|null $requestFactory
21
     *
22
     * @throws \Http\Discovery\Exception\NotFoundException
23
     */
24
    public function __construct(RequestFactory $requestFactory = null)
25
    {
26
        $this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find();
0 ignored issues
show
Documentation Bug introduced by
It seems like $requestFactory ?: \Http...actoryDiscovery::find() can also be of type object<Http\Message\RequestFactory>. However, the property $requestFactory is declared as type object<Http\Message\MessageFactory>. 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...
27
    }
28
29
    /**
30
     * Create a new PSR-7 request
31
     *
32
     * @param string                               $method
33
     * @param string|UriInterface                  $uri
34
     * @param array|null                           $headers
35
     * @param resource|string|StreamInterface|null $body
36
     *
37
     * @return \Psr\Http\Message\RequestInterface
38
     */
39
    public function create($method, $uri, array $headers = null, $body = null)
40
    {
41
        return $this->requestFactory->createRequest($method, $uri, $headers, $body);
0 ignored issues
show
Bug introduced by
It seems like $headers defined by parameter $headers on line 39 can also be of type null; however, Http\Message\RequestFactory::createRequest() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and 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...
42
    }
43
}
44