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 ( 5442c0...516271 )
by François
02:38
created

Response   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 78
Duplicated Lines 5.13 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 2
cbo 0
dl 4
loc 78
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getStatusCode() 0 4 1
A getBody() 0 4 1
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 RuntimeException;
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
     * @return string|null
70
     */
71
    public function getHeader($key)
72
    {
73
        foreach ($this->responseHeaders as $k => $v) {
74
            if (strtoupper($key) === strtoupper($k)) {
75
                return $v;
76
            }
77
        }
78
79
        return null;
80
    }
81
82
    /**
83
     * @return mixed
84
     */
85
    public function json()
86
    {
87
        if (false === strpos($this->getHeader('Content-Type'), 'application/json')) {
88
            throw new RuntimeException(sprintf('response MUST have JSON content type'));
89
        }
90
        $decodedJson = json_decode($this->responseBody, true);
91 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...
92
            $errorMsg = function_exists('json_last_error_msg') ? json_last_error_msg() : json_last_error();
93
            throw new RuntimeException(sprintf('unable to decode JSON: %s', $errorMsg));
94
        }
95
96
        return $decodedJson;
97
    }
98
99
    /**
100
     * @return bool
101
     */
102
    public function isOkay()
103
    {
104
        return 200 <= $this->statusCode && 300 > $this->statusCode;
105
    }
106
}
107