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 |
||
24 | class ProfileClient implements HttpClient, HttpAsyncClient |
||
25 | { |
||
26 | use VersionBridgeClient; |
||
27 | |||
28 | /** |
||
29 | * @var HttpClient|HttpAsyncClient |
||
30 | */ |
||
31 | private $client; |
||
32 | |||
33 | /** |
||
34 | * @var Collector |
||
35 | */ |
||
36 | private $collector; |
||
37 | |||
38 | /** |
||
39 | * @var Formatter |
||
40 | */ |
||
41 | private $formatter; |
||
42 | |||
43 | /** |
||
44 | * @var Stopwatch |
||
45 | */ |
||
46 | private $stopwatch; |
||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | private $eventNames = []; |
||
52 | |||
53 | /** |
||
54 | * @param HttpClient|HttpAsyncClient $client The client to profile. Client must implement HttpClient or |
||
55 | * HttpAsyncClient interface. |
||
56 | * @param Collector $collector |
||
57 | * @param Formatter $formatter |
||
58 | * @param Stopwatch $stopwatch |
||
59 | */ |
||
60 | 21 | public function __construct($client, Collector $collector, Formatter $formatter, Stopwatch $stopwatch) |
|
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | 3 | public function sendAsyncRequest(RequestInterface $request) |
|
120 | |||
121 | 5 | protected function doSendRequest(RequestInterface $request) |
|
122 | { |
||
123 | 5 | $stack = $this->collector->getActiveStack(); |
|
124 | 5 | if (null === $stack) { |
|
125 | //When using a discovered client not wrapped in a PluginClient, we don't have a stack from StackPlugin. So |
||
126 | //we create our own stack but don't activate it. |
||
127 | $stack = new Stack('Default', $this->formatter->formatRequest($request)); |
||
128 | $this->collector->addStack($stack); |
||
129 | } |
||
130 | |||
131 | 5 | $this->collectRequestInformations($request, $stack); |
|
132 | 5 | $event = $this->stopwatch->start($this->getStopwatchEventName($request)); |
|
133 | |||
134 | try { |
||
135 | 5 | $response = $this->client->sendRequest($request); |
|
136 | 4 | $this->collectResponseInformations($response, $event, $stack); |
|
137 | |||
138 | 4 | return $response; |
|
139 | 1 | } catch (\Exception $e) { |
|
140 | $this->collectExceptionInformations($e, $event, $stack); |
||
141 | |||
142 | throw $e; |
||
143 | 1 | } catch (\Throwable $e) { |
|
144 | 1 | $this->collectExceptionInformations($e, $event, $stack); |
|
145 | |||
146 | 1 | throw $e; |
|
147 | } finally { |
||
148 | 5 | $event->stop(); |
|
149 | } |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param RequestInterface $request |
||
154 | * @param Stack $stack |
||
155 | */ |
||
156 | 8 | private function collectRequestInformations(RequestInterface $request, Stack $stack) |
|
165 | |||
166 | /** |
||
167 | * @param ResponseInterface $response |
||
168 | * @param StopwatchEvent $event |
||
169 | * @param Stack $stack |
||
170 | */ |
||
171 | 6 | private function collectResponseInformations(ResponseInterface $response, StopwatchEvent $event, Stack $stack) |
|
177 | |||
178 | /** |
||
179 | * @param \Throwable $exception |
||
180 | * @param StopwatchEvent $event |
||
181 | * @param Stack $stack |
||
182 | */ |
||
183 | 2 | private function collectExceptionInformations(\Throwable $exception, StopwatchEvent $event, Stack $stack) |
|
192 | |||
193 | /** |
||
194 | * Generates the event name. |
||
195 | * |
||
196 | * @param RequestInterface $request |
||
197 | * |
||
198 | * @return string |
||
199 | */ |
||
200 | 8 | private function getStopwatchEventName(RequestInterface $request) |
|
212 | } |
||
213 |
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.