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 | |||
8 | View Code Duplication | public function __construct($params) |
|
|
|||
9 | { |
||
10 | if(isset($params['user'])) |
||
11 | { |
||
12 | $this->pdo = new \PDO($params['dsn'], $params['user'], $params['pass']); |
||
13 | } |
||
14 | else |
||
15 | { |
||
16 | $this->pdo = new \PDO($params['dsn']); |
||
17 | } |
||
18 | } |
||
19 | |||
20 | /** |
||
21 | * Get the number of rows affected by the query |
||
22 | * |
||
23 | * @param string $sql The SQL string |
||
24 | * |
||
25 | * @return integer The number of rows affected by the query |
||
26 | */ |
||
27 | private function _get_row_count_for_query($sql) |
||
42 | |||
43 | View Code Duplication | function _tableExistsNoPrefix($name) |
|
55 | |||
56 | View Code Duplication | function _tableExists($name) |
|
68 | |||
69 | View Code Duplication | function _viewExists($name) |
|
81 | |||
82 | function tableExists($name) |
||
98 | |||
99 | function getTable($name) |
||
115 | |||
116 | /** |
||
117 | * @param array $sort The array to sort by or false to not sort |
||
118 | */ |
||
119 | private function getOrderByClause($sort) |
||
134 | |||
135 | private function getLimitClause($count, $skip) |
||
149 | |||
150 | /** |
||
151 | * Read data from the specified SQL table |
||
152 | * |
||
153 | * @param string $tablename The name of the table to read from |
||
154 | * @param string $where The where caluse of the SQL statement |
||
155 | * @param string $select The colums to read |
||
156 | * @param string $count The number of rows to read |
||
157 | * @param string $skip The number of rows to skip over |
||
158 | * @param array $sort The array to sort by or false to not sort |
||
159 | * |
||
160 | * @return array An array of all the returned records |
||
161 | */ |
||
162 | public function read($tablename, $where = false, $select = '*', $count = false, $skip = false, $sort = false) |
||
195 | |||
196 | View Code Duplication | function update($tablename, $where, $data) |
|
217 | |||
218 | View Code Duplication | function create($tablename, $data) |
|
240 | |||
241 | function delete($tablename, $where) |
||
250 | |||
251 | function raw_query($sql) |
||
261 | } |
||
262 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
263 |
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.