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.

Utils::jsonDecode()   B
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 27
ccs 14
cts 14
cp 1
rs 8.5806
cc 4
eloc 16
nc 3
nop 2
crap 4
1
<?php declare(strict_types=1);
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, bool $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 string  $key  The key to extract
44
     *
45
     * @return array
46
     */
47
    public static function flattenJson($data, string $key = null)
48
    {
49
        return (!empty($data) && $key && isset($data[$key])) ? $data[$key] : $data;
0 ignored issues
show
Bug Best Practice introduced by
The expression $key of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
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(string $url): string
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): UriInterface
80 5
    {
81
        return uri_for(rtrim((string) $uri, '/') . '/' . implode('/', $paths));
82
    }
83
84
    public static function appendPath(UriInterface $uri, $path): UriInterface
85
    {
86
        return uri_for(rtrim((string) $uri, '/') . '/' . $path);
87
    }
88
}
89