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 |
||
20 | class Guzzle6Adapter implements AdapterInterface |
||
21 | { |
||
22 | /** |
||
23 | * @var Client |
||
24 | */ |
||
25 | protected $client; |
||
26 | |||
27 | protected $logger; |
||
28 | |||
29 | 73 | public function __construct(array $options = []) |
|
30 | { |
||
31 | 73 | $options = array_merge( |
|
32 | [ |
||
33 | 73 | 'allow_redirects' => false, |
|
34 | 'verify' => true, |
||
35 | 'timeout' => 60, |
||
36 | 'connect_timeout' => 10, |
||
37 | 'pool_size' => 25 |
||
38 | ], |
||
39 | $options |
||
40 | ); |
||
41 | 73 | $this->client = new Client($options); |
|
42 | 73 | } |
|
43 | |||
44 | 12 | public function setLogger(LoggerInterface $logger) |
|
49 | |||
50 | /** |
||
51 | * @param RequestInterface $request |
||
52 | * @return ResponseInterface |
||
53 | */ |
||
54 | 352 | public function execute(RequestInterface $request) |
|
55 | { |
||
56 | try { |
||
57 | 352 | $response = $this->client->send($request); |
|
58 | 49 | } catch (RequestException $exception) { |
|
59 | 49 | $response = $exception->getResponse(); |
|
60 | 49 | throw ApiException::create($request, $response, $exception); |
|
61 | } |
||
62 | |||
63 | 329 | return $response; |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param RequestInterface[] $requests |
||
68 | * @return ResponseInterface[] |
||
69 | */ |
||
70 | 234 | public function executeBatch(array $requests) |
|
71 | { |
||
72 | 234 | $results = Pool::batch( |
|
73 | 234 | $this->client, |
|
74 | $requests |
||
75 | ); |
||
76 | |||
77 | 234 | $responses = []; |
|
78 | 234 | foreach ($results as $key => $result) { |
|
79 | 234 | $httpResponse = $result; |
|
80 | 234 | View Code Duplication | if ($result instanceof RequestException) { |
|
|||
81 | 7 | $request = $requests[$key]; |
|
82 | 7 | $httpResponse = $result->getResponse(); |
|
83 | 7 | $httpResponse = ApiException::create($request, $httpResponse, $result); |
|
84 | } |
||
85 | 234 | $responses[$key] = $httpResponse; |
|
86 | } |
||
87 | |||
88 | 234 | return $responses; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param $oauthUri |
||
93 | * @param $clientId |
||
94 | * @param $clientSecret |
||
95 | * @param $formParams |
||
96 | * @return ResponseInterface |
||
97 | */ |
||
98 | 12 | public function authenticate($oauthUri, $clientId, $clientSecret, $formParams) |
|
99 | { |
||
100 | $options = [ |
||
101 | 12 | 'form_params' => $formParams, |
|
102 | 12 | 'auth' => [$clientId, $clientSecret] |
|
103 | ]; |
||
104 | |||
105 | try { |
||
106 | 12 | $response = $this->client->post($oauthUri, $options); |
|
107 | 1 | } catch (RequestException $exception) { |
|
108 | 1 | throw ApiException::create($exception->getRequest(), $exception->getResponse(), $exception); |
|
109 | } |
||
110 | 11 | return $response; |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * @param RequestInterface $request |
||
115 | * @return AdapterPromiseInterface |
||
116 | */ |
||
117 | 4 | public function executeAsync(RequestInterface $request) |
|
123 | } |
||
124 |
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.