Total Complexity | 53 |
Total Lines | 366 |
Duplicated Lines | 8.74 % |
Changes | 0 |
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 CI_DB_utility 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.
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 CI_DB_utility, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
47 | abstract class CI_DB_utility { |
||
48 | |||
49 | /** |
||
50 | * Database object |
||
51 | * |
||
52 | * @var object |
||
53 | */ |
||
54 | protected $db; |
||
55 | |||
56 | // -------------------------------------------------------------------- |
||
57 | |||
58 | /** |
||
59 | * List databases statement |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $_list_databases = FALSE; |
||
64 | |||
65 | /** |
||
66 | * OPTIMIZE TABLE statement |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $_optimize_table = FALSE; |
||
71 | |||
72 | /** |
||
73 | * REPAIR TABLE statement |
||
74 | * |
||
75 | * @var string |
||
76 | */ |
||
77 | protected $_repair_table = FALSE; |
||
78 | |||
79 | // -------------------------------------------------------------------- |
||
80 | |||
81 | /** |
||
82 | * Class constructor |
||
83 | * |
||
84 | * @param object &$db Database object |
||
85 | * @return void |
||
86 | */ |
||
87 | public function __construct(&$db) |
||
88 | { |
||
89 | $this->db =& $db; |
||
90 | } |
||
91 | |||
92 | // -------------------------------------------------------------------- |
||
93 | |||
94 | /** |
||
95 | * List databases |
||
96 | * |
||
97 | * @return array |
||
98 | */ |
||
99 | public function list_databases() |
||
125 | } |
||
126 | |||
127 | // -------------------------------------------------------------------- |
||
128 | |||
129 | /** |
||
130 | * Determine if a particular database exists |
||
131 | * |
||
132 | * @param string $database_name |
||
133 | * @return bool |
||
134 | */ |
||
135 | public function database_exists($database_name) |
||
136 | { |
||
137 | return in_array($database_name, $this->list_databases()); |
||
138 | } |
||
139 | |||
140 | // -------------------------------------------------------------------- |
||
141 | |||
142 | /** |
||
143 | * Optimize Table |
||
144 | * |
||
145 | * @param string $table_name |
||
146 | * @return mixed |
||
147 | */ |
||
148 | View Code Duplication | public function optimize_table($table_name) |
|
|
|||
149 | { |
||
150 | if ($this->_optimize_table === FALSE) |
||
151 | { |
||
152 | return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; |
||
153 | } |
||
154 | |||
155 | $query = $this->db->query(sprintf($this->_optimize_table, $this->db->escape_identifiers($table_name))); |
||
156 | if ($query !== FALSE) |
||
157 | { |
||
158 | $query = $query->result_array(); |
||
159 | return current($query); |
||
160 | } |
||
161 | |||
162 | return FALSE; |
||
163 | } |
||
164 | |||
165 | // -------------------------------------------------------------------- |
||
166 | |||
167 | /** |
||
168 | * Optimize Database |
||
169 | * |
||
170 | * @return mixed |
||
171 | */ |
||
172 | public function optimize_database() |
||
173 | { |
||
174 | if ($this->_optimize_table === FALSE) |
||
175 | { |
||
176 | return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; |
||
177 | } |
||
178 | |||
179 | $result = array(); |
||
180 | foreach ($this->db->list_tables() as $table_name) |
||
181 | { |
||
182 | $res = $this->db->query(sprintf($this->_optimize_table, $this->db->escape_identifiers($table_name))); |
||
183 | if (is_bool($res)) |
||
184 | { |
||
185 | return $res; |
||
186 | } |
||
187 | |||
188 | // Build the result array... |
||
189 | $res = $res->result_array(); |
||
190 | $res = current($res); |
||
191 | $key = str_replace($this->db->database.'.', '', current($res)); |
||
192 | $keys = array_keys($res); |
||
193 | unset($res[$keys[0]]); |
||
194 | |||
195 | $result[$key] = $res; |
||
196 | } |
||
197 | |||
198 | return $result; |
||
199 | } |
||
200 | |||
201 | // -------------------------------------------------------------------- |
||
202 | |||
203 | /** |
||
204 | * Repair Table |
||
205 | * |
||
206 | * @param string $table_name |
||
207 | * @return mixed |
||
208 | */ |
||
209 | View Code Duplication | public function repair_table($table_name) |
|
210 | { |
||
211 | if ($this->_repair_table === FALSE) |
||
212 | { |
||
213 | return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; |
||
214 | } |
||
215 | |||
216 | $query = $this->db->query(sprintf($this->_repair_table, $this->db->escape_identifiers($table_name))); |
||
217 | if (is_bool($query)) |
||
218 | { |
||
219 | return $query; |
||
220 | } |
||
221 | |||
222 | $query = $query->result_array(); |
||
223 | return current($query); |
||
224 | } |
||
225 | |||
226 | // -------------------------------------------------------------------- |
||
227 | |||
228 | /** |
||
229 | * Generate CSV from a query result object |
||
230 | * |
||
231 | * @param object $query Query result object |
||
232 | * @param string $delim Delimiter (default: ,) |
||
233 | * @param string $newline Newline character (default: \n) |
||
234 | * @param string $enclosure Enclosure (default: ") |
||
235 | * @return string |
||
236 | */ |
||
237 | public function csv_from_result($query, $delim = ',', $newline = "\n", $enclosure = '"') |
||
238 | { |
||
239 | if ( ! is_object($query) OR ! method_exists($query, 'list_fields')) { |
||
240 | error_log('You must submit a valid result object'); |
||
241 | } |
||
242 | $out = ''; |
||
243 | // First generate the headings from the table column names |
||
244 | foreach ($query->list_fields() as $name) { |
||
245 | $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $name).$enclosure.$delim; |
||
246 | } |
||
247 | $out = substr($out, 0, -strlen($delim)).$newline; |
||
248 | // Next blast through the result array and build out the rows |
||
249 | while ($row = $query->unbuffered_row('array')) { |
||
250 | $line = array(); |
||
251 | foreach ($row as $ite) { |
||
252 | $line[] = $enclosure.str_replace($enclosure, $enclosure.$enclosure, $item).$enclosure; |
||
253 | } |
||
254 | $out .= implode($delim, $line).$newline; |
||
255 | } |
||
256 | return $out; |
||
257 | } |
||
258 | |||
259 | // -------------------------------------------------------------------- |
||
260 | |||
261 | /** |
||
262 | * Generate XML data from a query result object |
||
263 | * |
||
264 | * @param object $query Query result object |
||
265 | * @param array $params Any preferences |
||
266 | * @return string |
||
267 | */ |
||
268 | public function xml_from_result($query, $params = array()) |
||
303 | } |
||
304 | |||
305 | // -------------------------------------------------------------------- |
||
306 | |||
307 | /** |
||
308 | * Database Backup |
||
309 | * |
||
310 | * @param array $params |
||
311 | * @return string |
||
312 | */ |
||
313 | public function backup($params = array()) |
||
413 | } |
||
414 | |||
416 |
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.