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 Cache implements RobotsTxtInterface, SQLInterface |
||
16 | { |
||
17 | use UriParser; |
||
18 | |||
19 | /** |
||
20 | * Supported database drivers |
||
21 | */ |
||
22 | const SUPPORTED_DRIVERS = [ |
||
23 | self::DRIVER_MYSQL, |
||
24 | ]; |
||
25 | |||
26 | /** |
||
27 | * Client nextUpdate margin in seconds |
||
28 | * @var int |
||
29 | */ |
||
30 | protected $clientUpdateMargin = 300; |
||
31 | |||
32 | /** |
||
33 | * Database handler |
||
34 | * @var PDO |
||
35 | */ |
||
36 | private $pdo; |
||
37 | |||
38 | /** |
||
39 | * cURL options |
||
40 | * @var array |
||
41 | */ |
||
42 | private $curlOptions = []; |
||
43 | |||
44 | /** |
||
45 | * Byte limit |
||
46 | * @var int|null |
||
47 | */ |
||
48 | private $byteLimit = self::BYTE_LIMIT; |
||
49 | |||
50 | /** |
||
51 | * PDO driver |
||
52 | * @var string |
||
53 | */ |
||
54 | private $driver; |
||
55 | |||
56 | /** |
||
57 | * Cache constructor. |
||
58 | * |
||
59 | * @param PDO $pdo |
||
60 | * @param array $curlOptions |
||
61 | * @param int|null $byteLimit |
||
62 | */ |
||
63 | public function __construct(PDO $pdo, array $curlOptions = [], $byteLimit = self::BYTE_LIMIT) |
||
64 | { |
||
65 | $this->pdo = $this->pdoInitialize($pdo); |
||
66 | $this->curlOptions = $curlOptions; |
||
67 | $this->byteLimit = $byteLimit; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Initialize PDO connection |
||
72 | * |
||
73 | * @param PDO $pdo |
||
74 | * @return PDO |
||
75 | * @throws SQLException |
||
76 | */ |
||
77 | private function pdoInitialize(PDO $pdo) |
||
78 | { |
||
79 | if ($pdo->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_SILENT) { |
||
80 | $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); |
||
81 | } |
||
82 | $pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL); |
||
83 | $pdo->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL); |
||
84 | $pdo->exec('SET NAMES ' . self::SQL_ENCODING); |
||
85 | $this->driver = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME); |
||
86 | if (!in_array($this->driver, self::SUPPORTED_DRIVERS)) { |
||
87 | throw new SQLException('Unsupported database. ' . self::README_SQL_CACHE); |
||
88 | } |
||
89 | try { |
||
90 | $pdo->query("SELECT 1 FROM robotstxt__cache1 LIMIT 1;"); |
||
91 | } catch (\Exception $exception1) { |
||
92 | try { |
||
93 | $pdo->query(file_get_contents(__DIR__ . '/../res/cache.sql')); |
||
94 | } catch (\Exception $exception2) { |
||
95 | throw new SQLException('Missing table `' . self::TABLE_CACHE . '`. Setup instructions: ' . self::README_SQL_CACHE); |
||
96 | } |
||
97 | } |
||
98 | return $pdo; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Parser client |
||
103 | * |
||
104 | * @param string $baseUri |
||
105 | * @return TxtClient |
||
106 | */ |
||
107 | public function client($baseUri) |
||
108 | { |
||
109 | $base = $this->urlBase($baseUri); |
||
110 | $query = $this->pdo->prepare(<<<SQL |
||
111 | SELECT |
||
112 | content, |
||
113 | statusCode, |
||
114 | nextUpdate, |
||
115 | effective, |
||
116 | worker, |
||
117 | UNIX_TIMESTAMP() |
||
118 | FROM robotstxt__cache1 |
||
119 | WHERE base = :base; |
||
120 | SQL |
||
121 | ); |
||
122 | $query->bindParam(':base', $base, PDO::PARAM_STR); |
||
123 | $query->execute(); |
||
124 | if ($query->rowCount() > 0) { |
||
125 | $row = $query->fetch(PDO::FETCH_ASSOC); |
||
126 | $this->clockSyncCheck($row['UNIX_TIMESTAMP()']); |
||
127 | if ($row['nextUpdate'] > ($row['UNIX_TIMESTAMP()'] - $this->clientUpdateMargin)) { |
||
128 | $this->markAsActive($base, $row['worker']); |
||
129 | return new TxtClient($base, $row['statusCode'], $row['content'], self::ENCODING, $row['effective'], $this->byteLimit); |
||
130 | } |
||
131 | } |
||
132 | $request = new UriClient($base, $this->curlOptions, $this->byteLimit); |
||
133 | $this->push($request, null); |
||
134 | return new TxtClient($base, $request->getStatusCode(), $request->getContents(), $request->getEncoding(), $request->getEffectiveUri(), $this->byteLimit); |
||
135 | } |
||
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 | * @param int|null $worker |
||
177 | * @return bool |
||
178 | */ |
||
179 | private function push(UriClient $client, $worker = 0) |
||
216 | |||
217 | /** |
||
218 | * Displace push timestamp |
||
219 | * |
||
220 | * @param string $base |
||
221 | * @param int $nextUpdate |
||
222 | * @return bool |
||
223 | */ |
||
224 | private function displacePush($base, $nextUpdate) |
||
255 | |||
256 | /** |
||
257 | * Invalidate cache |
||
258 | * |
||
259 | * @param $baseUri |
||
260 | * @return bool |
||
261 | */ |
||
262 | public function invalidate($baseUri) |
||
273 | |||
274 | /** |
||
275 | * Process the update queue |
||
276 | * |
||
277 | * @param float|int $targetTime |
||
278 | * @param int|null $workerID |
||
279 | * @return string[]|false |
||
280 | * @throws ClientException |
||
281 | */ |
||
282 | public function cron($targetTime = 60, $workerID = null) |
||
318 | |||
319 | /** |
||
320 | * Set WorkerID |
||
321 | * |
||
322 | * @param int|null $workerID |
||
323 | * @return int |
||
324 | */ |
||
325 | protected function setWorkerID($workerID = null) |
||
338 | |||
339 | /** |
||
340 | * Clean the cache table |
||
341 | * |
||
342 | * @param int $delay - in seconds |
||
343 | * @return bool |
||
344 | */ |
||
345 | View Code Duplication | public function clean($delay = 600) |
|
356 | } |
||
357 |
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.