Completed
Pull Request — master (#136)
by Fabien
06:00
created

ProfileClientFactory::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 2
crap 3
1
<?php
2
3
namespace Http\HttplugBundle\Collector;
4
5
use Http\HttplugBundle\ClientFactory\ClientFactory;
6
7
class ProfileClientFactory implements ClientFactory
8
{
9
    /**
10
     * @var ClientFactory|callable
11
     */
12
    private $factory;
13
14
    /**
15
     * @var Collector
16
     */
17
    private $collector;
18
19
    /**
20
     * @param ClientFactory|callable $factory
21
     * @param Collector $collector
22
     */
23 1
    public function __construct($factory, Collector $collector)
24
    {
25 1
        if (!$factory instanceof ClientFactory && !is_callable($factory)) {
26
            throw new \RuntimeException(sprintf('Second argument to PluginClientFactory::createPluginClient must be a "%s" or a callable.', ClientFactory::class));
27
        }
28
        $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...
29
        $this->collector = $collector;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function createClient(array $config = [])
36
    {
37
        $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...
38
39
        return new ProfileClient($client, $this->collector);
40
    }
41
}
42