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 Cache implements RobotsTxtInterface, SQLInterface |
||
15 | { |
||
16 | use UriParser; |
||
17 | |||
18 | /** |
||
19 | * Supported database drivers |
||
20 | */ |
||
21 | const SUPPORTED_DRIVERS = [ |
||
22 | self::DRIVER_MYSQL, |
||
23 | ]; |
||
24 | |||
25 | /** |
||
26 | * Client nextUpdate margin in seconds |
||
27 | * @var int |
||
28 | */ |
||
29 | protected $clientUpdateMargin = 300; |
||
30 | |||
31 | /** |
||
32 | * Database handler |
||
33 | * @var PDO |
||
34 | */ |
||
35 | private $pdo; |
||
36 | |||
37 | /** |
||
38 | * cURL options |
||
39 | * @var array |
||
40 | */ |
||
41 | private $curlOptions = []; |
||
42 | |||
43 | /** |
||
44 | * Byte limit |
||
45 | * @var int|null |
||
46 | */ |
||
47 | private $byteLimit = self::BYTE_LIMIT; |
||
48 | |||
49 | /** |
||
50 | * PDO driver |
||
51 | * @var string |
||
52 | */ |
||
53 | private $driver; |
||
54 | |||
55 | /** |
||
56 | * Cache constructor. |
||
57 | * |
||
58 | * @param PDO $pdo |
||
59 | * @param array $curlOptions |
||
60 | * @param int|null $byteLimit |
||
61 | */ |
||
62 | public function __construct(PDO $pdo, array $curlOptions = [], $byteLimit = self::BYTE_LIMIT) |
||
63 | { |
||
64 | $this->pdo = $this->pdoInitialize($pdo); |
||
65 | $this->curlOptions = $curlOptions; |
||
66 | $this->byteLimit = $byteLimit; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Initialize PDO connection |
||
71 | * |
||
72 | * @param PDO $pdo |
||
73 | * @return PDO |
||
74 | * @throws SQLException |
||
75 | */ |
||
76 | private function pdoInitialize(PDO $pdo) |
||
99 | |||
100 | /** |
||
101 | * Parser client |
||
102 | * |
||
103 | * @param string $baseUri |
||
104 | * @return TxtClient |
||
105 | */ |
||
106 | public function client($baseUri) |
||
136 | |||
137 | /** |
||
138 | * Clock sync check |
||
139 | * |
||
140 | * @param int $time |
||
141 | * @throws SQLException |
||
142 | */ |
||
143 | private function clockSyncCheck($time) |
||
149 | |||
150 | /** |
||
151 | * Mark robots.txt as active |
||
152 | * |
||
153 | * @param string $base |
||
154 | * @param int|null $workerID |
||
155 | * @return bool |
||
156 | */ |
||
157 | View Code Duplication | private function markAsActive($base, $workerID = 0) |
|
171 | |||
172 | /** |
||
173 | * Update an robots.txt in the database |
||
174 | * |
||
175 | * @param UriClient $client |
||
176 | * @return bool |
||
177 | */ |
||
178 | private function push(UriClient $client) |
||
179 | { |
||
180 | $base = $client->getBaseUri(); |
||
181 | $statusCode = $client->getStatusCode(); |
||
182 | $nextUpdate = $client->nextUpdate(); |
||
183 | $effective = ($effective = $client->getEffectiveUri()) === $base ? null : $effective; |
||
184 | if ( |
||
185 | stripos($base, 'http') === 0 && |
||
186 | ( |
||
187 | $statusCode === null || |
||
188 | ( |
||
189 | $statusCode >= 500 && |
||
190 | $statusCode < 600 |
||
191 | ) |
||
192 | ) && |
||
193 | $this->displacePush($base, $nextUpdate) |
||
194 | ) { |
||
195 | return true; |
||
196 | } |
||
197 | $validUntil = $client->validUntil(); |
||
198 | $content = $client->render(); |
||
199 | $query = $this->pdo->prepare(<<<SQL |
||
200 | INSERT INTO robotstxt__cache1 (base, content, statusCode, validUntil, nextUpdate, effective) |
||
201 | VALUES (:base, :content, :statusCode, :validUntil, :nextUpdate, :effective) |
||
202 | ON DUPLICATE KEY UPDATE content = :content, statusCode = :statusCode, validUntil = :validUntil, |
||
203 | nextUpdate = :nextUpdate, effective = :effective, worker = 0; |
||
204 | SQL |
||
205 | ); |
||
206 | $query->bindParam(':base', $base, PDO::PARAM_STR); |
||
207 | $query->bindParam(':content', $content, PDO::PARAM_STR); |
||
208 | $query->bindParam(':statusCode', $statusCode, PDO::PARAM_INT | PDO::PARAM_NULL); |
||
209 | $query->bindParam(':validUntil', $validUntil, PDO::PARAM_INT); |
||
210 | $query->bindParam(':nextUpdate', $nextUpdate, PDO::PARAM_INT); |
||
211 | $query->bindParam(':effective', $effective, PDO::PARAM_STR | PDO::PARAM_NULL); |
||
212 | return $query->execute(); |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Displace push timestamp |
||
217 | * |
||
218 | * @param string $base |
||
219 | * @param int $nextUpdate |
||
220 | * @return bool |
||
221 | */ |
||
222 | private function displacePush($base, $nextUpdate) |
||
253 | |||
254 | /** |
||
255 | * Invalidate cache |
||
256 | * |
||
257 | * @param $baseUri |
||
258 | * @return bool |
||
259 | */ |
||
260 | public function invalidate($baseUri) |
||
271 | |||
272 | /** |
||
273 | * Process the update queue |
||
274 | * |
||
275 | * @param int|null $workerID |
||
276 | * @return bool |
||
277 | */ |
||
278 | public function cron($workerID = null) |
||
306 | |||
307 | /** |
||
308 | * Set WorkerID |
||
309 | * |
||
310 | * @param int|null $workerID |
||
311 | * @return int |
||
312 | */ |
||
313 | protected function setWorkerID($workerID = null) |
||
326 | |||
327 | /** |
||
328 | * Clean the cache table |
||
329 | * |
||
330 | * @param int $delay - in seconds |
||
331 | * @return bool |
||
332 | */ |
||
333 | View Code Duplication | public function clean($delay = 600) |
|
344 | } |
||
345 |
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.