ProfileClientFactory   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 6 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 3
loc 50
ccs 12
cts 13
cp 0.9231
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A createClient() 3 10 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Http\HttplugBundle\Collector;
6
7
use Http\Client\Common\FlexibleHttpClient;
8
use Http\Client\HttpAsyncClient;
9
use Http\Client\HttpClient;
10
use Http\HttplugBundle\ClientFactory\ClientFactory;
11
use Psr\Http\Client\ClientInterface;
12
use Symfony\Component\Stopwatch\Stopwatch;
13
14
/**
15
 * The ProfileClientFactory decorates any ClientFactory and returns the created client decorated by a ProfileClient.
16
 *
17
 * @author Fabien Bourigault <[email protected]>
18
 *
19
 * @internal
20
 */
21
class ProfileClientFactory implements ClientFactory
22
{
23
    /**
24
     * @var ClientFactory|callable
25
     */
26
    private $factory;
27
28
    /**
29
     * @var Collector
30
     */
31
    private $collector;
32
33
    /**
34
     * @var Formatter
35
     */
36
    private $formatter;
37
38
    /**
39
     * @var Stopwatch
40
     */
41
    private $stopwatch;
42
43
    /**
44
     * @param ClientFactory|callable $factory
45
     */
46 5
    public function __construct($factory, Collector $collector, Formatter $formatter, Stopwatch $stopwatch)
47
    {
48 5
        if (!$factory instanceof ClientFactory && !is_callable($factory)) {
49
            throw new \RuntimeException(sprintf('First argument to ProfileClientFactory::__construct must be a "%s" or a callable.', ClientFactory::class));
50
        }
51 5
        $this->factory = $factory;
52 5
        $this->collector = $collector;
53 5
        $this->formatter = $formatter;
54 5
        $this->stopwatch = $stopwatch;
55 5
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 5
    public function createClient(array $config = [])
61
    {
62 5
        $client = is_callable($this->factory) ? call_user_func($this->factory, $config) : $this->factory->createClient($config);
63
64 5 View Code Duplication
        if (!(($client instanceof HttpClient || $client instanceof ClientInterface) && $client instanceof HttpAsyncClient)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65 2
            $client = new FlexibleHttpClient($client);
66
        }
67
68 5
        return new ProfileClient($client, $this->collector, $this->formatter, $this->stopwatch);
69
    }
70
}
71