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.

Response   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 96
Duplicated Lines 4.17 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 16
c 0
b 0
f 0
lcom 2
cbo 1
dl 4
loc 96
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getStatusCode() 0 4 1
A getBody() 0 4 1
A hasHeader() 0 10 3
A getHeader() 0 10 3
B json() 4 13 5
A isOkay() 0 4 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * Copyright (c) 2016, 2017 François Kooman <[email protected]>.
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in all
14
 * copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 */
24
25
namespace fkooman\OAuth\Client\Http;
26
27
use fkooman\OAuth\Client\Http\Exception\ResponseException;
28
29
class Response
30
{
31
    /** @var int */
32
    private $statusCode;
33
34
    /** @var string */
35
    private $responseBody;
36
37
    /** @var array */
38
    private $responseHeaders;
39
40
    /**
41
     * @param int    $statusCode
42
     * @param string $responseBody
43
     * @param array  $responseHeaders
44
     */
45
    public function __construct($statusCode, $responseBody, array $responseHeaders = [])
46
    {
47
        $this->statusCode = $statusCode;
48
        $this->responseBody = $responseBody;
49
        $this->responseHeaders = $responseHeaders;
50
    }
51
52
    /**
53
     * @return int
54
     */
55
    public function getStatusCode()
56
    {
57
        return $this->statusCode;
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function getBody()
64
    {
65
        return $this->responseBody;
66
    }
67
68
    /**
69
     * @param string $key
70
     *
71
     * @return bool
72
     */
73
    public function hasHeader($key)
74
    {
75
        foreach ($this->responseHeaders as $k => $v) {
76
            if (strtoupper($key) === strtoupper($k)) {
77
                return true;
78
            }
79
        }
80
81
        return false;
82
    }
83
84
    /**
85
     * @param string $key
86
     *
87
     * @return string
88
     */
89
    public function getHeader($key)
90
    {
91
        foreach ($this->responseHeaders as $k => $v) {
92
            if (strtoupper($key) === strtoupper($k)) {
93
                return $v;
94
            }
95
        }
96
97
        throw new ResponseException(sprintf('header "%s" not set', $key));
98
    }
99
100
    /**
101
     * @return mixed
102
     */
103
    public function json()
104
    {
105
        if (false === strpos($this->getHeader('Content-Type'), 'application/json')) {
106
            throw new ResponseException(sprintf('response MUST have JSON content type'));
107
        }
108
        $decodedJson = json_decode($this->responseBody, true);
109 View Code Duplication
        if (is_null($decodedJson) && JSON_ERROR_NONE !== json_last_error()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
110
            $errorMsg = function_exists('json_last_error_msg') ? json_last_error_msg() : json_last_error();
111
            throw new ResponseException(sprintf('unable to decode JSON: %s', $errorMsg));
112
        }
113
114
        return $decodedJson;
115
    }
116
117
    /**
118
     * @return bool
119
     */
120
    public function isOkay()
121
    {
122
        return 200 <= $this->statusCode && 300 > $this->statusCode;
123
    }
124
}
125