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 DoctrineDriver implements \Bernard\Driver |
||
14 | { |
||
15 | protected $connection; |
||
16 | |||
17 | /** |
||
18 | * {@inheritdoc} |
||
19 | */ |
||
20 | public function __construct(Connection $connection) |
||
24 | |||
25 | /** |
||
26 | * {@inheritdoc} |
||
27 | */ |
||
28 | public function listQueues() |
||
35 | |||
36 | /** |
||
37 | * {@inheritDoc} |
||
38 | */ |
||
39 | public function createQueue($queueName) |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | public function countMessages($queueName) |
||
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | public function pushMessage($queueName, $message) |
||
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | public function popMessage($queueName, $duration = 5) |
||
120 | |||
121 | View Code Duplication | protected function doPopMessage($queueName) |
|
|
|||
122 | { |
||
123 | $query = 'SELECT id, message FROM bernard_messages |
||
124 | WHERE queue = :queue AND visible = :visible |
||
125 | ORDER BY sentAt, id LIMIT 1' . $this->connection->getDatabasePlatform()->getForUpdateSql(); |
||
126 | |||
127 | list($id, $message) = $this->connection->fetchArray($query, array( |
||
128 | 'queue' => $queueName, |
||
129 | 'visible' => true, |
||
130 | )); |
||
131 | |||
132 | if ($id) { |
||
133 | $this->connection->update('bernard_messages', array('visible' => 0), compact('id')); |
||
134 | |||
135 | return array($message, $id); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | public function acknowledgeMessage($queueName, $receipt) |
||
143 | { |
||
144 | $this->connection->delete('bernard_messages', ['id' => $receipt, 'queue' => $queueName]); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | public function peekQueue($queueName, $index = 0, $limit = 20) |
||
151 | { |
||
152 | $parameters = [$queueName, $limit, $index]; |
||
153 | $types = ['string', 'integer', 'integer']; |
||
154 | |||
155 | $query = 'SELECT message FROM bernard_messages WHERE queue = ? ORDER BY sentAt LIMIT ? OFFSET ?'; |
||
156 | |||
157 | return $this |
||
158 | ->connection |
||
159 | ->executeQuery($query, $parameters, $types) |
||
160 | ->fetchAll(\PDO::FETCH_COLUMN) |
||
161 | ; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * {@inheritdoc} |
||
166 | */ |
||
167 | public function removeQueue($queueName) |
||
168 | { |
||
169 | $this->connection->delete('bernard_messages', ['queue' => $queueName]); |
||
170 | $this->connection->delete('bernard_queues', ['name' => $queueName]); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * {@inheritdoc} |
||
175 | */ |
||
176 | public function info() |
||
177 | { |
||
178 | $params = $this->connection->getParams(); |
||
179 | |||
180 | unset($params['user'], $params['password']); |
||
181 | |||
182 | return $params; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Execute the actual query and process the response |
||
187 | * |
||
188 | * @param string $queueName |
||
189 | * |
||
190 | * @return array|null |
||
191 | */ |
||
192 | View Code Duplication | protected function doPopMessage($queueName) |
|
209 | } |
||
210 |
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.