Completed
Push — master ( e37e46...c75450 )
by David
01:07
created

src/PluginClientFactory.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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(ClientInterface|HttpAsyncClient, Plugin[], array): PluginClient)|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
     * @param callable(ClientInterface|HttpAsyncClient, Plugin[], array): PluginClient $factory
33
     */
34
    public static function setFactory(callable $factory): void
35
    {
36
        static::$factory = $factory;
37
    }
38
39
    /**
40
     * @param ClientInterface|HttpAsyncClient $client
41
     * @param Plugin[]                        $plugins
42
     * @param array                           $options {
43
     *
44
     *     @var string $client_name to give client a name which may be used when displaying client information  like in
45
     *         the HTTPlugBundle profiler.
46
     * }
47
     *
48
     * @see PluginClient constructor for PluginClient specific $options.
49
     */
50 2
    public function createClient($client, array $plugins = [], array $options = []): PluginClient
51
    {
52 2 View Code Duplication
        if (!$client instanceof ClientInterface && !$client instanceof HttpAsyncClient) {
0 ignored issues
show
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...
53
            throw new \TypeError(
54
                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
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...
55
            );
56
        }
57
58 2
        if (static::$factory) {
59
            $factory = static::$factory;
60
61
            return $factory($client, $plugins, $options);
62
        }
63
64 2
        unset($options['client_name']);
65
66 2
        return new PluginClient($client, $plugins, $options);
67
    }
68
}
69