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 |
||
7 | class PdoCrawlQueue implements CrawlQueue |
||
8 | { |
||
9 | const STATUS_PENDING = 1; |
||
10 | const STATUS_PROCESSED = 2; |
||
11 | |||
12 | /** @var \Pdo */ |
||
13 | private $pdo; |
||
14 | |||
15 | /** @var string */ |
||
16 | private $table; |
||
17 | |||
18 | public function __construct(\Pdo $pdo, string $table) |
||
19 | { |
||
20 | $this->pdo = $pdo; |
||
21 | $this->table = $table; |
||
22 | } |
||
23 | |||
24 | public function add(CrawlUrl $url): CrawlQueue |
||
25 | { |
||
26 | if ($this->has($url)) { |
||
27 | return $this; |
||
28 | } |
||
29 | |||
30 | $data = serialize($url); |
||
31 | |||
32 | $this->pdo->beginTransaction(); |
||
33 | |||
34 | try { |
||
35 | $statement = $this->pdo->prepare( |
||
36 | 'insert into '.$this->table.' (url, status, object) values (:url, :status, :object)' |
||
37 | ); |
||
38 | $statement->execute([ |
||
39 | 'url' => $url->url, |
||
40 | 'object' => $data, |
||
41 | 'status' => self::STATUS_PENDING, |
||
42 | ]); |
||
43 | $statement->closeCursor(); |
||
44 | $url->setId($this->pdo->lastInsertId()); |
||
45 | |||
46 | $statement = $this->pdo->prepare( |
||
47 | 'update '.$this->table.' set object = :object where id = :id' |
||
48 | ); |
||
49 | |||
50 | $data = serialize($url); |
||
51 | |||
52 | $statement->execute([ |
||
53 | 'object' => $data, |
||
54 | 'id' => $url->getId(), |
||
55 | ]); |
||
56 | $statement->closeCursor(); |
||
57 | $this->pdo->commit(); |
||
58 | } catch (\Exception $exception) { |
||
59 | $this->pdo->rollBack(); |
||
60 | throw $exception; |
||
61 | } |
||
62 | |||
63 | return $this; |
||
64 | } |
||
65 | |||
66 | public function hasPendingUrls(): bool |
||
67 | { |
||
68 | $statement = $this->pdo->prepare( |
||
69 | 'select url from '.$this->table.' where status = :status limit 1' |
||
70 | ); |
||
71 | $statement->execute([ |
||
72 | 'status' => self::STATUS_PENDING, |
||
73 | ]); |
||
74 | $result = $statement->fetch(); |
||
75 | $statement->closeCursor(); |
||
76 | |||
77 | return (bool) $result; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param int $id |
||
82 | * |
||
83 | * @return \Spatie\Crawler\CrawlUrl|null |
||
84 | */ |
||
85 | public function getUrlById(int $id): CrawlUrl |
||
86 | { |
||
87 | $statement = $this->pdo->prepare( |
||
88 | 'select object from '.$this->table.' where id = :id' |
||
89 | ); |
||
90 | $statement->execute([ |
||
91 | 'id' => $id, |
||
92 | ]); |
||
93 | $result = $statement->fetch(); |
||
94 | $statement->closeCursor(); |
||
95 | |||
96 | if (! $result) { |
||
97 | throw new UrlNotFoundByIndex(sprintf('#%d crawl url not found in database', $id)); |
||
98 | } |
||
99 | |||
100 | return unserialize($result['object']); |
||
101 | } |
||
102 | |||
103 | View Code Duplication | public function hasAlreadyBeenProcessed(CrawlUrl $url) |
|
|
|||
104 | { |
||
105 | $statement = $this->pdo->prepare( |
||
106 | 'select id from '.$this->table.' where url = :url and status = :status' |
||
107 | ); |
||
108 | $statement->execute([ |
||
109 | 'url' => $url->url, |
||
110 | 'status' => self::STATUS_PROCESSED, |
||
111 | ]); |
||
112 | $result = $statement->fetch(); |
||
113 | $statement->closeCursor(); |
||
114 | |||
115 | return (bool) $result; |
||
116 | } |
||
117 | |||
118 | public function markAsProcessed(CrawlUrl $crawlUrl) |
||
129 | |||
130 | /** |
||
131 | * @param CrawlUrl|Url $crawlUrl |
||
132 | * |
||
133 | * @return bool |
||
134 | */ |
||
135 | public function has($crawlUrl): bool |
||
152 | |||
153 | /** @return \Spatie\Crawler\CrawlUrl|null */ |
||
154 | View Code Duplication | public function getPendingUrl() |
|
171 | } |
||
172 |
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.