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') |
|
45 | { |
||
46 | 19 | Assert::stringNotEmpty($tableName, 'The table must be a string. Got: %s'); |
|
47 | |||
48 | 15 | $this->connection = $connection; |
|
49 | 15 | $this->tableName = $tableName; |
|
50 | 15 | } |
|
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) |
|
|||
76 | { |
||
77 | 8 | KeyUtil::validate($key); |
|
78 | |||
79 | 6 | $dbResult = $this->getDbRow($key); |
|
80 | |||
81 | 5 | if (null === $dbResult) { |
|
82 | 1 | return $default; |
|
83 | } |
||
84 | |||
85 | 4 | return Serializer::unserialize($dbResult['meta_value']); |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | 32 | View Code Duplication | public function getOrFail($key) |
92 | { |
||
93 | 32 | KeyUtil::validate($key); |
|
94 | |||
95 | 30 | $dbResult = $this->getDbRow($key); |
|
96 | |||
97 | 29 | if (null === $dbResult) { |
|
98 | 1 | throw NoSuchKeyException::forKey($key); |
|
99 | } |
||
100 | |||
101 | 28 | return Serializer::unserialize($dbResult['meta_value']); |
|
102 | } |
||
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 | public function remove($key) |
|
162 | { |
||
163 | 10 | KeyUtil::validate($key); |
|
164 | |||
165 | try { |
||
166 | 8 | $result = $this->connection->delete($this->tableName, array('meta_key' => $key)); |
|
167 | 1 | } catch (Exception $e) { |
|
168 | 1 | throw WriteException::forException($e); |
|
169 | } |
||
170 | |||
171 | 7 | return $result === 1; |
|
172 | } |
||
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | 60 | public function exists($key) |
|
178 | { |
||
179 | 60 | KeyUtil::validate($key); |
|
180 | |||
181 | try { |
||
182 | 58 | $result = $this->connection->fetchAssoc('SELECT * FROM '.$this->tableName.' WHERE meta_key = ?', array($key)); |
|
183 | 2 | } catch (Exception $e) { |
|
184 | 2 | throw ReadException::forException($e); |
|
185 | } |
||
186 | |||
187 | 56 | return $result ? true : false; |
|
188 | } |
||
189 | |||
190 | /** |
||
191 | * {@inheritdoc} |
||
192 | */ |
||
193 | 89 | public function clear() |
|
194 | { |
||
195 | try { |
||
196 | 89 | $stmt = $this->connection->query('DELETE FROM '.$this->tableName); |
|
197 | 89 | $stmt->execute(); |
|
198 | 1 | } catch (Exception $e) { |
|
199 | 1 | throw WriteException::forException($e); |
|
200 | } |
||
201 | 89 | } |
|
202 | |||
203 | /** |
||
204 | * {@inheritdoc} |
||
205 | */ |
||
206 | 4 | public function keys() |
|
207 | { |
||
208 | try { |
||
209 | 4 | $stmt = $this->connection->query('SELECT meta_key FROM '.$this->tableName); |
|
210 | 3 | $result = $stmt->fetchAll(PDO::FETCH_COLUMN); |
|
211 | 1 | } catch (Exception $e) { |
|
212 | 1 | throw ReadException::forException($e); |
|
213 | } |
||
214 | |||
215 | 3 | return $result; |
|
216 | } |
||
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() |
||
234 | { |
||
235 | $schema = new Schema(); |
||
236 | |||
237 | $table = $schema->createTable($this->getTableName()); |
||
247 | |||
248 | 56 | private function doInsert($key, $value) |
|
261 | |||
262 | 1 | private function doUpdate($key, $value) |
|
276 | |||
277 | 34 | private function doGetMultiple(array $keys) |
|
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.