Completed
Push — master ( 9c21b6...43c791 )
by David
04:48
created

FlexibleHttpClient::__construct()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.3888
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Http\Client\Common;
6
7
use Http\Client\HttpAsyncClient;
8
use Http\Client\HttpClient;
9
use Psr\Http\Client\ClientInterface;
10
11
/**
12
 * A flexible http client, which implements both interface and will emulate
13
 * one contract, the other, or none at all depending on the injected client contract.
14
 *
15
 * @author Joel Wurtz <[email protected]>
16
 */
17
final class FlexibleHttpClient implements HttpClient, HttpAsyncClient
18
{
19
    use HttpClientDecorator;
20
    use HttpAsyncClientDecorator;
21
22
    /**
23
     * @param ClientInterface|HttpAsyncClient $client
24
     */
25 34
    public function __construct($client)
26
    {
27 34
        if (!($client instanceof ClientInterface) && !($client instanceof HttpAsyncClient)) {
28 1
            throw new \LogicException('Client must be an instance of Psr\\Http\\Client\\ClientInterface or Http\\Client\\HttpAsyncClient');
29
        }
30
31 33
        $this->httpClient = $client;
0 ignored issues
show
Documentation Bug introduced by
It seems like $client can also be of type object<Http\Client\HttpAsyncClient>. However, the property $httpClient is declared as type object<Psr\Http\Client\ClientInterface>. 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...
32 33
        $this->httpAsyncClient = $client;
0 ignored issues
show
Documentation Bug introduced by
It seems like $client can also be of type object<Psr\Http\Client\ClientInterface>. However, the property $httpAsyncClient is declared as type object<Http\Client\HttpAsyncClient>. 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...
33
34 33
        if (!($this->httpClient instanceof ClientInterface)) {
35 11
            $this->httpClient = new EmulatedHttpClient($this->httpClient);
36
        }
37
38 33
        if (!($this->httpAsyncClient instanceof HttpAsyncClient)) {
39 21
            $this->httpAsyncClient = new EmulatedHttpAsyncClient($this->httpAsyncClient);
40
        }
41 33
    }
42
}
43