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 |
||
19 | class Request implements RequestInterface |
||
20 | { |
||
21 | protected $url; |
||
22 | protected $post = []; |
||
23 | protected $startTime; |
||
24 | protected $endTime; |
||
25 | protected $result; |
||
26 | protected $listeners = []; |
||
27 | protected $timeout; |
||
28 | protected $curlHandle; |
||
29 | protected $headers = []; |
||
30 | protected $options = []; |
||
31 | protected $success; |
||
32 | protected $response; |
||
33 | |||
34 | // TODO: make __clone() |
||
|
|||
35 | |||
36 | /** |
||
37 | * Camelizes a string |
||
38 | * @param string $str string to camelize |
||
39 | * @return string camelized string |
||
40 | */ |
||
41 | public static function camelize($str) |
||
45 | |||
46 | /** |
||
47 | * Magic setter function |
||
48 | * @param string $name attribute to set |
||
49 | * @param mixed $value the new value |
||
50 | * @return void |
||
51 | */ |
||
52 | View Code Duplication | public function __set($name, $value) |
|
62 | |||
63 | /** |
||
64 | * Magic getter function |
||
65 | * @param string $name of the attribute to get |
||
66 | * @return mixed the attribute's value |
||
67 | */ |
||
68 | View Code Duplication | public function __get($name) |
|
78 | |||
79 | /** |
||
80 | * Request constructor. |
||
81 | * @param string $url optional url |
||
82 | */ |
||
83 | public function __construct($url = null) |
||
91 | |||
92 | public function __destruct() |
||
98 | |||
99 | /** |
||
100 | * Normalize an array |
||
101 | * change from ['key' => 'value'] format to ['key: value'] |
||
102 | * @param array $array array to normalize |
||
103 | * @return array normalized array |
||
104 | */ |
||
105 | protected function normalize(array $array) |
||
117 | |||
118 | /** |
||
119 | * Setter for the url field |
||
120 | * @param string $url url |
||
121 | * @return void |
||
122 | */ |
||
123 | public function setUrl($url) |
||
129 | |||
130 | /** |
||
131 | * Getter for url field |
||
132 | * @return string url |
||
133 | */ |
||
134 | public function getUrl() |
||
138 | |||
139 | /** |
||
140 | * Setter for the post data array |
||
141 | * @param array $postData post data |
||
142 | * @return void |
||
143 | */ |
||
144 | public function setPostData(array $postData) |
||
152 | |||
153 | /** |
||
154 | * Getter for the post data array |
||
155 | * @return array post data |
||
156 | */ |
||
157 | public function getPostData() |
||
161 | |||
162 | /** |
||
163 | * Returns the time (msec) it took to make the request |
||
164 | * @return float time |
||
165 | */ |
||
166 | public function getTime() |
||
172 | |||
173 | /** |
||
174 | * Start the request's internal timer |
||
175 | * @return void |
||
176 | */ |
||
177 | public function startTimer() |
||
181 | |||
182 | /** |
||
183 | * Stops the request's internal timer |
||
184 | * @return void |
||
185 | */ |
||
186 | public function stopTimer() |
||
190 | |||
191 | /** |
||
192 | * Get the result of a query |
||
193 | * @return mixed result |
||
194 | */ |
||
195 | public function getResult() |
||
199 | |||
200 | /** |
||
201 | * This gets called by an agent when a request has completed |
||
202 | * @param mixed $multiInfo result |
||
203 | * @return void |
||
204 | */ |
||
205 | public function callBack($mutliInfo) |
||
224 | |||
225 | /** |
||
226 | * Add a listener that gets notified when the Request has completed |
||
227 | * @param callable $function callback function |
||
228 | * @return void |
||
229 | */ |
||
230 | public function addListener(callable $function) |
||
236 | |||
237 | /** |
||
238 | * Notify all listeners of request completion |
||
239 | * @return void |
||
240 | */ |
||
241 | protected function notify() |
||
247 | |||
248 | /** |
||
249 | * Set a timeout value for the request |
||
250 | * @param float $timeout timeout (msec) |
||
251 | * @return void |
||
252 | */ |
||
253 | public function setTimeout($timeout) |
||
260 | |||
261 | /** |
||
262 | * Get the timeout value registered for the request |
||
263 | * @return float timeout |
||
264 | */ |
||
265 | public function getTimeout() |
||
269 | |||
270 | /** |
||
271 | * Get the cUrl handle for the request |
||
272 | * @return resource cUrl handle |
||
273 | */ |
||
274 | public function getHandle() |
||
283 | |||
284 | /** |
||
285 | * Add headers to the request |
||
286 | * @param array $headers headers in ['key' => 'value] or ['key: value'] format |
||
287 | * @return void |
||
288 | */ |
||
289 | public function setHeaders(array $headers) |
||
294 | |||
295 | /** |
||
296 | * Get headers set for the request |
||
297 | * @return array headers in ['key' => 'value'] format |
||
298 | */ |
||
299 | public function getHeaders() |
||
303 | |||
304 | /** |
||
305 | * Add cUrl options to the request |
||
306 | * @param array $options options in ['key' => 'value'] format |
||
307 | * @return void |
||
308 | */ |
||
309 | public function setOptions(array $options) |
||
313 | |||
314 | /** |
||
315 | * Get cUrl options set for the request |
||
316 | * @return array options in ['key' => 'value'] format |
||
317 | */ |
||
318 | public function getOptions() |
||
322 | |||
323 | /** |
||
324 | * Get the response for the finished query |
||
325 | * @return mixed response |
||
326 | */ |
||
327 | public function getResponse() |
||
331 | } |
||
332 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.