machadomatt /
cafeapi
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace MachadoMatt\CafeApi; |
||||
| 4 | |||||
| 5 | /** |
||||
| 6 | * Class CafeApi |
||||
| 7 | * @package MachadoMatt\CafeApi |
||||
| 8 | */ |
||||
| 9 | abstract class CafeApi |
||||
| 10 | { |
||||
| 11 | /** @var string */ |
||||
| 12 | private $apiUrl; |
||||
| 13 | |||||
| 14 | /** @var array */ |
||||
| 15 | private $headers; |
||||
| 16 | |||||
| 17 | /** @var array */ |
||||
| 18 | private $fields; |
||||
| 19 | |||||
| 20 | /** @var string */ |
||||
| 21 | private $endpoint; |
||||
| 22 | |||||
| 23 | /** @var string */ |
||||
| 24 | private $method; |
||||
| 25 | |||||
| 26 | /** @var object */ |
||||
| 27 | protected $response; |
||||
| 28 | |||||
| 29 | /** |
||||
| 30 | * CafeApi constructor. |
||||
| 31 | * @param string $apiUrl |
||||
| 32 | * @param string $email |
||||
| 33 | * @param string $password |
||||
| 34 | */ |
||||
| 35 | public function __construct(string $apiUrl, string $email, string $password) |
||||
| 36 | { |
||||
| 37 | $this->apiUrl = $apiUrl; |
||||
| 38 | $this->headers([ |
||||
| 39 | "email" => $email, |
||||
| 40 | "password" => $password |
||||
| 41 | ]); |
||||
| 42 | } |
||||
| 43 | |||||
| 44 | /** |
||||
| 45 | * @param string $method |
||||
| 46 | * @param string $endpoint |
||||
| 47 | * @param array|null $fields |
||||
| 48 | * @param array|null $headers |
||||
| 49 | */ |
||||
| 50 | protected function request(string $method, string $endpoint, array $fields = null, array $headers = null): void |
||||
| 51 | { |
||||
| 52 | $this->method = $method; |
||||
| 53 | $this->endpoint = $endpoint; |
||||
| 54 | $this->fields = $fields; |
||||
| 55 | $this->headers($headers); |
||||
| 56 | |||||
| 57 | $this->dispatch(); |
||||
| 58 | } |
||||
| 59 | |||||
| 60 | /** |
||||
| 61 | * @return object|null |
||||
| 62 | */ |
||||
| 63 | public function response() |
||||
| 64 | { |
||||
| 65 | return $this->response; |
||||
| 66 | } |
||||
| 67 | |||||
| 68 | /** |
||||
| 69 | * @return object|null |
||||
| 70 | */ |
||||
| 71 | public function error() |
||||
| 72 | { |
||||
| 73 | if (!empty($this->response->errors)) { |
||||
| 74 | return $this->response->errors; |
||||
| 75 | } |
||||
| 76 | |||||
| 77 | return null; |
||||
| 78 | } |
||||
| 79 | |||||
| 80 | /** |
||||
| 81 | * @param array|null $headers |
||||
| 82 | */ |
||||
| 83 | private function headers(?array $headers): void |
||||
| 84 | { |
||||
| 85 | if (!$headers) { |
||||
|
0 ignored issues
–
show
|
|||||
| 86 | return; |
||||
| 87 | } |
||||
| 88 | |||||
| 89 | foreach ($headers as $key => $header) { |
||||
| 90 | $this->headers[] = "{$key}: {$header}"; |
||||
| 91 | } |
||||
| 92 | } |
||||
| 93 | |||||
| 94 | /** |
||||
| 95 | * |
||||
| 96 | */ |
||||
| 97 | private function dispatch(): void |
||||
| 98 | { |
||||
| 99 | $curl = curl_init(); |
||||
| 100 | |||||
| 101 | if (empty($this->fields["files"])) { |
||||
| 102 | $this->fields = (!empty($this->fields) ? http_build_query($this->fields) : null); |
||||
|
0 ignored issues
–
show
It seems like
! empty($this->fields) ?...y($this->fields) : null can also be of type string. However, the property $fields is declared as type array. 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...
|
|||||
| 103 | } |
||||
| 104 | |||||
| 105 | curl_setopt_array($curl, array( |
||||
|
0 ignored issues
–
show
It seems like
$curl can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 106 | CURLOPT_URL => "{$this->apiUrl}/{$this->endpoint}", |
||||
| 107 | CURLOPT_RETURNTRANSFER => true, |
||||
| 108 | CURLOPT_MAXREDIRS => 10, |
||||
| 109 | CURLOPT_TIMEOUT => 30, |
||||
| 110 | CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
||||
| 111 | CURLOPT_CUSTOMREQUEST => $this->method, |
||||
| 112 | CURLOPT_POSTFIELDS => $this->fields, |
||||
| 113 | CURLOPT_HTTPHEADER => $this->headers, |
||||
| 114 | )); |
||||
| 115 | |||||
| 116 | $this->response = json_decode(curl_exec($curl)); |
||||
|
0 ignored issues
–
show
It seems like
$curl can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 117 | curl_close($curl); |
||||
|
0 ignored issues
–
show
It seems like
$curl can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 118 | } |
||||
| 119 | } |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.