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 |
||
35 | class DbalStore implements KeyValueStore |
||
36 | { |
||
37 | private $connection; |
||
38 | private $tableName; |
||
39 | |||
40 | /** |
||
41 | * @param Connection $connection A doctrine connection instance |
||
42 | * @param string $tableName The name of the database table |
||
43 | */ |
||
44 | 19 | public function __construct(Connection $connection, $tableName = 'store') |
|
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | 59 | public function set($key, $value) |
|
56 | { |
||
57 | 59 | KeyUtil::validate($key); |
|
58 | |||
59 | try { |
||
60 | 57 | $existing = $this->exists($key); |
|
61 | 1 | } catch (Exception $e) { |
|
62 | 1 | throw WriteException::forException($e); |
|
63 | } |
||
64 | |||
65 | 56 | if (false === $existing) { |
|
66 | 56 | $this->doInsert($key, $value); |
|
67 | } else { |
||
68 | 1 | $this->doUpdate($key, $value); |
|
69 | } |
||
70 | 54 | } |
|
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | 8 | View Code Duplication | public function get($key, $default = null) |
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | 32 | View Code Duplication | public function getOrFail($key) |
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | 7 | public function getMultiple(array $keys, $default = null) |
|
108 | { |
||
109 | 7 | KeyUtil::validateMultiple($keys); |
|
110 | |||
111 | // Normalize indices of the array |
||
112 | 5 | $keys = array_values($keys); |
|
113 | 5 | $data = $this->doGetMultiple($keys); |
|
114 | |||
115 | 3 | $results = array(); |
|
116 | 3 | $resolved = array(); |
|
117 | 3 | View Code Duplication | foreach ($data as $row) { |
118 | 3 | $results[$row['meta_key']] = Serializer::unserialize($row['meta_value']); |
|
119 | 2 | $resolved[$row['meta_key']] = $row['meta_key']; |
|
120 | } |
||
121 | |||
122 | 2 | $notResolvedArr = array_diff($keys, $resolved); |
|
123 | 2 | foreach ($notResolvedArr as $notResolved) { |
|
124 | 1 | $results[$notResolved] = $default; |
|
125 | } |
||
126 | |||
127 | 2 | return $results; |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | 31 | public function getMultipleOrFail(array $keys) |
|
134 | { |
||
135 | 31 | KeyUtil::validateMultiple($keys); |
|
136 | |||
137 | // Normalize indices of the array |
||
138 | 29 | $keys = array_values($keys); |
|
139 | |||
140 | 29 | $data = $this->doGetMultiple($keys); |
|
141 | |||
142 | 29 | $results = array(); |
|
143 | 29 | $resolved = array(); |
|
144 | 29 | View Code Duplication | foreach ($data as $row) { |
145 | 29 | $results[$row['meta_key']] = Serializer::unserialize($row['meta_value']); |
|
146 | 28 | $resolved[] = $row['meta_key']; |
|
147 | } |
||
148 | |||
149 | 28 | $notResolvedArr = array_diff($keys, $resolved); |
|
150 | |||
151 | 28 | if (!empty($notResolvedArr)) { |
|
152 | 1 | throw NoSuchKeyException::forKeys($notResolvedArr); |
|
153 | } |
||
154 | |||
155 | 27 | return $results; |
|
156 | } |
||
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | 10 | View Code Duplication | public function remove($key) |
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | 60 | public function exists($key) |
|
189 | |||
190 | /** |
||
191 | * {@inheritdoc} |
||
192 | */ |
||
193 | 89 | public function clear() |
|
202 | |||
203 | /** |
||
204 | * {@inheritdoc} |
||
205 | */ |
||
206 | 4 | public function keys() |
|
217 | |||
218 | /** |
||
219 | * The name for our DBAL database table. |
||
220 | * |
||
221 | * @return string |
||
222 | */ |
||
223 | 2 | public function getTableName() |
|
227 | |||
228 | /** |
||
229 | * Object Representation of the table used in this class. |
||
230 | * |
||
231 | * @return Table |
||
232 | */ |
||
233 | public function getTableForCreate() |
||
247 | |||
248 | 56 | View Code Duplication | private function doInsert($key, $value) |
249 | { |
||
250 | 56 | $serialized = Serializer::serialize($value); |
|
251 | |||
252 | try { |
||
253 | 54 | $this->connection->insert($this->tableName, array( |
|
254 | 54 | 'meta_key' => $key, |
|
255 | 54 | 'meta_value' => $serialized, |
|
256 | )); |
||
257 | } catch (Exception $e) { |
||
258 | throw WriteException::forException($e); |
||
259 | } |
||
260 | 54 | } |
|
261 | |||
262 | 1 | View Code Duplication | private function doUpdate($key, $value) |
263 | { |
||
264 | 1 | $serialized = Serializer::serialize($value); |
|
265 | |||
266 | try { |
||
267 | 1 | $this->connection->update($this->tableName, array( |
|
268 | 1 | 'meta_value' => $serialized, |
|
269 | ), array( |
||
270 | 1 | 'meta_key' => $key, |
|
271 | )); |
||
272 | } catch (Exception $e) { |
||
273 | throw WriteException::forException($e); |
||
274 | } |
||
275 | 1 | } |
|
276 | |||
277 | 34 | private function doGetMultiple(array $keys) |
|
278 | { |
||
279 | try { |
||
280 | 34 | $stmt = $this->connection->executeQuery('SELECT * FROM '.$this->tableName.' WHERE meta_key IN (?)', |
|
281 | 34 | array($keys), |
|
282 | 34 | array(Connection::PARAM_STR_ARRAY) |
|
283 | ); |
||
284 | 32 | $data = $stmt->fetchAll(); |
|
285 | 2 | } catch (Exception $e) { |
|
286 | 2 | throw ReadException::forException($e); |
|
287 | } |
||
288 | |||
289 | 32 | return is_array($data) ? $data : array(); |
|
290 | } |
||
291 | |||
292 | 36 | private function getDbRow($key) |
|
306 | } |
||
307 |
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.