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 MemoryDatabase 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 MemoryDatabase, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class MemoryDatabase extends AbstractDatabase |
||
11 | { |
||
12 | |||
13 | public $tables = []; |
||
14 | private $lastIdCreated; |
||
15 | private $lastAutoId = 0; |
||
16 | private $primaryKey; |
||
17 | |||
18 | /** |
||
19 | * @param string $primaryKey |
||
20 | */ |
||
21 | 13 | public function __construct($primaryKey = 'id', $rawData = null) |
|
28 | |||
29 | 4 | public function dumpRawData() |
|
33 | |||
34 | /** |
||
35 | * @return mixed |
||
36 | */ |
||
37 | 2 | public function lastIdCreated() |
|
41 | |||
42 | 1 | private function createTableIfNotExists($table) |
|
48 | |||
49 | /** |
||
50 | * @param string $table |
||
51 | * @param array $fields |
||
52 | * @param array|null $options |
||
53 | * @return void |
||
54 | */ |
||
55 | 1 | public function create($table, array $fields, array $options = []) |
|
74 | |||
75 | /** |
||
76 | * If the condition matches the data in the record, then the return value is true. Otherwise, |
||
77 | * the return value is false. |
||
78 | * |
||
79 | * @return bool Returns true if the condition matches the data in the record. |
||
80 | */ |
||
81 | 6 | private function isRecordMatchingCondition($primaryKeyValue, $record, Condition $condition = null) |
|
135 | |||
136 | /** |
||
137 | * @param string $table |
||
138 | * @param array $fields |
||
139 | * @param Condition|null $criteria |
||
140 | * @param array|null $options |
||
141 | * @return array An array of records that have been updated. |
||
142 | */ |
||
143 | 1 | public function update($table, array $fields, Condition $criteria = null, array $options = []) |
|
179 | |||
180 | /** |
||
181 | * @param string $table |
||
182 | * @param Condition|null $criteria |
||
183 | * @param array|null $options |
||
184 | * @return array An array of records that were deleted. |
||
185 | */ |
||
186 | 1 | public function delete($table, Condition $criteria = null, array $options = []) |
|
219 | |||
220 | /** |
||
221 | * Returns all records that match the criteria. |
||
222 | * |
||
223 | * @param string $table |
||
224 | * @param Condition|null $criteria |
||
225 | * @param array|null $options |
||
226 | * @return array |
||
227 | */ |
||
228 | 6 | public function findAll($table, Condition $criteria = null, array $options = []) |
|
263 | |||
264 | /** |
||
265 | * Returns the number of records that match the criteria. |
||
266 | * |
||
267 | * If the crtieria is null, then the return value is the total number of |
||
268 | * records in the table. |
||
269 | * |
||
270 | * @param string $table |
||
271 | * @param Condition|null $criteria |
||
272 | * @param array|null $options |
||
273 | * @return int |
||
274 | */ |
||
275 | 1 | public function count($table, Condition $criteria = null, array $options = []) |
|
293 | |||
294 | /** |
||
295 | * Returns true if there exists a record that matches the criteria. |
||
296 | * |
||
297 | * If the criteria is null and the table has data, then the return value is true. There are two |
||
298 | * reasons for this result. |
||
299 | * - The table has stuff. |
||
300 | * - Running `$db->has('table_name')` is similar to saying, |
||
301 | "does the database have table_name?" |
||
302 | * |
||
303 | * @param string $table |
||
304 | * @param Condition|null $criteria |
||
305 | * @param array|null $options |
||
306 | * @return boolean |
||
307 | */ |
||
308 | 1 | public function has($table, Condition $criteria = null, array $options = []) |
|
325 | } |
||
326 |
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.