Completed
Push — master ( 4c96a9...8879ed )
by Sergey
13:46 queued 10:03
created

GuzzleHttpAdater::parseResponse()   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 1
1
<?php
2
3
namespace seregazhuk\HeadHunterApi\Adapters;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Psr7\Request;
7
use GuzzleHttp\Psr7\Response;
8
use seregazhuk\HeadHunterApi\Contracts\HttpInterface;
9
10
class GuzzleHttpAdater implements HttpInterface
11
{
12
    /**
13
     * @var Client
14
     */
15
    protected $client;
16
17
    public function __construct($baseUrl)
18
    {
19
        $this->client = new Client(['base_uri' => $baseUrl]);
20
    }
21
22
    /**
23
     * @param string $uri
24
     * @param array $params
25
     * @param null $headers
26
     * @return array|null
27
     */
28
    public function get($uri, $params = [], $headers = null)
29
    {
30
        if(!empty($params)){
31
            $uri .= '?'. http_build_query($params);
32
        }
33
34
        $request = new Request('GET', $uri, $headers);
0 ignored issues
show
Documentation introduced by
$headers is of type null, but the function expects a array.

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...
35
36
        $response = $this
37
            ->client
38
            ->send($request);
39
40
        return $this->parseResponse($response);
0 ignored issues
show
Compatibility introduced by
$response of type object<Psr\Http\Message\ResponseInterface> is not a sub-type of object<GuzzleHttp\Psr7\Response>. It seems like you assume a concrete implementation of the interface Psr\Http\Message\ResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
41
    }
42
43
    /**
44
     * @param string $uri
45
     * @param array $params
46
     * @param null $headers
47
     * @return array|null
48
     */
49
    public function post($uri, $params = [], $headers = null)
50
    {
51
        $request = new Request('POST', $uri, $headers);
0 ignored issues
show
Documentation introduced by
$headers is of type null, but the function expects a array.

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...
52
53
        $response = $this
54
            ->client
55
            ->send($request, ['query' => $params]);
56
57
        return $this->parseResponse($response);
0 ignored issues
show
Compatibility introduced by
$response of type object<Psr\Http\Message\ResponseInterface> is not a sub-type of object<GuzzleHttp\Psr7\Response>. It seems like you assume a concrete implementation of the interface Psr\Http\Message\ResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
58
    }
59
60
    /**
61
     * @param Response $response
62
     * @return array|null
63
     */
64
    private function parseResponse(Response $response)
65
    {
66
        return json_decode($response->getBody(), true);
67
    }
68
69
}