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
Pull Request — master (#41)
by
unknown
02:30
created

Utils::normalizeUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 1
cts 1
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace OpenStack\Common\Transport;
4
5
use function GuzzleHttp\Psr7\uri_for;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\UriInterface;
8
9
class Utils
10
{
11 115
    public static function jsonDecode(ResponseInterface $response, $assoc = true)
12
    {
13
        $jsonErrors = [
14 115
            JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded',
15 115
            JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch',
16 115
            JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found',
17 115
            JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON',
18 115
            JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded'
19 115
        ];
20
21 115
        $responseBody = (string) $response->getBody();
22
23 115
        if (strlen($responseBody) === 0) {
24 1
            return $responseBody;
25 1
        }
26 1
27 1
        $data = json_decode($responseBody, $assoc);
28
29
        if (JSON_ERROR_NONE !== json_last_error()) {
30 114
            $last = json_last_error();
31
            throw new \InvalidArgumentException(
32
                'Unable to parse JSON data: ' . (isset($jsonErrors[$last]) ? $jsonErrors[$last] : 'Unknown error')
33
            );
34
        }
35
36
        return $data;
37
    }
38
39
    /**
40
     * Method for flattening a nested array.
41 94
     *
42
     * @param array $data The nested array
43 94
     * @param null  $key  The key to extract
44
     *
45
     * @return array
46
     */
47
    public static function flattenJson($data, $key = null)
48
    {
49
        return (!empty($data) && $key && isset($data[$key])) ? $data[$key] : $data;
50
    }
51
52
    /**
53
     * Method for normalize an URL string.
54
     *
55
     * Append the http:// prefix if not present, and add a
56 6
     * closing url separator when missing.
57
     *
58 6
     * @param string $url The url representation.
59 6
     *
60 6
     * @return string
61
     */
62 6
    public static function normalizeUrl($url)
63
    {
64
        if (strpos($url, 'http') === false) {
65
            $url = 'http://' . $url;
66
        }
67
68
        return rtrim($url, '/') . '/';
69
    }
70
71
    /**
72
     * Add an unlimited list of paths to a given URI.
73 2
     *
74
     * @param UriInterface $uri
75 2
     * @param              ...$paths
76
     *
77
     * @return UriInterface
78 5
     */
79
    public static function addPaths(UriInterface $uri, ...$paths)
80 5
    {
81
        return uri_for(rtrim((string) $uri, '/') . '/' . implode('/', $paths));
82
    }
83
84
    public static function appendPath(UriInterface $uri, $path)
85
    {
86
        return uri_for(rtrim((string) $uri, '/') . '/' . $path);
87
    }
88
}
89