These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace DigitalOceanV2\Adapter; |
||
4 | |||
5 | use DigitalOceanV2\Exception\HttpException; |
||
6 | use GuzzleHttp\Client; |
||
7 | use GuzzleHttp\ClientInterface; |
||
8 | use GuzzleHttp\Exception\RequestException; |
||
9 | use GuzzleHttp\Message\ResponseInterface; |
||
10 | use GuzzleHttp\Psr7\Response; |
||
11 | |||
12 | /** |
||
13 | * @author Marcos Sigueros <[email protected]> |
||
14 | * @author Chris Fidao <[email protected]> |
||
15 | * @author Graham Campbell <[email protected]> |
||
16 | */ |
||
17 | class GuzzleHttpAdapter implements AdapterInterface |
||
18 | { |
||
19 | /** |
||
20 | * @var ClientInterface |
||
21 | */ |
||
22 | protected $client; |
||
23 | |||
24 | /** |
||
25 | * @var Response|ResponseInterface |
||
26 | */ |
||
27 | protected $response; |
||
28 | |||
29 | /** |
||
30 | * @param string $token |
||
31 | * @param ClientInterface|null $client |
||
32 | */ |
||
33 | public function __construct($token, ClientInterface $client = null) |
||
34 | { |
||
35 | if (version_compare(ClientInterface::VERSION, '6') === 1) { |
||
36 | $this->client = $client ?: new Client(['headers' => ['Authorization' => sprintf('Bearer %s', $token)]]); |
||
37 | } else { |
||
38 | $this->client = $client ?: new Client(); |
||
39 | |||
40 | $this->client->setDefaultOption('headers/Authorization', sprintf('Bearer %s', $token)); |
||
41 | } |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | */ |
||
47 | public function get($url) |
||
48 | { |
||
49 | try { |
||
50 | $this->response = $this->client->get($url); |
||
51 | } catch (RequestException $e) { |
||
52 | $this->response = $e->getResponse(); |
||
0 ignored issues
–
show
|
|||
53 | $this->handleError(); |
||
54 | } |
||
55 | |||
56 | return $this->response->getBody(); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | public function delete($url) |
||
63 | { |
||
64 | try { |
||
65 | $this->response = $this->client->delete($url); |
||
66 | } catch (RequestException $e) { |
||
67 | $this->response = $e->getResponse(); |
||
0 ignored issues
–
show
It seems like
$e->getResponse() can also be of type object<Psr\Http\Message\ResponseInterface> . However, the property $response is declared as type object<GuzzleHttp\Psr7\R...sage\ResponseInterface> . Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
Loading history...
|
|||
68 | $this->handleError(); |
||
69 | } |
||
70 | |||
71 | return $this->response->getBody(); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | public function put($url, $content = '') |
||
78 | { |
||
79 | $options = []; |
||
80 | |||
81 | $options[is_array($content) ? 'json' : 'body'] = $content; |
||
82 | |||
83 | try { |
||
84 | $this->response = $this->client->put($url, $options); |
||
85 | } catch (RequestException $e) { |
||
86 | $this->response = $e->getResponse(); |
||
0 ignored issues
–
show
It seems like
$e->getResponse() can also be of type object<Psr\Http\Message\ResponseInterface> . However, the property $response is declared as type object<GuzzleHttp\Psr7\R...sage\ResponseInterface> . Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
Loading history...
|
|||
87 | $this->handleError(); |
||
88 | } |
||
89 | |||
90 | return $this->response->getBody(); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | public function post($url, $content = '') |
||
97 | { |
||
98 | $options = []; |
||
99 | |||
100 | $options[is_array($content) ? 'json' : 'body'] = $content; |
||
101 | |||
102 | try { |
||
103 | $this->response = $this->client->post($url, $options); |
||
104 | } catch (RequestException $e) { |
||
105 | $this->response = $e->getResponse(); |
||
0 ignored issues
–
show
It seems like
$e->getResponse() can also be of type object<Psr\Http\Message\ResponseInterface> . However, the property $response is declared as type object<GuzzleHttp\Psr7\R...sage\ResponseInterface> . Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
Loading history...
|
|||
106 | $this->handleError(); |
||
107 | } |
||
108 | |||
109 | return $this->response->getBody(); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | public function getLatestResponseHeaders() |
||
116 | { |
||
117 | if (null === $this->response) { |
||
118 | return; |
||
119 | } |
||
120 | |||
121 | return [ |
||
122 | 'reset' => (int) (string) $this->response->getHeader('RateLimit-Reset'), |
||
123 | 'remaining' => (int) (string) $this->response->getHeader('RateLimit-Remaining'), |
||
124 | 'limit' => (int) (string) $this->response->getHeader('RateLimit-Limit'), |
||
125 | ]; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @throws HttpException |
||
130 | */ |
||
131 | protected function handleError() |
||
132 | { |
||
133 | $body = (string) $this->response->getBody(); |
||
134 | $code = (int) $this->response->getStatusCode(); |
||
135 | |||
136 | $content = json_decode($body); |
||
137 | |||
138 | throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code); |
||
139 | } |
||
140 | } |
||
141 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.