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:
Complex classes like ConsoleRequest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ConsoleRequest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class ConsoleRequest |
||
9 | { |
||
10 | /** |
||
11 | * @var ApiHandlerInterface |
||
12 | */ |
||
13 | private $handler; |
||
14 | |||
15 | /** |
||
16 | * Create ConsoleRequest |
||
17 | * |
||
18 | * @param ApiHandlerInterface $handler |
||
19 | */ |
||
20 | 3 | public function __construct(ApiHandlerInterface $handler) |
|
24 | |||
25 | /** |
||
26 | * Make request to API url |
||
27 | * |
||
28 | * @param string $url |
||
29 | * @param string $method |
||
30 | * @param array $values |
||
31 | * @param array $additionalValues |
||
32 | * @param string|null $token |
||
33 | * |
||
34 | * @return ConsoleResponse |
||
35 | */ |
||
36 | 3 | public function makeRequest($url, $method, array $values, array $additionalValues = [], $token = null) |
|
37 | { |
||
38 | 3 | list($postFields, $getFields, $cookieFields, $rawPost, $putFields) = $this->processValues($values); |
|
39 | |||
40 | 3 | if (isset($additionalValues['postFields'])) { |
|
41 | $postFields = array_merge($postFields, $additionalValues['postFields']); |
||
42 | } |
||
43 | |||
44 | 3 | if (isset($additionalValues['getFields'])) { |
|
45 | $getFields = array_merge($postFields, $additionalValues['getFields']); |
||
46 | } |
||
47 | |||
48 | 3 | if (isset($additionalValues['cookieFields'])) { |
|
49 | $cookieFields = array_merge($postFields, $additionalValues['cookieFields']); |
||
50 | } |
||
51 | |||
52 | 3 | if (isset($additionalValues['putFields'])) { |
|
53 | $putFields = array_merge($putFields, $additionalValues['putFields']); |
||
54 | } |
||
55 | |||
56 | 3 | $postFields = $this->normalizeValues($postFields); |
|
57 | 3 | $getFields = $this->normalizeValues($getFields); |
|
58 | 3 | $putFields = $this->normalizeValues($putFields); |
|
59 | |||
60 | 3 | View Code Duplication | if (count($getFields)) { |
|
|||
61 | 3 | $parts = []; |
|
62 | 3 | foreach ($getFields as $key => $value) { |
|
63 | 3 | $parts[] = "$key=$value"; |
|
64 | 1 | } |
|
65 | 3 | $url = $url . '?' . implode('&', $parts); |
|
66 | 1 | } |
|
67 | |||
68 | 3 | $putRawPost = null; |
|
69 | 3 | View Code Duplication | if (count($putFields)) { |
70 | $parts = []; |
||
71 | foreach ($putFields as $key => $value) { |
||
72 | $parts[] = "$key=$value"; |
||
73 | } |
||
74 | $putRawPost = implode('&', $parts); |
||
75 | } |
||
76 | |||
77 | 3 | $startTime = microtime(true); |
|
78 | |||
79 | 3 | $curl = curl_init(); |
|
80 | 3 | curl_setopt($curl, CURLOPT_URL, $url); |
|
81 | 3 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); |
|
82 | 3 | curl_setopt($curl, CURLOPT_NOBODY, false); |
|
83 | 3 | curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); |
|
84 | 3 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
85 | 3 | curl_setopt($curl, CURLOPT_VERBOSE, false); |
|
86 | 3 | curl_setopt($curl, CURLOPT_TIMEOUT, 10); |
|
87 | 3 | curl_setopt($curl, CURLOPT_HEADER, true); |
|
88 | 3 | if (count($postFields)) { |
|
89 | curl_setopt($curl, CURLOPT_POST, true); |
||
90 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields); |
||
91 | } |
||
92 | 3 | if ($rawPost) { |
|
93 | curl_setopt($curl, CURLOPT_POST, true); |
||
94 | curl_setopt($curl, CURLOPT_POSTFIELDS, $rawPost); |
||
95 | } |
||
96 | 3 | if ($putRawPost) { |
|
97 | curl_setopt($curl, CURLOPT_POST, true); |
||
98 | curl_setopt($curl, CURLOPT_POSTFIELDS, $putRawPost); |
||
99 | } |
||
100 | 3 | if (count($cookieFields)) { |
|
101 | $parts = []; |
||
102 | foreach ($cookieFields as $key => $value) { |
||
103 | $parts[] = "$key=$value"; |
||
104 | } |
||
105 | curl_setopt($curl, CURLOPT_HTTPHEADER, ["Cookie: " . implode('&', $parts)]); |
||
106 | } |
||
107 | |||
108 | 3 | curl_setopt($curl, CURLOPT_TIMEOUT, 30); |
|
109 | 3 | $headers = []; |
|
110 | 3 | if ($token !== null && $token !== false) { |
|
111 | $headers = ['Authorization: Bearer ' . $token]; |
||
112 | curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); |
||
113 | } |
||
114 | |||
115 | 3 | $consoleResponse = new ConsoleResponse( |
|
116 | 3 | $url, |
|
117 | 3 | $method, |
|
118 | 3 | $postFields, |
|
119 | 3 | $getFields, |
|
120 | 3 | $cookieFields, |
|
121 | 3 | $headers, |
|
122 | 2 | $rawPost |
|
123 | 1 | ); |
|
124 | |||
125 | 3 | $response = curl_exec($curl); |
|
126 | 3 | $elapsed = intval((microtime(true) - $startTime) * 1000); |
|
127 | |||
128 | 3 | $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); |
|
129 | 3 | $responseHeaders = substr($response, 0, $headerSize); |
|
130 | 3 | $responseBody = substr($response, $headerSize); |
|
131 | |||
132 | 3 | $curlErrorNumber = curl_errno($curl); |
|
133 | 3 | $curlError = curl_error($curl); |
|
134 | 3 | if ($curlErrorNumber > 0) { |
|
135 | 3 | $consoleResponse->logError($curlErrorNumber, $curlError, $elapsed); |
|
136 | 1 | } else { |
|
137 | $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||
138 | $consoleResponse->logRequest($httpCode, $responseBody, $responseHeaders, $elapsed); |
||
139 | } |
||
140 | |||
141 | 3 | return $consoleResponse; |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * Process given values to POST and GET fields |
||
146 | * |
||
147 | * @param array $values |
||
148 | * |
||
149 | * @return array |
||
150 | */ |
||
151 | 3 | private function processValues(array $values) |
|
199 | |||
200 | /** |
||
201 | * Process one param and returns value |
||
202 | * |
||
203 | * @param InputParam $param input param |
||
204 | * @param string $key param key |
||
205 | * @param string $value actual value from request |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | 3 | private function processParam(InputParam $param, $key, $value) |
|
234 | |||
235 | /** |
||
236 | * Normalize values array. |
||
237 | * |
||
238 | * @param $values |
||
239 | * @return array |
||
240 | */ |
||
241 | 3 | private function normalizeValues($values) |
|
258 | } |
||
259 |
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.