Client   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 50
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 3
A sendRequest() 0 24 3
1
<?php
2
3
namespace Http\Adapter\Cake;
4
5
use Cake\Http\Client as CakeClient;
6
use Cake\Http\Client\Request;
7
use Http\Client\Exception\NetworkException;
8
use Http\Client\HttpClient;
9
use Http\Discovery\MessageFactoryDiscovery;
10
use Http\Message\ResponseFactory;
11
use Psr\Http\Message\RequestInterface;
12
use Psr\Http\Message\ResponseInterface;
13
use Throwable;
14
15
/**
16
 * Client compatible with PSR7 and Httplug interfaces, using a CakePHP client.
17
 */
18
class Client implements HttpClient
19
{
20
    /**
21
     * @var \Cake\Http\Client
22
     */
23
    private $client;
24
25
    /**
26
     * @var \Http\Message\ResponseFactory
27
     */
28
    private $responseFactory;
29 53
30
    /**
31 53
     * @param \Cake\Http\Client|null $client
32 53
     * @param \Http\Message\ResponseFactory|null $responseFactory
33 53
     */
34
    public function __construct(CakeClient $client = null, ResponseFactory $responseFactory = null)
35
    {
36
        $this->client = $client ?: new CakeClient();
37
        $this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find();
0 ignored issues
show
Documentation Bug introduced by
It seems like $responseFactory ?: \Htt...actoryDiscovery::find() can also be of type object<Http\Message\MessageFactory>. However, the property $responseFactory is declared as type object<Http\Message\ResponseFactory>. 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...
38 53
    }
39
40 53
    /**
41 53
     * @inheritdoc
42 53
     */
43 53
    public function sendRequest(RequestInterface $request): ResponseInterface
44 53
    {
45
        $cakeRequest = new Request(
46
            (string) $request->getUri(),
47 53
            $request->getMethod(),
48 53
            $request->getHeaders()
49
        );
50 53
51 53
        $cakeRequest = $cakeRequest
52 53
            ->withProtocolVersion($request->getProtocolVersion())
53
            ->withBody($request->getBody());
54
55 53
        if (!$cakeRequest->getHeader('Content-Type')) {
56 53
            $cakeRequest = $cakeRequest->withHeader('Content-Type', 'application/x-www-form-urlencoded');
57 1
        }
58
59
        try {
60 52
            $response = $this->client->send($cakeRequest, $this->client->getConfig());
61
        } catch (Throwable $exception) {
62
            throw new NetworkException('Failed to send request', $request, $exception);
0 ignored issues
show
Documentation introduced by
$exception is of type object<Throwable>, but the function expects a null|object<Exception>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
63
        }
64
65
        return $response;
66
    }
67
}
68