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 |
||
13 | class Request extends Client |
||
14 | { |
||
15 | const GUZZLE_HTTP_CONFIG = [ |
||
16 | 'allow_redirects' => [ |
||
17 | 'max' => self::MAX_REDIRECTS, |
||
18 | 'referer' => true, |
||
19 | 'strict' => true, |
||
20 | ], |
||
21 | 'decode_content' => true, |
||
22 | 'headers' => [ |
||
23 | 'Accept' => 'text/plain;q=1.0, text/*;q=0.8, */*;q=0.1', |
||
24 | 'Accept-Charset' => 'utf-8;q=1.0, *;q=0.1', |
||
25 | 'Accept-Encoding' => 'identity;q=1.0, *;q=0.1', |
||
26 | 'User-Agent' => 'RobotsTxtParser-VIPnytt/2.0 (+https://github.com/VIPnytt/RobotsTxtParser/blob/master/README.md)', |
||
27 | ], |
||
28 | 'http_errors' => false, |
||
29 | 'timeout' => 60, |
||
30 | 'verify' => true, |
||
31 | ]; |
||
32 | |||
33 | /** |
||
34 | * Request timestamp |
||
35 | * @var int |
||
36 | */ |
||
37 | protected $time; |
||
38 | |||
39 | /** |
||
40 | * Cache-Control max-age |
||
41 | * @var int |
||
42 | */ |
||
43 | protected $maxAge; |
||
44 | |||
45 | /** |
||
46 | * HTTP Status code |
||
47 | * @var int |
||
48 | */ |
||
49 | protected $statusCode; |
||
50 | |||
51 | /** |
||
52 | * Robots.txt contents |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $contents; |
||
56 | |||
57 | /** |
||
58 | * Robots.txt character encoding |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $encoding; |
||
62 | |||
63 | /** |
||
64 | * Request constructor. |
||
65 | * |
||
66 | * @param string $baseUri |
||
67 | * @param array $guzzleConfig |
||
68 | * @param int|null $byteLimit |
||
69 | */ |
||
70 | public function __construct($baseUri, array $guzzleConfig = [], $byteLimit = self::BYTE_LIMIT) |
||
97 | |||
98 | /** |
||
99 | * Content-Type encoding HTTP header |
||
100 | * |
||
101 | * @param array $headers |
||
102 | * @return string |
||
103 | */ |
||
104 | protected function headerEncoding(array $headers) |
||
111 | |||
112 | /** |
||
113 | * Client header |
||
114 | * |
||
115 | * @param array $headers |
||
116 | * @param string $part |
||
117 | * @param string $delimiter |
||
118 | * @return string|false |
||
119 | */ |
||
120 | protected function parseHeader(array $headers, $part, $delimiter = ";") |
||
132 | |||
133 | /** |
||
134 | * Cache-Control max-age HTTP header |
||
135 | * |
||
136 | * @param array $headers |
||
137 | * @return int |
||
138 | */ |
||
139 | protected function headerMaxAge(array $headers) |
||
146 | |||
147 | /** |
||
148 | * Base URI |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | public function getBaseUri() |
||
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 int |
||
191 | */ |
||
192 | public function nextUpdate() |
||
196 | |||
197 | /** |
||
198 | * Valid until timestamp |
||
199 | * |
||
200 | * @return \DateTime|false |
||
201 | */ |
||
202 | public function validUntil() |
||
206 | } |
||
207 |
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.