zvermafia /
transliteration
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Zvermafia\Transliteration\Abstracts; |
||||
| 4 | |||||
| 5 | use Zvermafia\Transliteration\Interfaces\TransliteratorInterface; |
||||
| 6 | use Zvermafia\Transliteration\Exceptions\TransliteratorException; |
||||
| 7 | |||||
| 8 | /** |
||||
| 9 | * Base transliterator class |
||||
| 10 | * |
||||
| 11 | * This base class can be extended by all transliterators which will use cURL as an HTTP handler. |
||||
| 12 | */ |
||||
| 13 | abstract class HttpTransliteratorAbstract extends TransliteratorAbstract |
||||
| 14 | { |
||||
| 15 | /** @var resource */ |
||||
| 16 | protected $curl_handle; |
||||
| 17 | |||||
| 18 | /** |
||||
| 19 | * Initialize a cURL instance. |
||||
| 20 | */ |
||||
| 21 | 48 | public function __construct() |
|||
| 22 | { |
||||
| 23 | 48 | $this->curl_handle = \curl_init(); |
|||
|
0 ignored issues
–
show
|
|||||
| 24 | 48 | \curl_setopt_array( |
|||
| 25 | 48 | $this->curl_handle, |
|||
|
0 ignored issues
–
show
It seems like
$this->curl_handle 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...
|
|||||
| 26 | 48 | array_replace( |
|||
| 27 | 48 | $this->getDefaultOptions(), |
|||
| 28 | [ |
||||
| 29 | 48 | \CURLOPT_RETURNTRANSFER => true, |
|||
| 30 | \CURLOPT_USERAGENT => 'Zvermafia-Transliteration/2.0.0 (+https://zvermafia/transliteration)', |
||||
| 31 | ] |
||||
| 32 | ) |
||||
| 33 | ); |
||||
| 34 | 48 | } |
|||
| 35 | |||||
| 36 | /** |
||||
| 37 | * Destroy the cURL instance. |
||||
| 38 | */ |
||||
| 39 | public function __destruct() |
||||
| 40 | { |
||||
| 41 | \curl_close($this->curl_handle); |
||||
| 42 | } |
||||
| 43 | |||||
| 44 | /** |
||||
| 45 | * @inheritDoc |
||||
| 46 | */ |
||||
| 47 | 12 | public function translit(): TransliteratorInterface |
|||
| 48 | { |
||||
| 49 | 12 | $this->setResult($this->getResultFromResponse($this->makeRequest())); |
|||
| 50 | |||||
| 51 | 12 | return $this; |
|||
| 52 | } |
||||
| 53 | |||||
| 54 | /** |
||||
| 55 | * Make request to an API. |
||||
| 56 | * |
||||
| 57 | * @throws \Zvermafia\Transliteration\Exceptions\TransliteratorException |
||||
| 58 | * @return string Response body |
||||
| 59 | */ |
||||
| 60 | protected function makeRequest(): string |
||||
| 61 | { |
||||
| 62 | \curl_setopt_array($this->curl_handle, $this->getPerRequestOptions()); |
||||
| 63 | $response = \curl_exec($this->curl_handle); |
||||
| 64 | |||||
| 65 | if ($response === false || \curl_errno($this->curl_handle) !== 0) { |
||||
| 66 | throw new TransliteratorException( |
||||
| 67 | \curl_error($this->curl_handle), |
||||
| 68 | \curl_getinfo($this->curl_handle, CURLINFO_HTTP_CODE) |
||||
| 69 | ); |
||||
| 70 | } |
||||
| 71 | |||||
| 72 | return $response; |
||||
| 73 | } |
||||
| 74 | |||||
| 75 | /** |
||||
| 76 | * Get default options for all the subsequent requests. |
||||
| 77 | * |
||||
| 78 | * @return array |
||||
| 79 | */ |
||||
| 80 | abstract protected function getDefaultOptions(): array; |
||||
| 81 | |||||
| 82 | /** |
||||
| 83 | * Get per request options. |
||||
| 84 | * |
||||
| 85 | * @return array |
||||
| 86 | */ |
||||
| 87 | abstract protected function getPerRequestOptions(): array; |
||||
| 88 | |||||
| 89 | /** |
||||
| 90 | * Fetch the result from the response and return it. |
||||
| 91 | * |
||||
| 92 | * @param string $response |
||||
| 93 | * @return string |
||||
| 94 | */ |
||||
| 95 | abstract protected function getResultFromResponse(string $response): string; |
||||
| 96 | } |
||||
| 97 |
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
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. 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.