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 declare(strict_types=1); |
||
18 | final class Client implements ClientInterface |
||
19 | { |
||
20 | const DEFAULT_OPTIONS = [ |
||
21 | Options::SCHEMA => 'https', |
||
22 | Options::PATH => '/', |
||
23 | Options::HEADERS => [], |
||
24 | ]; |
||
25 | |||
26 | /** |
||
27 | * @var LoopInterface |
||
28 | */ |
||
29 | protected $loop; |
||
30 | |||
31 | /** |
||
32 | * @var Locator |
||
33 | */ |
||
34 | protected $locator; |
||
35 | |||
36 | /** |
||
37 | * @var Browser |
||
38 | */ |
||
39 | protected $browser; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $options = []; |
||
45 | |||
46 | /** |
||
47 | * @var string[] |
||
48 | */ |
||
49 | protected $middleware = []; |
||
50 | |||
51 | /** |
||
52 | * @param LoopInterface $loop |
||
53 | * @param Locator $locator |
||
54 | * @param Browser $buzz |
||
55 | * @param array $options |
||
56 | */ |
||
57 | 18 | public function __construct( |
|
72 | |||
73 | /** |
||
74 | * @param RequestInterface $request |
||
75 | * @param array $options |
||
76 | * @return PromiseInterface |
||
77 | */ |
||
78 | 17 | public function request(RequestInterface $request, array $options = []): PromiseInterface |
|
79 | { |
||
80 | 17 | $body = $request->getBody(); |
|
81 | 17 | if ($body instanceof ReadableStreamInterface) { |
|
82 | 1 | $body->pause(); |
|
83 | } |
||
84 | |||
85 | 17 | $options = $this->applyRequestOptions($options); |
|
86 | 17 | $request = $this->applyApiSettingsToRequest($request, $options); |
|
87 | 17 | $executioner = $this->constructMiddlewares($options); |
|
88 | |||
89 | 17 | View Code Duplication | return $executioner->pre($request)->then(function (RequestInterface $request) use ($options) { |
|
|||
90 | 17 | $body = $request->getBody(); |
|
91 | 17 | if ($body instanceof ReadableStreamInterface) { |
|
92 | 1 | $this->loop->futureTick(function () use ($body) { |
|
93 | 1 | $body->resume(); |
|
94 | 1 | }); |
|
95 | } |
||
96 | |||
97 | 17 | return resolve($this->browser->send( |
|
98 | 17 | $request |
|
99 | )); |
||
100 | }, function (ResponseInterface $response) { |
||
101 | return resolve($response); |
||
102 | })->then(function (ResponseInterface $response) use ($executioner) { |
||
103 | 8 | $body = $response->getBody(); |
|
104 | 8 | if ($body instanceof ReadableStreamInterface) { |
|
105 | $body->pause(); |
||
106 | } |
||
107 | |||
108 | 8 | return $executioner->post($response); |
|
109 | View Code Duplication | })->then(function (ResponseInterface $response) { |
|
110 | 8 | $body = $response->getBody(); |
|
111 | 8 | if ($body instanceof ReadableStreamInterface) { |
|
112 | $this->loop->futureTick(function () use ($body) { |
||
113 | $body->resume(); |
||
114 | }); |
||
115 | } |
||
116 | |||
117 | 8 | return resolve($response); |
|
118 | })->otherwise(function (Throwable $throwable) use ($executioner) { |
||
119 | 9 | return reject($executioner->error($throwable)); |
|
120 | 17 | }); |
|
121 | } |
||
122 | |||
123 | 17 | public function applyRequestOptions(array $options): array |
|
134 | |||
135 | 17 | protected function constructMiddlewares(array $options): MiddlewareRunner |
|
151 | |||
152 | protected function combinedMiddlewares(array $extraMiddlewares): array |
||
166 | |||
167 | 17 | protected function applyApiSettingsToRequest(RequestInterface $request, array $options): RequestInterface |
|
190 | } |
||
191 |
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.