Completed
Push — master ( 955d05...19c28e )
by David
03:37
created

FlexibleHttpClient::__construct()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 8.8333
c 0
b 0
f 0
cc 7
nc 5
nop 1
crap 7
1
<?php
2
3
namespace Http\Client\Common;
4
5
use Http\Client\HttpAsyncClient;
6
use Http\Client\HttpClient;
7
use Psr\Http\Client\ClientInterface;
8
9
/**
10
 * A flexible http client, which implements both interface and will emulate
11
 * one contract, the other, or none at all depending on the injected client contract.
12
 *
13
 * @author Joel Wurtz <[email protected]>
14
 */
15
final class FlexibleHttpClient implements HttpClient, HttpAsyncClient
16
{
17
    use HttpClientDecorator;
18
    use HttpAsyncClientDecorator;
19
20
    /**
21
     * @param HttpClient|HttpAsyncClient|ClientInterface $client
22
     */
23 34
    public function __construct($client)
24
    {
25 34
        if (!($client instanceof HttpClient) && !($client instanceof HttpAsyncClient) && !($client instanceof ClientInterface)) {
0 ignored issues
show
Bug introduced by
The class Psr\Http\Client\ClientInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
26 1
            throw new \LogicException('Client must be an instance of Http\\Client\\HttpClient or Http\\Client\\HttpAsyncClient');
27
        }
28
29 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<Http\Client\HttpC...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...
30 33
        $this->httpAsyncClient = $client;
0 ignored issues
show
Documentation Bug introduced by
It seems like $client can also be of type object<Http\Client\HttpClient> or 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...
31
32 33
        if (!($this->httpClient instanceof HttpClient) && !($client instanceof ClientInterface)) {
0 ignored issues
show
Bug introduced by
The class Psr\Http\Client\ClientInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
33 11
            $this->httpClient = new EmulatedHttpClient($this->httpClient);
34
        }
35
36 33
        if (!($this->httpAsyncClient instanceof HttpAsyncClient)) {
37 21
            $this->httpAsyncClient = new EmulatedHttpAsyncClient($this->httpAsyncClient);
38
        }
39 33
    }
40
}
41