Completed
Push — master ( cd17f3...38b66e )
by David
01:50 queued 12s
created

PluginClientFactory::createClient()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5.4042

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 5
cts 9
cp 0.5556
rs 9.6666
c 0
b 0
f 0
cc 4
nc 3
nop 3
crap 5.4042
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Http\Client\Common;
6
7
use Http\Client\HttpAsyncClient;
8
use Psr\Http\Client\ClientInterface;
9
10
/**
11
 * Factory to create PluginClient instances. Using this factory instead of calling PluginClient constructor will enable
12
 * the Symfony profiling without any configuration.
13
 *
14
 * @author Fabien Bourigault <[email protected]>
15
 */
16
final class PluginClientFactory
17
{
18
    /**
19
     * @var callable|null
20
     */
21
    private static $factory;
22
23
    /**
24
     * Set the factory to use.
25
     * The callable to provide must have the same arguments and return type as PluginClientFactory::createClient.
26
     * This is used by the HTTPlugBundle to provide a better Symfony integration.
27
     * Unlike the createClient method, this one is static to allow zero configuration profiling by hooking into early
28
     * application execution.
29
     *
30
     * @internal
31
     */
32
    public static function setFactory(callable $factory)
33
    {
34
        static::$factory = $factory;
0 ignored issues
show
Comprehensibility introduced by
Since Http\Client\Common\PluginClientFactory is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
35
    }
36
37
    /**
38
     * @param ClientInterface|HttpAsyncClient $client
39
     * @param Plugin[]                        $plugins
40
     * @param array                           $options {
41
     *
42
     *     @var string $client_name to give client a name which may be used when displaying client information  like in
43
     *         the HTTPlugBundle profiler.
44
     * }
45
     *
46
     * @see PluginClient constructor for PluginClient specific $options.
47
     */
48 2
    public function createClient($client, array $plugins = [], array $options = []): PluginClient
49
    {
50 2
        if (!$client instanceof ClientInterface && !$client instanceof HttpAsyncClient) {
51
            throw new \TypeError(
52
                sprintf('%s::createClient(): Argument #1 ($client) must be of type %s|%s, %s given', self::class, ClientInterface::class, HttpAsyncClient::class, get_debug_type($client))
0 ignored issues
show
Unused Code introduced by
The call to TypeError::__construct() has too many arguments starting with sprintf('%s::createClien...et_debug_type($client)).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
53
            );
54
        }
55
56 2
        if (static::$factory) {
0 ignored issues
show
Comprehensibility introduced by
Since Http\Client\Common\PluginClientFactory is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
57
            $factory = static::$factory;
0 ignored issues
show
Comprehensibility introduced by
Since Http\Client\Common\PluginClientFactory is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
58
59
            return $factory($client, $plugins, $options);
60
        }
61
62 2
        unset($options['client_name']);
63
64 2
        return new PluginClient($client, $plugins, $options);
65
    }
66
}
67