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\Common\Exception\HttpClientNoMatchException; |
||
8 | use Http\Client\HttpAsyncClient; |
||
9 | use Http\Message\RequestMatcher; |
||
10 | use Psr\Http\Client\ClientInterface; |
||
11 | use Psr\Http\Message\RequestInterface; |
||
12 | use Psr\Http\Message\ResponseInterface; |
||
13 | |||
14 | /** |
||
15 | * {@inheritdoc} |
||
16 | * |
||
17 | * @author Joel Wurtz <[email protected]> |
||
18 | */ |
||
19 | final class HttpClientRouter implements HttpClientRouterInterface |
||
20 | { |
||
21 | /** |
||
22 | * @var (array{matcher: RequestMatcher, client: FlexibleHttpClient})[] |
||
23 | */ |
||
24 | private $clients = []; |
||
25 | |||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | */ |
||
29 | 2 | public function sendRequest(RequestInterface $request): ResponseInterface |
|
30 | { |
||
31 | 2 | return $this->chooseHttpClient($request)->sendRequest($request); |
|
32 | } |
||
33 | |||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | */ |
||
37 | 2 | public function sendAsyncRequest(RequestInterface $request) |
|
38 | { |
||
39 | 2 | return $this->chooseHttpClient($request)->sendAsyncRequest($request); |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * Add a client to the router. |
||
44 | * |
||
45 | * @param ClientInterface|HttpAsyncClient $client |
||
46 | */ |
||
47 | 4 | public function addClient($client, RequestMatcher $requestMatcher): void |
|
48 | { |
||
49 | 4 | View Code Duplication | if (!$client instanceof ClientInterface && !$client instanceof HttpAsyncClient) { |
0 ignored issues
–
show
|
|||
50 | throw new \TypeError( |
||
51 | sprintf('%s::addClient(): 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::addClient()...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
Loading history...
|
|||
52 | ); |
||
53 | } |
||
54 | |||
55 | 4 | $this->clients[] = [ |
|
56 | 4 | 'matcher' => $requestMatcher, |
|
57 | 4 | 'client' => new FlexibleHttpClient($client), |
|
58 | ]; |
||
59 | 4 | } |
|
60 | |||
61 | /** |
||
62 | * Choose an HTTP client given a specific request. |
||
63 | */ |
||
64 | 4 | private function chooseHttpClient(RequestInterface $request): FlexibleHttpClient |
|
65 | { |
||
66 | 4 | foreach ($this->clients as $client) { |
|
67 | 4 | if ($client['matcher']->matches($request)) { |
|
68 | 2 | return $client['client']; |
|
69 | } |
||
70 | } |
||
71 | |||
72 | 2 | throw new HttpClientNoMatchException('No client found for the specified request', $request); |
|
73 | } |
||
74 | } |
||
75 |
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.