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 XoopsMySQLDatabase 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 XoopsMySQLDatabase, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class XoopsMySQLDatabase extends XoopsDatabase |
||
|
|||
31 | { |
||
32 | |||
33 | /** |
||
34 | * @var object keep track of last result since we need it for getAffectedRows |
||
35 | */ |
||
36 | private $lastResult = null; |
||
37 | |||
38 | /** |
||
39 | * Database connection |
||
40 | * |
||
41 | * @var Xoops\Core\Database\Connection; |
||
42 | */ |
||
43 | public $conn; |
||
44 | |||
45 | /** |
||
46 | * Database connection is established |
||
47 | * |
||
48 | * @var boolean |
||
49 | */ |
||
50 | private $connect = false; |
||
51 | |||
52 | /** |
||
53 | * Database connection |
||
54 | * |
||
55 | * @var resource |
||
56 | */ |
||
57 | private $selectdb; |
||
58 | |||
59 | /** |
||
60 | * Issue a deprecated warning once per session |
||
61 | * |
||
62 | * @return void |
||
63 | */ |
||
64 | 1 | protected function deprecated() |
|
77 | |||
78 | |||
79 | /** |
||
80 | * connect to the database |
||
81 | * |
||
82 | * @param bool $selectdb select the database now? |
||
83 | * |
||
84 | * @return bool successful? |
||
85 | * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector. |
||
86 | */ |
||
87 | public function connect($selectdb = true) |
||
94 | |||
95 | |||
96 | /** |
||
97 | * generate an ID for a new row |
||
98 | * |
||
99 | * This is for compatibility only. Will always return 0, because MySQL supports |
||
100 | * autoincrement for primary keys. |
||
101 | * |
||
102 | * @param string $sequence name of the sequence from which to get the next ID |
||
103 | * |
||
104 | * @return int always 0, because mysql has support for autoincrement |
||
105 | * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector. |
||
106 | */ |
||
107 | 1 | public function genId($sequence) |
|
112 | |||
113 | /** |
||
114 | * Get a result row as an enumerated array |
||
115 | * |
||
116 | * @param resource $result resource to get result from |
||
117 | * |
||
118 | * @return array |
||
119 | * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector. |
||
120 | */ |
||
121 | public function fetchRow($result) |
||
129 | |||
130 | /** |
||
131 | * Fetch a result row as an associative array |
||
132 | * |
||
133 | * @param resource $result resource to get result from |
||
134 | * |
||
135 | * @return array |
||
136 | * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector. |
||
137 | */ |
||
138 | public function fetchArray($result) |
||
147 | |||
148 | /** |
||
149 | * Fetch a result row as an associative array |
||
150 | * |
||
151 | * @param resource $result resource to get result from |
||
152 | * |
||
153 | * @return array |
||
154 | * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector. |
||
155 | */ |
||
156 | public function fetchBoth($result) |
||
165 | |||
166 | /** |
||
167 | * Fetch a result row as an object |
||
168 | * |
||
169 | * @param resource $result resource to get result from |
||
170 | * |
||
171 | * @return object|stdClass |
||
172 | * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector. |
||
173 | */ |
||
174 | public function fetchObject($result) |
||
183 | |||
184 | /** |
||
185 | * Get the ID generated from the previous INSERT operation |
||
186 | * |
||
187 | * @return int |
||
188 | * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector. |
||
189 | */ |
||
190 | public function getInsertId() |
||
195 | |||
196 | /** |
||
197 | * Get number of rows in result |
||
198 | * |
||
199 | * @param resource $result the resource containing the number of rows |
||
200 | * |
||
201 | * @return int the number of rows to return |
||
202 | * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector. |
||
203 | */ |
||
204 | public function getRowsNum($result) |
||
210 | |||
211 | /** |
||
212 | * Get number of affected rows |
||
213 | * |
||
214 | * @return int |
||
215 | * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector. |
||
216 | */ |
||
217 | public function getAffectedRows() |
||
222 | |||
223 | /** |
||
224 | * Close MySQL connection |
||
225 | * |
||
226 | * @return void |
||
227 | * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector. |
||
228 | */ |
||
229 | public function close() |
||
234 | |||
235 | /** |
||
236 | * will free all memory associated with the result identifier result. |
||
237 | * |
||
238 | * @param resource $result query result |
||
239 | * |
||
240 | * @return bool TRUE on success or FALSE on failure. |
||
241 | * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector. |
||
242 | */ |
||
243 | public function freeRecordSet($result) |
||
249 | |||
250 | /** |
||
251 | * Returns the text of the error message from previous MySQL operation |
||
252 | * |
||
253 | * @return bool Returns the error text from the last MySQL function, |
||
254 | * or '' (the empty string) if no error occurred. |
||
255 | * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector. |
||
256 | */ |
||
257 | public function error() |
||
263 | |||
264 | /** |
||
265 | * Returns the numerical value of the error message from previous |
||
266 | * MySQL operation |
||
267 | * |
||
268 | * @return int Returns the error number from the last MySQL function |
||
269 | * , or 0 (zero) if no error occurred. |
||
270 | * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector. |
||
271 | */ |
||
272 | public function errno() |
||
278 | |||
279 | /** |
||
280 | * Returns escaped string text with single |
||
281 | * quotes around it to be safely stored in database |
||
282 | * |
||
283 | * @param string $str unescaped string text |
||
284 | * |
||
285 | * @return string escaped string text with single quotes around |
||
286 | * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector. |
||
287 | */ |
||
288 | public function quoteString($str) |
||
294 | |||
295 | /** |
||
296 | * Quotes a string for use in a query. |
||
297 | * |
||
298 | * @param string $string string to quote |
||
299 | * |
||
300 | * @return string |
||
301 | * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector. |
||
302 | */ |
||
303 | public function quote($string) |
||
309 | |||
310 | /** |
||
311 | * Escapes a string for use in a query. Does not add quotes. |
||
312 | * |
||
313 | * @param string $string string to escape |
||
314 | * |
||
315 | * @return string |
||
316 | * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector. |
||
317 | */ |
||
318 | public function escape($string) |
||
325 | |||
326 | /** |
||
327 | * perform a query on the database |
||
328 | * |
||
329 | * @param string $sql a valid MySQL query |
||
330 | * @param int $limit number of records to return |
||
331 | * @param int $start offset of first record to return |
||
332 | * |
||
333 | * @return bool|resource query result or FALSE if successful |
||
334 | * or TRUE if successful and no result |
||
335 | * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector. |
||
336 | */ |
||
337 | public function queryF($sql, $limit = 0, $start = 0) |
||
338 | { |
||
339 | $this->deprecated(); |
||
340 | |||
341 | if (!empty($limit)) { |
||
342 | if (empty($start)) { |
||
343 | $start = 0; |
||
344 | } |
||
345 | $sql = $sql . ' LIMIT ' . (int) $start . ', ' . (int) $limit; |
||
346 | } |
||
347 | $events = \Xoops::getInstance()->events(); |
||
348 | $events->triggerEvent('core.database.query.start'); |
||
349 | try { |
||
350 | $result = $this->conn->query($sql); |
||
351 | } catch (Exception $e) { |
||
352 | $result=false; |
||
353 | } |
||
354 | $this->lastResult = $result; |
||
355 | $events->triggerEvent('core.database.query.end'); |
||
356 | |||
357 | if ($result) { |
||
358 | $events->triggerEvent('core.database.query.success', (array($sql))); |
||
359 | return $result; |
||
360 | } else { |
||
361 | $events->triggerEvent('core.database.query.failure', (array($sql, $this))); |
||
362 | return false; |
||
363 | } |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * perform a query |
||
368 | * |
||
369 | * This method is empty and does nothing! It should therefore only be |
||
370 | * used if nothing is exactly what you want done! ;-) |
||
371 | * |
||
372 | * @param string $sql a valid MySQL query |
||
373 | * @param int $limit number of records to return |
||
374 | * @param int $start offset of first record to return |
||
375 | * |
||
376 | * @return void |
||
377 | * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector. |
||
378 | */ |
||
379 | public function query($sql, $limit = 0, $start = 0) |
||
380 | { |
||
381 | $this->deprecated(); |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * perform queries from SQL dump file in a batch |
||
386 | * |
||
387 | * @param string $file file path to an SQL dump file |
||
388 | * |
||
389 | * @return bool FALSE if failed reading SQL file or TRUE |
||
390 | * if the file has been read and queries executed |
||
391 | * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector. |
||
392 | */ |
||
393 | View Code Duplication | public function queryFromFile($file) |
|
413 | |||
414 | /** |
||
415 | * Get field name |
||
416 | * |
||
417 | * @param resource $result query result |
||
418 | * @param int $offset numerical field index |
||
419 | * |
||
420 | * @return string |
||
421 | * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector. |
||
422 | */ |
||
423 | public function getFieldName($result, $offset) |
||
435 | |||
436 | /** |
||
437 | * Get field type |
||
438 | * |
||
439 | * @param resource $result query result |
||
440 | * @param int $offset numerical field index |
||
441 | * |
||
442 | * @return string |
||
443 | * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector. |
||
444 | */ |
||
445 | public function getFieldType($result, $offset) |
||
446 | { |
||
447 | $this->deprecated(); |
||
448 | |||
449 | try { |
||
450 | $temp = ($result->getColumnMeta($offset)); |
||
451 | $t = $temp['native_type']; |
||
452 | |||
453 | $temp = (string)( |
||
454 | ((($t === 'STRING') || ($t === 'VAR_STRING') ) ? 'string' : '' ) . |
||
455 | ( (in_array($t, array('TINY', 'SHORT', 'LONG', 'LONGLONG', 'INT24'))) ? 'int' : '' ) . |
||
456 | ( (in_array($t, array('FLOAT', 'DOUBLE', 'DECIMAL', 'NEWDECIMAL'))) ? 'real' : '' ) . |
||
457 | ( ($t === 'TIMESTAMP') ? 'timestamp' : '' ) . |
||
458 | ( ($t === 'YEAR') ? 'year' : '') . |
||
459 | ( (($t === 'DATE') || ($t === 'NEWDATE') ) ? 'date' : '' ) . |
||
460 | ( ($t === 'TIME') ? 'time' : '' ) . |
||
461 | ( ($t === 'SET') ? 'set' : '' ) . |
||
462 | ( ($t === 'ENUM') ? 'enum' : '' ) . |
||
463 | ( ($t === 'GEOMETRY') ? 'geometry' : '' ) . |
||
464 | ( ($t === 'DATETIME') ? 'datetime' : '' ) . |
||
465 | ( (in_array($t, array('TINY_BLOB', 'BLOB', 'MEDIUM_BLOB', 'LONG_BLOB' ))) ? 'blob' : '' ) . |
||
466 | ( ($t === 'NULL') ? 'null' : '' ) |
||
467 | ); |
||
468 | return $temp; |
||
469 | } catch (PDOException $e) { |
||
470 | return null; |
||
471 | } |
||
472 | } |
||
473 | |||
474 | /** |
||
475 | * Get number of fields in result |
||
476 | * |
||
477 | * @param resource $result query result |
||
478 | * |
||
479 | * @return int |
||
480 | * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector. |
||
481 | */ |
||
482 | public function getFieldsNum($result) |
||
488 | |||
489 | /** |
||
490 | * getServerVersion get version of the mysql server |
||
491 | * |
||
492 | * @return string |
||
493 | */ |
||
494 | public function getServerVersion() |
||
503 | } |
||
504 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.