HttpTransliteratorAbstract   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 19
c 1
b 0
f 0
dl 0
loc 83
ccs 11
cts 22
cp 0.5
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A translit() 0 5 1
A makeRequest() 0 13 3
A __destruct() 0 3 1
A __construct() 0 10 1
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
Documentation Bug introduced by
It seems like curl_init() can also be of type false. However, the property $curl_handle is declared as type resource. 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 $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account 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.

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...
24 48
        \curl_setopt_array(
25 48
            $this->curl_handle,
0 ignored issues
show
Bug introduced by
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 ignore-type  annotation

25
            /** @scrutinizer ignore-type */ $this->curl_handle,
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