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 PacktpublrBase 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 PacktpublrBase, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Cornford\Packtpublr; |
||
14 | abstract class PacktpublrBase implements RequestableInterface, OutputableInterface |
||
15 | { |
||
16 | |||
17 | const REQUEST_TIMEOUT = 15; |
||
18 | |||
19 | const REQUEST_METHOD_GET = 0; |
||
20 | const REQUEST_METHOD_POST = 1; |
||
21 | |||
22 | const REQUEST_CONTENT_PLAIN = 0; |
||
23 | const REQUEST_CONTENT_XML = 1; |
||
24 | const REQUEST_CONTENT_JSON = 2; |
||
25 | |||
26 | const RESPONSE_CODE_OK = 200; |
||
27 | |||
28 | /** |
||
29 | * Guzzle Http client. |
||
30 | * |
||
31 | * @var Client |
||
32 | */ |
||
33 | protected $httpClient; |
||
34 | |||
35 | /** |
||
36 | * Guzzle cookie subscriber. |
||
37 | * |
||
38 | * @var Cookie |
||
39 | */ |
||
40 | protected $cookieSubscriber; |
||
41 | |||
42 | /** |
||
43 | * Symfony console output. |
||
44 | * |
||
45 | * @var ConsoleOutput |
||
46 | */ |
||
47 | protected $consoleOutput; |
||
48 | |||
49 | /** |
||
50 | * The request type. |
||
51 | * |
||
52 | * @var integer |
||
53 | */ |
||
54 | protected $requestType = self::REQUEST_CONTENT_PLAIN; |
||
55 | |||
56 | /** |
||
57 | * The base URL. |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $baseUrl; |
||
62 | |||
63 | /** |
||
64 | * Response body. |
||
65 | * |
||
66 | * @var string|object |
||
67 | */ |
||
68 | protected $responseBody; |
||
69 | |||
70 | /** |
||
71 | * Response code. |
||
72 | * |
||
73 | * @var integer |
||
74 | */ |
||
75 | protected $responseCode; |
||
76 | |||
77 | /** |
||
78 | * Response headers. |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | protected $responseHeaders; |
||
83 | |||
84 | /** |
||
85 | * Construct Packtpublr |
||
86 | * |
||
87 | * @param array $options |
||
88 | * |
||
89 | * @throws PacktpublrArgumentException |
||
90 | */ |
||
91 | public function __construct(array $options = []) |
||
121 | |||
122 | /** |
||
123 | * Create request. |
||
124 | * |
||
125 | * @param string $url |
||
126 | * @param array $parameters |
||
127 | * @param integer $requestMethod |
||
128 | * @param integer $requestContent |
||
129 | * |
||
130 | * @throws PacktpublrRequestException |
||
131 | * |
||
132 | * @return boolean |
||
133 | */ |
||
134 | protected function createRequest( |
||
151 | |||
152 | /** |
||
153 | * Send request. |
||
154 | * |
||
155 | * @param string $url |
||
156 | * @param array $parameters |
||
157 | * @param integer $requestMethod |
||
158 | * @param integer $requestContent |
||
159 | * |
||
160 | * @return boolean |
||
161 | */ |
||
162 | protected function sendRequest( |
||
199 | |||
200 | /** |
||
201 | * Parse response. |
||
202 | * |
||
203 | * @param ResponseInterface $response |
||
204 | * @param integer $requestContent |
||
205 | * |
||
206 | * @return boolean |
||
207 | */ |
||
208 | protected function parseResponse(ResponseInterface $response, $requestContent = self::REQUEST_CONTENT_PLAIN) |
||
231 | |||
232 | /** |
||
233 | * Get Http client. |
||
234 | * |
||
235 | * @return Client |
||
236 | */ |
||
237 | public function getHttpClient() |
||
241 | |||
242 | /** |
||
243 | * Set Http client. |
||
244 | * |
||
245 | * @param Client $client |
||
246 | * |
||
247 | * @return void |
||
248 | */ |
||
249 | public function setHttpClient(Client $client) |
||
253 | |||
254 | /** |
||
255 | * Get cookie subscriber |
||
256 | * |
||
257 | * @return Cookie |
||
258 | */ |
||
259 | public function getCookieSubscriber() |
||
263 | |||
264 | /** |
||
265 | * Set cookie subscriber. |
||
266 | * |
||
267 | * @param Cookie $cookieSubscriber |
||
268 | * |
||
269 | * @return void |
||
270 | */ |
||
271 | public function setCookieSubscriber(Cookie $cookieSubscriber) |
||
277 | |||
278 | /** |
||
279 | * Get console output. |
||
280 | * |
||
281 | * @return ConsoleOutput |
||
282 | */ |
||
283 | public function getConsoleOutput() |
||
287 | |||
288 | /** |
||
289 | * Set console output. |
||
290 | * |
||
291 | * @param ConsoleOutput $consoleOutput |
||
292 | * |
||
293 | * @return void |
||
294 | */ |
||
295 | public function setConsoleOutput(ConsoleOutput $consoleOutput) |
||
299 | |||
300 | /** |
||
301 | * Write a string as standard output. |
||
302 | * |
||
303 | * @param string $string |
||
304 | * |
||
305 | * @return void |
||
306 | */ |
||
307 | public function line($string) |
||
311 | |||
312 | /** |
||
313 | * Write a string as information output. |
||
314 | * |
||
315 | * @param string $string |
||
316 | * |
||
317 | * @return void |
||
318 | */ |
||
319 | public function info($string) |
||
323 | |||
324 | /** |
||
325 | * Write a string as success output. |
||
326 | * |
||
327 | * @param string $string |
||
328 | * |
||
329 | * @return void |
||
330 | */ |
||
331 | public function success($string) |
||
335 | |||
336 | /** |
||
337 | * Write a string as warning output. |
||
338 | * |
||
339 | * @param string $string |
||
340 | * |
||
341 | * @return void |
||
342 | */ |
||
343 | public function warning($string) |
||
347 | |||
348 | /** |
||
349 | * Write a string as error output. |
||
350 | * |
||
351 | * @param string $string |
||
352 | * |
||
353 | * @return void |
||
354 | */ |
||
355 | public function error($string) |
||
359 | |||
360 | /** |
||
361 | * Get response body. |
||
362 | * |
||
363 | * @return string|object |
||
364 | */ |
||
365 | public function getResponseBody() |
||
369 | |||
370 | /** |
||
371 | * Set response code. |
||
372 | * |
||
373 | * @param string|object $body |
||
374 | * |
||
375 | * @return void |
||
376 | */ |
||
377 | protected function setResponseBody($body) |
||
381 | |||
382 | /** |
||
383 | * Get response code. |
||
384 | * |
||
385 | * @return integer |
||
386 | */ |
||
387 | public function getResponseCode() |
||
391 | |||
392 | /** |
||
393 | * Set response code. |
||
394 | * |
||
395 | * @param integer $code |
||
396 | * |
||
397 | * @return void |
||
398 | */ |
||
399 | protected function setResponseCode($code) |
||
403 | |||
404 | /** |
||
405 | * Get response headers. |
||
406 | * |
||
407 | * @return array |
||
408 | */ |
||
409 | public function getResponseHeaders() |
||
413 | |||
414 | /** |
||
415 | * Set response headers. |
||
416 | * |
||
417 | * @param array $headers |
||
418 | * |
||
419 | * @return void |
||
420 | */ |
||
421 | protected function setResponseHeaders($headers) |
||
425 | |||
426 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.