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.

ApiClient::signParams()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * (c) Christian Gripp <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Core23\LastFm\Client;
11
12
use Core23\LastFm\Connection\ConnectionInterface;
13
use Core23\LastFm\Exception\ApiException;
14
use Core23\LastFm\Exception\NotFoundException;
15
use Core23\LastFm\Session\SessionInterface;
16
17
final class ApiClient implements ApiClientInterface
18
{
19
    /**
20
     * @var ConnectionInterface
21
     */
22
    private $connection;
23
24
    /**
25
     * @var string
26
     */
27
    private $apiKey;
28
29
    /**
30
     * @var string
31
     */
32
    private $sharedSecret;
33
34
    public function __construct(ConnectionInterface $connection, string $apiKey, string $sharedSecret)
35
    {
36
        $this->connection   = $connection;
37
        $this->apiKey       = $apiKey;
38
        $this->sharedSecret = $sharedSecret;
39
    }
40
41
    public function getApiKey(): string
42
    {
43
        return $this->apiKey;
44
    }
45
46
    public function getSharedSecret(): string
47
    {
48
        return $this->sharedSecret;
49
    }
50
51
    public function signedCall(string $method, array $params = [], SessionInterface $session = null, $requestMethod = 'GET'): array
52
    {
53
        // Call parameter
54
        $callParams = [
55
            'method'  => $method,
56
            'api_key' => $this->apiKey,
57
        ];
58
59
        // Add session key
60
        if (null !== $session) {
61
            $callParams['sk'] = $session->getKey();
62
        }
63
64
        $params = array_merge($callParams, $params);
65
        $params = $this->filterNull($params);
66
        $params = $this->encodeUTF8($params);
67
68
        // Sign parameter
69
        $params['api_sig'] = $this->signParams($params);
0 ignored issues
show
Bug introduced by
It seems like $params defined by $this->encodeUTF8($params) on line 66 can also be of type string; however, Core23\LastFm\Client\ApiClient::signParams() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
70
71
        return $this->call($method, $params);
0 ignored issues
show
Bug introduced by
It seems like $params defined by $this->encodeUTF8($params) on line 66 can also be of type string; however, Core23\LastFm\Client\ApiClient::call() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
72
    }
73
74
    public function unsignedCall(string $method, array $params = [], string $requestMethod = 'GET'): array
75
    {
76
        // Call parameter
77
        $callParameter = [
78
            'method'  => $method,
79
            'api_key' => $this->apiKey,
80
        ];
81
82
        $params = array_merge($callParameter, $params);
83
        $params = $this->filterNull($params);
84
        $params = $this->encodeUTF8($params);
85
86
        return $this->call($method, $params);
0 ignored issues
show
Bug introduced by
It seems like $params defined by $this->encodeUTF8($params) on line 84 can also be of type string; however, Core23\LastFm\Client\ApiClient::call() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
87
    }
88
89
    /**
90
     * Filter null values.
91
     */
92
    private function filterNull(array $object): array
93
    {
94
        return array_filter($object, static function ($val) {
95
            return null !== $val;
96
        });
97
    }
98
99
    /**
100
     * Converts any string or array of strings to UTF8.
101
     *
102
     * @param mixed|mixed[] $object a String or an array
103
     *
104
     * @return mixed|mixed[] UTF8-string or array
105
     */
106
    private function encodeUTF8($object)
107
    {
108
        if (\is_array($object)) {
109
            return array_map([$this, 'encodeUTF8'], $object);
110
        }
111
112
        return mb_convert_encoding((string) $object, 'UTF-8', 'auto');
113
    }
114
115
    private function signParams(array $params): string
116
    {
117
        ksort($params);
118
119
        $signature = '';
120
        foreach ($params as $name => $value) {
121
            $signature .= $name.$value;
122
        }
123
        $signature .= $this->sharedSecret;
124
125
        return md5($signature);
126
    }
127
128
    private function call(string $method, array $params): array
129
    {
130
        try {
131
            return $this->connection->call($method, $params);
132
        } catch (ApiException $e) {
133
            if (6 === (int) $e->getCode()) {
134
                throw new NotFoundException('No entity was found for your request.', $e->getCode(), $e);
135
            }
136
137
            throw $e;
138
        }
139
    }
140
}
141