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 BaseDBAccess 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 BaseDBAccess, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class BaseDBAccess |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * @var DBDataset |
||
15 | */ |
||
16 | private $_db = null; |
||
17 | |||
18 | /** |
||
19 | * Wrapper for SQLHelper |
||
20 | * |
||
21 | * @var SQLHelper |
||
22 | */ |
||
23 | protected $_sqlhelper = null; |
||
24 | |||
25 | /** |
||
26 | * Base Class Constructor. Don't must be override. |
||
27 | * @param DBDataset $db |
||
28 | */ |
||
29 | public function __construct(DBDataset $db) |
||
33 | |||
34 | /** |
||
35 | * Create a instance of DBDataset to connect database |
||
36 | * @return DBDataset |
||
37 | */ |
||
38 | protected function getDBDataset() |
||
42 | |||
43 | /** |
||
44 | * Execute a SQL and dont wait for a response. |
||
45 | * @param string $sql |
||
46 | * @param string $param |
||
47 | * @param bool $getId |
||
48 | * @return int|null |
||
49 | */ |
||
50 | protected function executeSQL($sql, $param = null, $getId = false) |
||
87 | |||
88 | /** |
||
89 | * Execulte SELECT SQL Query |
||
90 | * |
||
91 | * @param string $sql |
||
92 | * @param array $param |
||
93 | * @param int $ttl |
||
94 | * @return IteratorInterface |
||
95 | */ |
||
96 | View Code Duplication | protected function getIterator($sql, $param = null, $ttl = null) |
|
123 | |||
124 | View Code Duplication | protected function getScalar($sql, $param = null) |
|
151 | |||
152 | /** |
||
153 | * Get a SQLHelper object |
||
154 | * |
||
155 | * @return SQLHelper |
||
156 | */ |
||
157 | public function getSQLHelper() |
||
165 | |||
166 | /** |
||
167 | * Get an Interator from an ID. Ideal for get data from PK |
||
168 | * |
||
169 | * @param string $tablename |
||
170 | * @param string $key |
||
171 | * @param string $value |
||
172 | * @return IteratorInterface |
||
173 | */ |
||
174 | protected function getIteratorbyId($tablename, $key, $value) |
||
181 | |||
182 | /** |
||
183 | * Get an Array from an existing Iterator |
||
184 | * |
||
185 | * @param IteratorInterface $it |
||
186 | * @param string $key |
||
187 | * @param string $value |
||
188 | * @param string $firstElement |
||
189 | * @return array |
||
190 | */ |
||
191 | public static function getArrayFromIterator(IteratorInterface $it, $key, $value, $firstElement = "-- Selecione --") |
||
203 | |||
204 | /** |
||
205 | * |
||
206 | * @param IteratorInterface $it |
||
207 | * @param string $name |
||
208 | * @param array $fields |
||
209 | * @param bool $echoToBrowser |
||
210 | * @return string |
||
211 | */ |
||
212 | public static function saveToCSV($it, $name = "data.csv", $fields = null, $echoToBrowser = true) |
||
252 | |||
253 | /** |
||
254 | * Get a IDbFunctions class containing specific database operations |
||
255 | * @return DBFunctionsInterface |
||
256 | */ |
||
257 | public function getDbFunctions() |
||
261 | |||
262 | public function beginTransaction() |
||
266 | |||
267 | public function commitTransaction() |
||
271 | |||
272 | public function rollbackTransaction() |
||
276 | |||
277 | /** |
||
278 | * @return bool |
||
279 | */ |
||
280 | public function getDebug() |
||
284 | } |
||
285 |