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.

HttpClient::request()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 2
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Teebot\Api;
6
7
use Teebot\Configuration\ContainerInterface;
8
use GuzzleHttp\{
9
    Client as GuzzleClient,
10
    Psr7\Response
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Teebot\Api\Response. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
11
};
12
13
class HttpClient
14
{
15
    protected const METHOD_GET = 'GET';
16
17
    protected const METHOD_POST = 'POST';
18
19
    /**
20
     * @var ContainerInterface $config
21
     */
22
    protected $config;
23
24
    /**
25
     * @var GuzzleClient
26
     */
27
    protected $client;
28
29
    /**
30
     * @param ContainerInterface $config
31
     */
32
    public function __construct(ContainerInterface $config)
33
    {
34
        $this->config = $config;
35
        $this->client = new GuzzleClient([
36
            'base_uri' => $this->getBaseUri(),
37
            $this->config->get('timeout'),
38
        ]);
39
    }
40
41
    /**
42
     * Returns base API url
43
     *
44
     * @return string
45
     */
46
    protected function getBaseUri(): string
47
    {
48
        return sprintf(
49
            '%s/%s%s/',
50
            $this->config->get('url'),
51
            $this->config->get('bot_prefix'),
52
            $this->config->get('token')
53
        );
54
    }
55
56
    /**
57
     * Performs the request and returns the Response contents
58
     *
59
     * @param string $apiMethodName
60
     * @param array  $requestOptions
61
     *
62
     * @return null|string
63
     */
64
    public function request(string $apiMethodName, array $requestOptions = []): ?string
65
    {
66
        $method = static::METHOD_POST;
67
68
        if (!isset($requestOptions['multipart']) && $this->config->get('method') == static::METHOD_GET) {
69
            $method = static::METHOD_GET;
70
        }
71
72
        $response = $this->client->request($method, $apiMethodName, $requestOptions);
73
74
        if ($response instanceof Response) {
75
            return $response->getBody()->getContents();
76
        }
77
78
        return null;
79
    }
80
}
81