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:
Complex classes like Pdo often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Pdo, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class Pdo implements StorageInterface |
||
31 | { |
||
32 | use DataTypeCasterTrait; |
||
33 | |||
34 | /** |
||
35 | * @var PhpPdo |
||
36 | */ |
||
37 | protected $conn; |
||
38 | |||
39 | /** |
||
40 | * @var PdoConfig |
||
41 | */ |
||
42 | protected $config; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $quoteMaps = array('mysql' => '`', 'pgsql' => '"'); |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $quote = '`'; |
||
53 | |||
54 | /** |
||
55 | * @var int |
||
56 | */ |
||
57 | protected $transactionalLevel = 0; |
||
58 | |||
59 | /** |
||
60 | * @var bool |
||
61 | */ |
||
62 | protected $transactional = false; |
||
63 | |||
64 | /** |
||
65 | * Constructor. |
||
66 | * |
||
67 | * @param PdoConfig $config |
||
68 | */ |
||
69 | public function __construct(PdoConfig $config) |
||
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | public function save(ReadModelInterface $model, $table) |
||
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | public function remove($id, $table) |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function findById($id, $table, $class) |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | public function finder($table, $class) |
||
132 | |||
133 | /** |
||
134 | * Execute query without statement. |
||
135 | * |
||
136 | * @param string $sql |
||
137 | * @param array $values |
||
138 | */ |
||
139 | public function exec($sql, array $values = array()) |
||
157 | |||
158 | /** |
||
159 | * Execute query with statement. |
||
160 | * |
||
161 | * @param string $sql |
||
162 | * @param int $mode |
||
163 | * |
||
164 | * @return PDOStatement |
||
165 | */ |
||
166 | public function query($sql, $mode = PhpPdo::FETCH_ASSOC) |
||
174 | |||
175 | /** |
||
176 | * @return int |
||
177 | */ |
||
178 | public function getTransactionalLevel() |
||
182 | |||
183 | /** |
||
184 | * @return boolean |
||
185 | */ |
||
186 | public function isTransactional() |
||
190 | |||
191 | /** |
||
192 | * Begin transaction |
||
193 | */ |
||
194 | View Code Duplication | public function beginTransaction() |
|
205 | |||
206 | /** |
||
207 | * Rollback transaction |
||
208 | */ |
||
209 | View Code Duplication | public function rollback() |
|
219 | |||
220 | /** |
||
221 | * Commit transaction. |
||
222 | */ |
||
223 | View Code Duplication | public function commit() |
|
235 | |||
236 | /** |
||
237 | * Compute fields to expressions. |
||
238 | * |
||
239 | * @param array $fields |
||
240 | * @param PdoFinder $finder |
||
241 | * |
||
242 | * @return PdoCompositeExpression |
||
243 | */ |
||
244 | protected function computeFieldsExpression(array $fields, PdoFinder $finder) |
||
253 | |||
254 | /** |
||
255 | * @param array $values |
||
256 | * @param string $class |
||
257 | * |
||
258 | * @return array |
||
259 | */ |
||
260 | protected function normalizeRelation(array $values, $class) |
||
294 | |||
295 | /** |
||
296 | * Normalize value. |
||
297 | * |
||
298 | * @param array $values |
||
299 | * |
||
300 | * @return array |
||
301 | */ |
||
302 | protected function normalize(array $values) |
||
323 | |||
324 | /** |
||
325 | * Build query update. |
||
326 | * |
||
327 | * @param array $values |
||
328 | * @param array $conditions |
||
329 | * @param string $table |
||
330 | * |
||
331 | * @return string |
||
332 | */ |
||
333 | protected function buildUpdateQuery(array $values, array $conditions, $table) |
||
343 | |||
344 | /** |
||
345 | * Build insert query. |
||
346 | * |
||
347 | * @param array $values |
||
348 | * @param string $table |
||
349 | * |
||
350 | * @return string |
||
351 | */ |
||
352 | public function buildInsertQuery(array $values, $table) |
||
361 | |||
362 | /** |
||
363 | * Build data sets. |
||
364 | * |
||
365 | * @param array $parts |
||
366 | * |
||
367 | * @return array |
||
368 | */ |
||
369 | protected function buildDataSets(array $parts) |
||
378 | |||
379 | /** |
||
380 | * Quote field. |
||
381 | * |
||
382 | * @param string $field |
||
383 | * |
||
384 | * @return string |
||
385 | */ |
||
386 | protected function quote($field) |
||
390 | |||
391 | /** |
||
392 | * @param mixed $value |
||
393 | * |
||
394 | * @return mixed|null|string |
||
395 | */ |
||
396 | protected function resolveValue($value) |
||
408 | } |
||
409 |
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.