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

Request   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
c 3
b 0
f 0
lcom 1
cbo 1
dl 0
loc 46
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A get() 0 5 1
A createHeaders() 0 7 2
A post() 0 5 1
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
}