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 |
||
14 | class Download implements RobotsTxtInterface |
||
15 | { |
||
16 | /** |
||
17 | * Base uri |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $baseUri; |
||
21 | |||
22 | /** |
||
23 | * Download timestamp |
||
24 | * @var int |
||
25 | */ |
||
26 | protected $time; |
||
27 | |||
28 | /** |
||
29 | * Cache-Control max-age |
||
30 | * @var int |
||
31 | */ |
||
32 | protected $maxAge; |
||
33 | |||
34 | /** |
||
35 | * HTTP Status code |
||
36 | * @var int |
||
37 | */ |
||
38 | protected $statusCode; |
||
39 | |||
40 | /** |
||
41 | * Robots.txt contents |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $contents; |
||
45 | |||
46 | /** |
||
47 | * Robots.txt character encoding |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $encoding; |
||
51 | |||
52 | /** |
||
53 | * Parser client class |
||
54 | * @var Client |
||
55 | */ |
||
56 | protected $parserClient; |
||
57 | |||
58 | /** |
||
59 | * Download constructor. |
||
60 | * |
||
61 | * @param string $baseUri |
||
62 | * @param array $guzzleConfig |
||
63 | */ |
||
64 | public function __construct($baseUri, array $guzzleConfig = []) |
||
104 | |||
105 | /** |
||
106 | * Content-Type encoding HTTP header |
||
107 | * |
||
108 | * @param array $headers |
||
109 | * @return string |
||
110 | */ |
||
111 | View Code Duplication | protected function headerEncoding(array $headers) |
|
123 | |||
124 | /** |
||
125 | * Cache-Control max-age HTTP header |
||
126 | * |
||
127 | * @param array $headers |
||
128 | * @return int |
||
129 | */ |
||
130 | View Code Duplication | protected function headerMaxAge(array $headers) |
|
142 | |||
143 | /** |
||
144 | * Parser client |
||
145 | * |
||
146 | * @param int|null $byteLimit |
||
147 | * @return Client |
||
148 | */ |
||
149 | public function parserClient($byteLimit = self::BYTE_LIMIT) |
||
156 | |||
157 | /** |
||
158 | * Status code |
||
159 | * |
||
160 | * @return int |
||
161 | */ |
||
162 | public function getStatusCode() |
||
166 | |||
167 | /** |
||
168 | * URL content |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | public function getContents() |
||
176 | |||
177 | /** |
||
178 | * Encoding |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | public function getEncoding() |
||
186 | |||
187 | /** |
||
188 | * Next update timestamp |
||
189 | * |
||
190 | * @return \DateTime|false |
||
191 | */ |
||
192 | public function nextUpdate() |
||
198 | |||
199 | /** |
||
200 | * Valid until timestamp |
||
201 | * |
||
202 | * @return \DateTime|false |
||
203 | */ |
||
204 | public function validUntil() |
||
210 | } |
||
211 |
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.