GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ApiClient::getHttpClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Multicoin\Api\Service;
4
5
use Exception;
6
use Http\Client\Common\Exception\ClientErrorException;
7
use Http\Client\Common\HttpMethodsClient;
8
use Http\Client\Common\Plugin\BaseUriPlugin;
9
use Http\Client\Common\PluginClient;
10
use Http\Client\HttpClient;
11
use Http\Discovery\HttpClientDiscovery;
12
use Http\Discovery\MessageFactoryDiscovery;
13
use Http\Discovery\UriFactoryDiscovery;
14
use Http\Message\RequestFactory;
15
use Http\Message\UriFactory;
16
use Illuminate\Support\Collection;
17
18
class ApiClient
19
{
20
    protected $client;
0 ignored issues
show
Coding Style introduced by
Protected member variable "client" must contain a leading underscore
Loading history...
21
    /**
22
     * @var HttpClient
23
     */
24
    protected $httpClient;
0 ignored issues
show
Coding Style introduced by
Protected member variable "httpClient" must contain a leading underscore
Loading history...
25
26
    protected $plugins = [];
0 ignored issues
show
Coding Style introduced by
Protected member variable "plugins" must contain a leading underscore
Loading history...
27
    /**
28
     * @var RequestFactory
29
     */
30
    private $requestFactory;
0 ignored issues
show
Coding Style introduced by
Private member variable "requestFactory" must contain a leading underscore
Loading history...
31
32
    /**
33
     * @var UriFactory
34
     */
35
    private $uriFactory;
0 ignored issues
show
Coding Style introduced by
Private member variable "uriFactory" must contain a leading underscore
Loading history...
36
37
    public function __construct(
38
        string $baseUrl,
39
        array $plugins = [],
40
        bool $replace = true,
41
        HttpClient $httpClient = null,
42
        RequestFactory $requestFactory = null
43
    ) {
44
        $this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find();
45
        $this->httpClient = $httpClient ?: HttpClientDiscovery::find();
46
        $this->uriFactory = new BaseUriPlugin(
0 ignored issues
show
Documentation Bug introduced by
It seems like new Http\Client\Common\P...'replace' => $replace)) of type Http\Client\Common\Plugin\BaseUriPlugin is incompatible with the declared type Http\Message\UriFactory of property $uriFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47
            UriFactoryDiscovery::find()->createUri($baseUrl),
48
            ['replace' => $replace]
49
        );
50
        $this->addPlugins(array_merge($plugins, [$this->uriFactory]));
51
        $this->client = $this->getHttpClient();
52
    }
53
54
    public function addPlugins($plugin)
55
    {
56
        $this->plugins = array_merge($this->plugins, $plugin);
57
        $this->client = $this->getHttpClient();
58
59
        return $this;
60
    }
61
62
    public function getHttpClient()
63
    {
64
        return new HttpMethodsClient($this->getPluginClient(), $this->requestFactory);
65
    }
66
67
    public function getPluginClient()
68
    {
69
        return new PluginClient($this->httpClient, $this->plugins);
70
    }
71
72
    public function doGet(string $url): ?Collection
73
    {
74
        try {
75
            $request = $this->client->get($url);
76
            $response = $request->getBody()->getContents();
77
        } catch (ClientErrorException $exception) {
78
            throw new \Exception($exception);
79
            throw new Exception($exception->getMessage()." Error Processing Request for [$url]", 1);
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $url instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Unused Code introduced by
ThrowNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
80
81
            return collect([
82
                'error' => [
83
                    'code' => $exception->getCode(),
84
                    'reason' => $exception->getMessage(),
85
                ],
86
            ]);
87
        }
88
89
        return $this->responseResult($response);
90
    }
91
92
    public function doPost(string $url, array $data = []): ?Collection
93
    {
94
        try {
95
            $request = $this->client->post($url, $data);
96
            $response = $request->getBody()->getContents();
97
        } catch (ClientErrorException $exception) {
98
            throw new \Exception($exception);
99
            // throw new Exception($exception->getMessage()." Error Processing Request for [$url]", 1);
100
101
            return collect([
0 ignored issues
show
Unused Code introduced by
return collect(array('er...eption->getMessage()))) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
102
                'error' => [
103
                    'code' => $exception->getCode(),
104
                    'reason' => $exception->getMessage(),
105
                ],
106
            ]);
107
        }
108
109
        return $this->responseResult($response);
110
    }
111
112
    protected function responseResult(?string $response): ?Collection
113
    {
114
        $data = json_decode($response, true);
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type null; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

114
        $data = json_decode(/** @scrutinizer ignore-type */ $response, true);
Loading history...
115
116
        if (isset($data['error'])) {
117
            throw new Exception($data['error']['message']);
118
        }
119
120
        return collect($data);
121
    }
122
}
123