PluginClientFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Http\HttplugBundle\Collector;
6
7
use Http\Client\Common\Plugin;
8
use Http\Client\Common\PluginClient;
9
use Http\Client\HttpAsyncClient;
10
use Http\Client\HttpClient;
11
use Psr\Http\Client\ClientInterface;
12
use Symfony\Component\Stopwatch\Stopwatch;
13
14
/**
15
 * This factory is used as a replacement for Http\Client\Common\PluginClientFactory when profiling is enabled. It
16
 * creates PluginClient instances with all profiling decorators and extra plugins.
17
 *
18
 * @author Fabien Bourigault <[email protected]>
19
 *
20
 * @internal
21
 */
22
final class PluginClientFactory
23
{
24
    /**
25
     * @var Collector
26
     */
27
    private $collector;
28
29
    /**
30
     * @var Formatter
31
     */
32
    private $formatter;
33
34
    /**
35
     * @var Stopwatch
36
     */
37
    private $stopwatch;
38
39 8
    public function __construct(Collector $collector, Formatter $formatter, Stopwatch $stopwatch)
40
    {
41 8
        $this->collector = $collector;
42 8
        $this->formatter = $formatter;
43 8
        $this->stopwatch = $stopwatch;
44 8
    }
45
46
    /**
47
     * @param HttpClient|ClientInterface|HttpAsyncClient $client
48
     * @param Plugin[]                                   $plugins
49
     * @param array                                      $options {
50
     *
51
     *     @var string $client_name to give client a name which may be used when displaying client information like in
52
     *         the HTTPlugBundle profiler.
53
     * }
54
     *
55
     * @see PluginClient constructor for PluginClient specific $options.
56
     *
57
     * @return PluginClient
58
     */
59 5
    public function createClient($client, array $plugins = [], array $options = [])
60
    {
61
        $plugins = array_map(function (Plugin $plugin) {
62 5
            return new ProfilePlugin($plugin, $this->collector, $this->formatter);
63 5
        }, $plugins);
64
65 5
        $clientName = isset($options['client_name']) ? $options['client_name'] : 'Default';
66 5
        array_unshift($plugins, new StackPlugin($this->collector, $this->formatter, $clientName));
67 5
        unset($options['client_name']);
68
69 5
        if (!$client instanceof ProfileClient) {
70 2
            $client = new ProfileClient($client, $this->collector, $this->formatter, $this->stopwatch);
0 ignored issues
show
Bug introduced by
It seems like $client can also be of type object<Psr\Http\Client\ClientInterface>; however, Http\HttplugBundle\Colle...leClient::__construct() does only seem to accept object<Http\Client\HttpC...Client\HttpAsyncClient>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
71
        }
72
73 5
        return new PluginClient($client, $plugins, $options);
74
    }
75
}
76