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 |
||
15 | class CacheHandler implements RobotsTxtInterface, SQLInterface |
||
16 | { |
||
17 | use UrlParser; |
||
18 | use SQLTrait; |
||
19 | |||
20 | /** |
||
21 | * Database connection |
||
22 | * @var PDO |
||
23 | */ |
||
24 | protected $pdo; |
||
25 | |||
26 | /** |
||
27 | * GuzzleHTTP config |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $guzzleConfig = []; |
||
31 | |||
32 | /** |
||
33 | * Byte limit |
||
34 | * @var int|null |
||
35 | */ |
||
36 | protected $byteLimit = self::BYTE_LIMIT; |
||
37 | |||
38 | /** |
||
39 | * Client nextUpdate margin in seconds |
||
40 | * @var int |
||
41 | */ |
||
42 | protected $clientUpdateMargin = 300; |
||
43 | |||
44 | /** |
||
45 | * PDO driver |
||
46 | * @var string |
||
47 | */ |
||
48 | private $driver; |
||
49 | |||
50 | /** |
||
51 | * CacheHandler constructor. |
||
52 | * |
||
53 | * @param PDO $pdo |
||
54 | * @param array $guzzleConfig |
||
55 | * @param int|null $byteLimit |
||
56 | */ |
||
57 | public function __construct(PDO $pdo, array $guzzleConfig = [], $byteLimit = self::BYTE_LIMIT) |
||
67 | |||
68 | /** |
||
69 | * Parser client |
||
70 | * |
||
71 | * @param string $baseUri |
||
72 | * @return TxtClient |
||
73 | */ |
||
74 | public function client($baseUri) |
||
102 | |||
103 | /** |
||
104 | * Mark robots.txt as active |
||
105 | * |
||
106 | * @param string $base |
||
107 | * @param int|null $workerID |
||
108 | * @return bool |
||
109 | */ |
||
110 | View Code Duplication | private function markAsActive($base, $workerID = 0) |
|
124 | |||
125 | /** |
||
126 | * Update an robots.txt in the database |
||
127 | * |
||
128 | * @param UriClient $request |
||
129 | * @return bool |
||
130 | */ |
||
131 | public function push(UriClient $request) |
||
160 | |||
161 | /** |
||
162 | * Displace push timestamp |
||
163 | * |
||
164 | * @param string $base |
||
165 | * @param int $nextUpdate |
||
166 | * @return bool |
||
167 | */ |
||
168 | private function displacePush($base, $nextUpdate) |
||
197 | |||
198 | /** |
||
199 | * Process the update queue |
||
200 | * |
||
201 | * @param int|null $workerID |
||
202 | * @return bool |
||
203 | */ |
||
204 | public function cron($workerID = null) |
||
232 | |||
233 | /** |
||
234 | * Set WorkerID |
||
235 | * |
||
236 | * @param int|null $workerID |
||
237 | * @return int |
||
238 | */ |
||
239 | protected function setWorkerID($workerID = null) |
||
252 | |||
253 | /** |
||
254 | * Clean the cache table |
||
255 | * |
||
256 | * @param int $delay - in seconds |
||
257 | * @return bool |
||
258 | */ |
||
259 | View Code Duplication | public function clean($delay = 600) |
|
270 | |||
271 | /** |
||
272 | * Create SQL table |
||
273 | * |
||
274 | * @return bool |
||
275 | * @throws SQLException |
||
276 | */ |
||
277 | public function setup() |
||
284 | } |
||
285 |
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.