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 |
||
19 | class Client extends Parser |
||
20 | { |
||
21 | /** |
||
22 | * HTTP status code parser |
||
23 | * @var StatusCodeParser |
||
24 | */ |
||
25 | protected $statusCodeParser; |
||
26 | |||
27 | /** |
||
28 | * Robots.txt base |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $baseUrl; |
||
32 | |||
33 | /** |
||
34 | * Status code |
||
35 | * @var int|null |
||
36 | */ |
||
37 | protected $statusCode; |
||
38 | |||
39 | /** |
||
40 | * Parser constructor. |
||
41 | * |
||
42 | * @param string $baseUrl |
||
43 | * @param int|null $statusCode |
||
44 | * @param string|null $content |
||
45 | * @param string $encoding |
||
46 | * @param int $byteLimit |
||
47 | */ |
||
48 | public function __construct($baseUrl, $statusCode = null, $content = null, $encoding = self::ENCODING, $byteLimit = self::BYTE_LIMIT) |
||
60 | |||
61 | /** |
||
62 | * Get sitemaps |
||
63 | * |
||
64 | * @return array |
||
65 | */ |
||
66 | public function getSitemaps() |
||
73 | |||
74 | /** |
||
75 | * Get host |
||
76 | * |
||
77 | * @return string|null |
||
78 | */ |
||
79 | View Code Duplication | public function getHost() |
|
86 | |||
87 | /** |
||
88 | * Get Clean-param |
||
89 | * |
||
90 | * @return array |
||
91 | */ |
||
92 | View Code Duplication | public function getCleanParam() |
|
99 | |||
100 | /** |
||
101 | * Return an User-agent instance, for future usage |
||
102 | * |
||
103 | * @param string $string |
||
104 | * @return UserAgentClient |
||
105 | */ |
||
106 | public function userAgent($string = self::USER_AGENT) |
||
120 | } |
||
121 |
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.