These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * (c) Christian Gripp <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Core23\LastFm\Connection; |
||
13 | |||
14 | use Core23\LastFm\Exception\ApiException; |
||
15 | use Http\Client\Exception; |
||
16 | use Http\Client\HttpClient; |
||
17 | use Http\Message\MessageFactory; |
||
18 | use Psr\Http\Message\RequestInterface; |
||
19 | use Psr\Http\Message\ResponseInterface; |
||
20 | |||
21 | final class HTTPlugConnection implements ConnectionInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var HttpClient |
||
25 | */ |
||
26 | private $client; |
||
27 | |||
28 | /** |
||
29 | * @var MessageFactory |
||
30 | */ |
||
31 | private $messageFactory; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $endpoint; |
||
37 | |||
38 | /** |
||
39 | * Initialize client. |
||
40 | * |
||
41 | * @param HttpClient $client |
||
42 | * @param MessageFactory $messageFactory |
||
43 | * @param string $endpoint |
||
44 | */ |
||
45 | public function __construct(HttpClient $client, MessageFactory $messageFactory, string $endpoint = ConnectionInterface::DEFAULT_ENDPOINT) |
||
46 | { |
||
47 | $this->client = $client; |
||
48 | $this->messageFactory = $messageFactory; |
||
49 | $this->endpoint = $endpoint; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | public function getPageBody(string $url, array $params = [], string $method = 'GET'): ?string |
||
56 | { |
||
57 | $request = $this->createRequest($method, $url, $params); |
||
58 | |||
59 | try { |
||
60 | $response = $this->client->sendRequest($request); |
||
61 | } catch (Exception $e) { |
||
62 | throw new ApiException('Error fetching page body', $e->getCode(), $e->getMessage()); |
||
63 | } |
||
64 | |||
65 | if ($response->getStatusCode() >= 400) { |
||
66 | return null; |
||
67 | } |
||
68 | |||
69 | return $response->getBody()->getContents(); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | public function call(string $url, array $params = [], string $method = 'GET'): array |
||
76 | { |
||
77 | $params = array_merge($params, ['format' => 'json']); |
||
78 | $request = $this->createRequest($method, $this->endpoint, $params); |
||
79 | |||
80 | try { |
||
81 | $response = $this->client->sendRequest($request); |
||
82 | |||
83 | // Parse response |
||
84 | return $this->parseResponse($response); |
||
85 | } catch (ApiException $e) { |
||
86 | throw $e; |
||
87 | } catch (\Exception $e) { |
||
88 | throw new ApiException('Technical error occurred.', 500, $e); |
||
89 | } catch (Exception $e) { |
||
90 | throw new ApiException('Technical error occurred.', $e->getCode(), $e); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @param string $method |
||
96 | * @param string $url |
||
97 | * @param array $params |
||
98 | * |
||
99 | * @return RequestInterface |
||
100 | */ |
||
101 | private function createRequest(string $method, string $url, array $params): RequestInterface |
||
102 | { |
||
103 | if ('POST' === $method) { |
||
104 | return $this->messageFactory->createRequest($method, $url, [], $params); |
||
0 ignored issues
–
show
|
|||
105 | } |
||
106 | $query = http_build_query($params); |
||
107 | |||
108 | return $this->messageFactory->createRequest($method, $url.'?'.$query); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @param ResponseInterface $response |
||
113 | * |
||
114 | * @throws ApiException |
||
115 | * |
||
116 | * @return array |
||
117 | */ |
||
118 | private function parseResponse(ResponseInterface $response): array |
||
119 | { |
||
120 | $array = json_decode($response->getBody()->getContents(), true); |
||
121 | |||
122 | if (\is_array($array) && \array_key_exists('error', $array) && \array_key_exists('message', $array)) { |
||
123 | throw new ApiException($array['message'], $array['error']); |
||
124 | } |
||
125 | |||
126 | return $array; |
||
127 | } |
||
128 | } |
||
129 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: