1 | <?php |
||
2 | |||
3 | /** Namespace */ |
||
4 | namespace MatheusBastos\CafeApi; |
||
5 | |||
6 | /** |
||
7 | * Api abstract class |
||
8 | * @package MatheusBastos\CafeApi |
||
9 | */ |
||
10 | abstract class CafeApi |
||
11 | { |
||
12 | /** @var string */ |
||
13 | private $api_url; |
||
14 | |||
15 | /** @var array */ |
||
16 | private $headers; |
||
17 | |||
18 | /** @var array */ |
||
19 | private $fields; |
||
20 | |||
21 | /** @var string */ |
||
22 | private $endpoint; |
||
23 | |||
24 | /** @var string */ |
||
25 | private $method; |
||
26 | |||
27 | /** @var object */ |
||
28 | protected $response; |
||
29 | |||
30 | /** |
||
31 | * Api constructor |
||
32 | * @param string $apiUrl |
||
33 | * @param string $email |
||
34 | * @param string $password |
||
35 | */ |
||
36 | public function __construct(string $api_url, string $email, string $password) |
||
37 | { |
||
38 | $this->api_url = $api_url; |
||
39 | $this->headers([ |
||
40 | 'email' => $email, |
||
41 | 'password' => $password, |
||
42 | ]); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Api request |
||
47 | * @param string $method |
||
48 | * @param string $endpoint |
||
49 | * @param array|null $fields |
||
50 | * @param array|null $headers |
||
51 | * @return void |
||
52 | */ |
||
53 | protected function request(string $method, string $endpoint, array $fields = null, array $headers = null): void |
||
54 | { |
||
55 | $this->method = $method; |
||
56 | $this->endpoint = $endpoint; |
||
57 | $this->fields = $fields; |
||
58 | $this->headers($headers); |
||
59 | |||
60 | $this->dispatch(); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Api response |
||
65 | * @return object|null |
||
66 | */ |
||
67 | public function response() |
||
68 | { |
||
69 | return $this->response; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Api error |
||
74 | * @return object|null |
||
75 | */ |
||
76 | public function error() |
||
77 | { |
||
78 | if (!empty($this->response->errors)) { |
||
79 | return $this->response->errors; |
||
80 | } |
||
81 | |||
82 | return null; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Api headers |
||
87 | * @param array|null $headers |
||
88 | * @return void |
||
89 | */ |
||
90 | private function headers(?array $headers): void |
||
91 | { |
||
92 | if (!$headers) { |
||
93 | return; |
||
94 | } |
||
95 | |||
96 | foreach ($headers as $key => $header) { |
||
97 | $this->headers[] = "{$key}: {$header}"; |
||
98 | } |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Api dispatch |
||
103 | * @return void |
||
104 | */ |
||
105 | private function dispatch(): void |
||
106 | { |
||
107 | $curl = curl_init(); |
||
108 | |||
109 | if (empty($this->fields['files'])) { |
||
110 | $this->fields = !empty($this->fields) ? http_build_query($this->fields) : null; |
||
0 ignored issues
–
show
|
|||
111 | } |
||
112 | |||
113 | curl_setopt_array($curl, [ |
||
114 | CURLOPT_URL => "{$this->api_url}/{$this->endpoint}", |
||
115 | CURLOPT_RETURNTRANSFER => true, |
||
116 | CURLOPT_MAXREDIRS => 10, |
||
117 | CURLOPT_TIMEOUT => 30, |
||
118 | CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
||
119 | CURLOPT_CUSTOMREQUEST => $this->method, |
||
120 | CURLOPT_POSTFIELDS => $this->fields, |
||
121 | CURLOPT_HTTPHEADER => $this->headers, |
||
122 | ]); |
||
123 | |||
124 | $this->response = json_decode(curl_exec($curl)); |
||
125 | curl_close($curl); |
||
126 | } |
||
127 | } |
||
128 |
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.