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 |
||
29 | class RequestFactory |
||
30 | { |
||
31 | /** |
||
32 | * |
||
33 | * @param RequestInterface $request |
||
34 | * @param array $options |
||
35 | * @param HttpClient $httpClient |
||
36 | * @param LoopInterface $loop |
||
37 | * @return \React\Promise\Promise |
||
38 | */ |
||
39 | public function create(RequestInterface $request, array $options, HttpClient $httpClient, LoopInterface $loop) |
||
48 | |||
49 | protected function createSender(array $options, HttpClient $httpClient, LoopInterface $loop) |
||
50 | { |
||
51 | $connector = $this->extractConnector($httpClient); |
||
52 | |||
53 | if (isset($options['proxy'])) { |
||
54 | $resolver = $this->extractResolver($connector); |
||
55 | $connector = (new SocksClient($options['proxy'], $loop, $connector, $resolver))->createConnector(); |
||
56 | } |
||
57 | |||
58 | return Sender::createFromLoopConnectors($loop, $connector); |
||
59 | } |
||
60 | |||
61 | protected function convertOptions(array $options) |
||
65 | |||
66 | protected function extractConnector(HttpClient $httpClient) |
||
73 | |||
74 | protected function extractResolver(ConnectorInterface $connector) |
||
92 | } |
||
93 |
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.