Completed
Pull Request — master (#136)
by Fabien
09:36 queued 06:59
created

ProfileClientFactory::createClient()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 0
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 4
nop 1
crap 20
1
<?php
2
3
namespace Http\HttplugBundle\Collector;
4
5
use Http\Client\Common\FlexibleHttpClient;
6
use Http\Client\HttpAsyncClient;
7
use Http\Client\HttpClient;
8
use Http\HttplugBundle\ClientFactory\ClientFactory;
9
10
/**
11
 * The ProfileClientFactory decorates any ClientFactory and returns the created client decorated by a ProfileClient.
12
 *
13
 * @author Fabien Bourigault <[email protected]>
14
 *
15
 * @internal
16
 */
17
class ProfileClientFactory implements ClientFactory
18
{
19
    /**
20
     * @var ClientFactory|callable
21
     */
22
    private $factory;
23
24
    /**
25
     * @var Collector
26
     */
27
    private $collector;
28
29
    /**
30
     * @param ClientFactory|callable $factory
31
     * @param Collector              $collector
32
     */
33 1
    public function __construct($factory, Collector $collector)
34
    {
35 1
        if (!$factory instanceof ClientFactory && !is_callable($factory)) {
36
            throw new \RuntimeException(sprintf('First argument to ProfileClientFactory::__construct must be a "%s" or a callable.', ClientFactory::class));
37
        }
38
        $this->factory = $factory;
1 ignored issue
show
Documentation Bug introduced by
It seems like $factory can also be of type object<Http\HttplugBundl...tFactory\ClientFactory>. However, the property $factory is declared as type callable. 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...
39
        $this->collector = $collector;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function createClient(array $config = [])
46
    {
47
        $client = is_callable($this->factory) ? $this->factory($config) : $this->factory->createClient($config);
0 ignored issues
show
Bug introduced by
The method factory() does not seem to exist on object<Http\HttplugBundl...r\ProfileClientFactory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
49
        if (!($client instanceof HttpClient && $client instanceof HttpAsyncClient)) {
50
            $client = new FlexibleHttpClient($client);
51
        }
52
53
        return new ProfileClient($client, $this->collector);
54
    }
55
}
56