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 SQLDataSet 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 SQLDataSet, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
4 | class SQLDataSet extends DataSet |
||
5 | { |
||
6 | protected $pdo; |
||
7 | protected $params; |
||
8 | |||
9 | /** |
||
10 | * Create a new SQLDataSet |
||
11 | * |
||
12 | * @param array $params An array containing atleast 'dsn' and possibly 'user' and 'pass' |
||
13 | */ |
||
14 | public function __construct($params) |
||
26 | |||
27 | public function __sleep() |
||
32 | |||
33 | public function __wakeup() |
||
37 | |||
38 | protected function connect() |
||
49 | |||
50 | /** |
||
51 | * Get the number of rows affected by the query |
||
52 | * |
||
53 | * @param string $sql The SQL string |
||
54 | * |
||
55 | * @return integer The number of rows affected by the query |
||
56 | */ |
||
57 | private function _get_row_count_for_query($sql) |
||
72 | |||
73 | private function _tableExistsNoPrefix($name) |
||
85 | |||
86 | private function _tableExists($name) |
||
90 | |||
91 | private function _viewExists($name) |
||
95 | |||
96 | public function tableExists($name) |
||
112 | |||
113 | public function getTable($name) |
||
129 | |||
130 | /** |
||
131 | * @param boolean|array $sort The array to sort by or false to not sort |
||
132 | */ |
||
133 | private function getOrderByClause($sort) |
||
148 | |||
149 | /** |
||
150 | * Convert OData style $count and $skip into SQL LIMIT |
||
151 | * |
||
152 | * @param boolean|string $count The number of items to return |
||
153 | * @param boolean|string $skip The number of items to skip |
||
154 | */ |
||
155 | private function getLimitClause($count, $skip) |
||
169 | |||
170 | /** |
||
171 | * Read data from the specified SQL table |
||
172 | * |
||
173 | * @param string $tablename The name of the table to read from |
||
174 | * @param boolean|string $where The where caluse of the SQL statement |
||
175 | * @param string $select The colums to read |
||
176 | * @param boolean|string $count The number of rows to read |
||
177 | * @param boolean|string $skip The number of rows to skip over |
||
178 | * @param boolean|array $sort The array to sort by or false to not sort |
||
179 | * |
||
180 | * @return false|array An array of all the returned records |
||
181 | */ |
||
182 | public function read($tablename, $where = false, $select = '*', $count = false, $skip = false, $sort = false) |
||
215 | |||
216 | /** |
||
217 | * Perform an SQL update on the specified table |
||
218 | * |
||
219 | * @param string $tablename The name of the table to insert to |
||
220 | * @param string $where The where clause in SQL format |
||
221 | * @param mixed $data The data to write to the table |
||
222 | * |
||
223 | * @return boolean true if successful, false otherwise |
||
224 | */ |
||
225 | public function update($tablename, $where, $data) |
||
265 | |||
266 | /** |
||
267 | * Perform an SQL insert on the specified table |
||
268 | * |
||
269 | * @param string $tablename The name of the table to insert to |
||
270 | * @param mixed $data The data to write to the table |
||
271 | * |
||
272 | * @return boolean true if successful, false otherwise |
||
273 | */ |
||
274 | public function create($tablename, $data) |
||
306 | |||
307 | /** |
||
308 | * Perform an SQL delete on the specified table |
||
309 | * |
||
310 | * @param string $tablename The name of the table to insert to |
||
311 | * @param string $where The where clause in SQL format |
||
312 | * |
||
313 | * @return boolean true if successful, false otherwise |
||
314 | */ |
||
315 | public function delete($tablename, $where) |
||
324 | |||
325 | /** |
||
326 | * Perform an SQL query |
||
327 | * |
||
328 | * @param string $sql The raw SQL |
||
329 | * |
||
330 | * @return mixed false on a failure, an array of data otherwise |
||
331 | */ |
||
332 | public function raw_query($sql) |
||
342 | |||
343 | public function getLastError() |
||
347 | } |
||
348 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
349 |
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.