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 Request 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 Request, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class Request implements RequestInterface |
||
21 | { |
||
22 | protected $url; |
||
23 | protected $post = []; |
||
24 | protected $result; |
||
25 | protected $listeners = []; |
||
26 | protected $timeout; |
||
27 | protected $curlHandle; |
||
28 | protected $headers = []; |
||
29 | protected $options = []; |
||
30 | protected $response; |
||
31 | protected $httpCode; |
||
32 | |||
33 | /** |
||
34 | * Camelizes a string |
||
35 | * @param string $str string to camelize |
||
36 | * @return string camelized string |
||
37 | */ |
||
38 | public static function camelize($str) |
||
42 | |||
43 | /** |
||
44 | * Magic setter function |
||
45 | * @param string $name attribute to set |
||
46 | * @param mixed $value the new value |
||
47 | * @return void |
||
48 | */ |
||
49 | View Code Duplication | public function __set($name, $value) |
|
59 | |||
60 | /** |
||
61 | * Magic getter function |
||
62 | * @param string $name of the attribute to get |
||
63 | * @return mixed the attribute's value |
||
64 | */ |
||
65 | View Code Duplication | public function __get($name) |
|
75 | |||
76 | /** |
||
77 | * Request constructor. |
||
78 | * @param string $url optional url |
||
79 | */ |
||
80 | public function __construct($url = null) |
||
89 | |||
90 | /** |
||
91 | * Destructor |
||
92 | */ |
||
93 | public function __destruct() |
||
99 | |||
100 | /** |
||
101 | * Clone the object |
||
102 | * @return void |
||
103 | */ |
||
104 | public function __clone() |
||
110 | |||
111 | /** |
||
112 | * Normalize an array |
||
113 | * change from ['key' => 'value'] format to ['key: value'] |
||
114 | * @param array $array array to normalize |
||
115 | * @return array normalized array |
||
116 | */ |
||
117 | protected function normalize(array $array) |
||
129 | |||
130 | /** |
||
131 | * Setter for the url field |
||
132 | * @param string $url url |
||
133 | * @return void |
||
134 | */ |
||
135 | public function setUrl($url) |
||
141 | |||
142 | /** |
||
143 | * Getter for url field |
||
144 | * @return string url |
||
145 | */ |
||
146 | public function getUrl() |
||
150 | |||
151 | /** |
||
152 | * Setter for the post data array |
||
153 | * @param array $postData post data |
||
154 | * @return void |
||
155 | */ |
||
156 | public function setPostData(array $postData) |
||
164 | |||
165 | /** |
||
166 | * Getter for the post data array |
||
167 | * @return array post data |
||
168 | */ |
||
169 | public function getPostData() |
||
173 | |||
174 | /** |
||
175 | * Returns the time (msec) it took to make the request |
||
176 | * @return float time |
||
177 | */ |
||
178 | public function getTime() |
||
185 | |||
186 | /** |
||
187 | * Get the result of a query |
||
188 | * @return mixed result |
||
189 | */ |
||
190 | public function getResult() |
||
194 | |||
195 | /** |
||
196 | * This gets called by an agent when a request has completed |
||
197 | * @param array $multiInfo result |
||
198 | * @return void |
||
199 | */ |
||
200 | public function callBack(array $multiInfo) |
||
204 | |||
205 | /** |
||
206 | * Add a listener that gets notified when the Request has completed |
||
207 | * @param callable $function callback function |
||
208 | * @return void |
||
209 | */ |
||
210 | public function addListener(callable $function) |
||
218 | |||
219 | /** |
||
220 | * Notify all listeners of request completion |
||
221 | * @return void |
||
222 | */ |
||
223 | protected function notify() |
||
229 | |||
230 | /** |
||
231 | * Set a timeout value for the request |
||
232 | * @param float $timeout timeout (msec) |
||
233 | * @return void |
||
234 | */ |
||
235 | public function setTimeout($timeout) |
||
242 | |||
243 | /** |
||
244 | * Get the timeout value registered for the request |
||
245 | * @return float timeout |
||
246 | */ |
||
247 | public function getTimeout() |
||
251 | |||
252 | /** |
||
253 | * Get the cUrl handle for the request |
||
254 | * @return resource cUrl handle |
||
255 | */ |
||
256 | public function getHandle() |
||
264 | |||
265 | /** |
||
266 | * Add headers to the request |
||
267 | * @param array $headers headers in ['key' => 'value] or ['key: value'] format |
||
268 | * @return void |
||
269 | */ |
||
270 | public function setHeaders(array $headers) |
||
275 | |||
276 | /** |
||
277 | * Get headers set for the request |
||
278 | * @return array headers in ['key' => 'value'] format |
||
279 | */ |
||
280 | public function getHeaders() |
||
284 | |||
285 | /** |
||
286 | * Add cUrl options to the request |
||
287 | * @param array $options options in ['key' => 'value'] format |
||
288 | * @return void |
||
289 | */ |
||
290 | public function setOptions(array $options) |
||
294 | |||
295 | /** |
||
296 | * Get cUrl options set for the request |
||
297 | * @return array options in ['key' => 'value'] format |
||
298 | */ |
||
299 | public function getOptions() |
||
303 | |||
304 | /** |
||
305 | * Get the response for the finished query |
||
306 | * @return mixed response |
||
307 | */ |
||
308 | public function getResponse() |
||
317 | |||
318 | /** |
||
319 | * Get the Http code of the response |
||
320 | * @return mixed |
||
321 | */ |
||
322 | public function getHttpCode() |
||
331 | } |
||
332 |
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.