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 | * @param array|null $rawData |
||
21 | */ |
||
22 | 13 | public function __construct($primaryKey = 'id', array $rawData = null) |
|
30 | 4 | ||
31 | public function dumpRawData() |
||
35 | |||
36 | /** |
||
37 | * @return mixed |
||
38 | 2 | */ |
|
39 | public function lastIdCreated() |
||
43 | 1 | ||
44 | private function createTableIfNotExists($table) |
||
50 | |||
51 | /** |
||
52 | * @param string $table The table that will be accessed and written. |
||
53 | * @param array $fields A list of fields and values to be used when creating a new record. |
||
54 | * @param array $options The list of options that will help with creating the records. |
||
55 | * @return void |
||
56 | 1 | */ |
|
57 | public function create($table, array $fields, array $options = []) |
||
76 | |||
77 | /** |
||
78 | * If the condition matches the data in the record, then the return value is true. Otherwise, |
||
79 | * the return value is false. |
||
80 | * |
||
81 | * @return bool Returns true if the condition matches the data in the record. |
||
82 | 6 | */ |
|
83 | private function isRecordMatchingCondition($primaryKeyValue, $record, Condition $condition = null) |
||
137 | |||
138 | /** |
||
139 | * @param string $table The table that will be accessed and written. |
||
140 | * @param array $fields A list of fields and values to be used when updating a record. |
||
141 | * @param Condition|null $criteria The criteria that will filter the data. |
||
142 | * @param array $options The list of options that will help with updating the records. |
||
143 | * @return void |
||
144 | 1 | */ |
|
145 | public function update($table, array $fields, Condition $criteria = null, array $options = []) |
||
176 | 1 | ||
177 | 1 | /** |
|
178 | * @param string $table The table that will be accessed and written. |
||
179 | * @param Condition|null $criteria The criteria that will filter the data. |
||
180 | * @param array $options The list of options that will help with deleting the records. |
||
181 | * @return void |
||
182 | */ |
||
183 | public function delete($table, Condition $criteria = null, array $options = []) |
||
211 | |||
212 | /** |
||
213 | * Returns all records that match the criteria. |
||
214 | * |
||
215 | * @param string $table The table that will be accessed and written. |
||
216 | * @param Condition|null $criteria The criteria that will filter the records. |
||
217 | * @param array $options The list of options that will help with finding the records. |
||
218 | * @return array[] Multiple records from the table that match the criteria. |
||
219 | */ |
||
220 | public function findAll($table, Condition $criteria = null, array $options = []) |
||
255 | 5 | ||
256 | /** |
||
257 | * Returns the number of records that match the criteria. |
||
258 | * |
||
259 | * If the crtieria is null, then the return value is the total number of |
||
260 | * records in the table. |
||
261 | * |
||
262 | * @param string $table The table that will be accessed and written. |
||
263 | * @param Condition|null $criteria The criteria that will filter the records. |
||
264 | * @param array $options The list of options that help with counting the records. |
||
265 | * @return int The number of records that match the criteria. |
||
266 | */ |
||
267 | public function count($table, Condition $criteria = null, array $options = []) |
||
285 | |||
286 | /** |
||
287 | * Returns true if there exists a record that matches the criteria. |
||
288 | * |
||
289 | * If the criteria is null and the table has data, then the return value is true. There are two |
||
290 | * reasons for this result. |
||
291 | * - The table has stuff. |
||
292 | * - Running `$db->has('table_name')` is similar to saying, |
||
293 | "does the database have table_name?" |
||
294 | * |
||
295 | * @param string $table The table that will be accessed and written. |
||
296 | * @param Condition|null $criteria The criteria that will filter the records. |
||
297 | * @param array $options The list of options that will help with finding the records. |
||
298 | * @return boolean A boolean value indicating if any record matches the criteria. |
||
299 | */ |
||
300 | public function has($table, Condition $criteria = null, array $options = []) |
||
317 | } |
||
318 |
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.