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.
Completed
Push — master ( 0961e1...5da68e )
by François
03:46
created

GuzzleHttpClient::addRequestHeader()   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 2
1
<?php
2
/**
3
 *  Copyright (C) 2016 SURFnet.
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU Affero General Public License as
7
 *  published by the Free Software Foundation, either version 3 of the
8
 *  License, or (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU Affero General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Affero General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
namespace SURFnet\VPN\Common\HttpClient;
19
20
use GuzzleHttp\Client;
21
use SURFnet\VPN\Common\HttpClient\Exception\HttpClientException;
22
use GuzzleHttp\Exception\BadResponseException;
23
use InvalidArgumentException;
24
use RuntimeException;
25
26
class GuzzleHttpClient implements HttpClientInterface
27
{
28
    /** @var \GuzzleHttp\Client */
29
    private $httpClient;
30
31
    public function __construct(array $guzzleOptions)
32
    {
33
        // http://docs.guzzlephp.org/en/5.3/clients.html#request-options
34
        $defaultOptions = [
35
            'allow_redirects' => false,
36
            'timeout' => 5,
37
            'headers' => [
38
                'Accept' => 'application/json',
39
            ],
40
        ];
41
42
        $this->httpClient = new Client(
43
            array_merge_recursive($defaultOptions, $guzzleOptions)
44
        );
45
    }
46
47 View Code Duplication
    public function get($requestUri, array $requestHeaders = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
    {
49
        try {
50
            return $this->httpClient->get(
51
                $requestUri,
52
                [
53
                    'headers' => $requestHeaders,
54
                ]
55
            )->json();
56
        } catch (BadResponseException $e) {
57
            $this->handleError($e);
58
        }
59
    }
60
61 View Code Duplication
    public function post($requestUri, array $postData, array $requestHeaders = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63
        try {
64
            return $this->httpClient->post(
65
                $requestUri,
66
                [
67
                    'body' => [
68
                        $postData,
69
                    ],
70
                    'headers' => $requestHeaders,
71
                ]
72
            )->json();
73
        } catch (BadResponseException $e) {
74
            $this->handleError($e);
75
        }
76
    }
77
78
    public function handleError(BadResponseException $e)
0 ignored issues
show
Unused Code introduced by
The parameter $e is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $e. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
79
    {
80
        try {
81
            $responseData = $e->getResponse()->json();
0 ignored issues
show
Bug introduced by
The method getResponse does only exist in GuzzleHttp\Exception\BadResponseException, but not in InvalidArgumentException.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
82
        } catch (InvalidArgumentException $e) {
83
            // unable to decode JSON
84
            throw new RuntimeException('unable to decode JSON in HTTP response');
85
        }
86
87
        if (!is_array($responseData) && !array_key_exists('error', $responseData)) {
88
            throw new RuntimeException(
89
                sprintf(
90
                    'unexpected response with code "%s" from "%s"',
91
                    $e->getResponse()->getStatusCode(),
92
                    $e->getResponse()->getEffectiveUrl()
93
                )
94
            );
95
        }
96
97
        throw new HttpClientException($responseData['error']);
98
    }
99
}
100