Completed
Pull Request — master (#1)
by Sergey
05:27
created

Request::post()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace seregazhuk\HeadHunterApi;
4
5
6
use seregazhuk\HeadHunterApi\Contracts\HttpInterface;
7
use seregazhuk\HeadHunterApi\Contracts\RequestInterface;
8
9
class Request implements RequestInterface
10
{
11
    const BASE_URL = 'https://api.hh.ru';
12
    private $client;
13
    private $token;
14
15
    public function __construct(HttpInterface $http, $token = null)
16
    {
17
        $http->setBaseUrl(self::BASE_URL);
18
        $this->client = $http;
19
        $this->token = $token;
20
    }
21
22
    /**
23
     * @param string $uri
24
     * @param array $params
25
     * @return array|null
26
     */
27
    public function get($uri, $params = [])
28
    {
29
        $headers = $this->createHeaders();
30
        return $this->client->get($uri, $params, $headers);
0 ignored issues
show
Bug introduced by
It seems like $headers defined by $this->createHeaders() on line 29 can also be of type array; however, seregazhuk\HeadHunterApi...ts\HttpInterface::get() does only seem to accept null, 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...
31
    }
32
33
    /**
34
     * @return array|null
35
     */
36
    protected function createHeaders()
37
    {
38
        $headers = null;
39
        if(isset($this->token)) $headers['Authorization'] = 'Bearer ' . $this->token;
40
41
        return $headers;
42
    }
43
44
    /**
45
     * @param string $uri
46
     * @param array $params
47
     * @return array
48
     */
49
    public function post($uri, $params = [])
50
    {
51
        $headers = $this->createHeaders();
52
        return $this->client->post($uri, $params, $headers);
0 ignored issues
show
Bug introduced by
It seems like $headers defined by $this->createHeaders() on line 51 can also be of type array; however, seregazhuk\HeadHunterApi...s\HttpInterface::post() does only seem to accept null, 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...
53
    }
54
}