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 Base 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 Base, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | abstract class Base |
||
|
|
|||
| 10 | { |
||
| 11 | const PAGE_INDEX = "index"; |
||
| 12 | const PAGE_ALL_AUTHORS = "1"; |
||
| 13 | const PAGE_AUTHORS_FIRST_LETTER = "2"; |
||
| 14 | const PAGE_AUTHOR_DETAIL = "3"; |
||
| 15 | const PAGE_ALL_BOOKS = "4"; |
||
| 16 | const PAGE_ALL_BOOKS_LETTER = "5"; |
||
| 17 | const PAGE_ALL_SERIES = "6"; |
||
| 18 | const PAGE_SERIE_DETAIL = "7"; |
||
| 19 | const PAGE_OPENSEARCH = "8"; |
||
| 20 | const PAGE_OPENSEARCH_QUERY = "9"; |
||
| 21 | const PAGE_ALL_RECENT_BOOKS = "10"; |
||
| 22 | const PAGE_ALL_TAGS = "11"; |
||
| 23 | const PAGE_TAG_DETAIL = "12"; |
||
| 24 | const PAGE_BOOK_DETAIL = "13"; |
||
| 25 | const PAGE_ALL_CUSTOMS = "14"; |
||
| 26 | const PAGE_CUSTOM_DETAIL = "15"; |
||
| 27 | const PAGE_ABOUT = "16"; |
||
| 28 | const PAGE_ALL_LANGUAGES = "17"; |
||
| 29 | const PAGE_LANGUAGE_DETAIL = "18"; |
||
| 30 | const PAGE_CUSTOMIZE = "19"; |
||
| 31 | const PAGE_ALL_PUBLISHERS = "20"; |
||
| 32 | const PAGE_PUBLISHER_DETAIL = "21"; |
||
| 33 | const PAGE_ALL_RATINGS = "22"; |
||
| 34 | const PAGE_RATING_DETAIL = "23"; |
||
| 35 | |||
| 36 | const COMPATIBILITY_XML_ALDIKO = "aldiko"; |
||
| 37 | |||
| 38 | private static $db = NULL; |
||
| 39 | |||
| 40 | 144 | public static function isMultipleDatabaseEnabled () { |
|
| 44 | |||
| 45 | 49 | public static function useAbsolutePath () { |
|
| 51 | |||
| 52 | 56 | public static function noDatabaseSelected () { |
|
| 55 | |||
| 56 | 4 | View Code Duplication | public static function getDbList () { |
| 57 | 4 | global $config; |
|
| 58 | 4 | if (self::isMultipleDatabaseEnabled ()) { |
|
| 59 | 4 | return $config['calibre_directory']; |
|
| 60 | } else { |
||
| 61 | 1 | return array ("" => $config['calibre_directory']); |
|
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | 5 | View Code Duplication | public static function getDbNameList () { |
| 73 | |||
| 74 | 1 | View Code Duplication | public static function getDbName ($database = NULL) { |
| 75 | 1 | global $config; |
|
| 76 | 1 | if (self::isMultipleDatabaseEnabled ()) { |
|
| 77 | 1 | if (is_null ($database)) $database = GetUrlParam (DB, 0); |
|
| 78 | 1 | if (!is_null($database) && !preg_match('/^\d+$/', $database)) { |
|
| 79 | self::error ($database); |
||
| 80 | } |
||
| 81 | 1 | $array = array_keys ($config['calibre_directory']); |
|
| 82 | 1 | return $array[$database]; |
|
| 83 | } |
||
| 84 | return ""; |
||
| 85 | } |
||
| 86 | |||
| 87 | 126 | View Code Duplication | public static function getDbDirectory ($database = NULL) { |
| 88 | 126 | global $config; |
|
| 89 | 126 | if (self::isMultipleDatabaseEnabled ()) { |
|
| 90 | 9 | if (is_null ($database)) $database = GetUrlParam (DB, 0); |
|
| 91 | 9 | if (!is_null($database) && !preg_match('/^\d+$/', $database)) { |
|
| 92 | self::error ($database); |
||
| 93 | } |
||
| 94 | 9 | $array = array_values ($config['calibre_directory']); |
|
| 95 | 9 | return $array[$database]; |
|
| 96 | } |
||
| 97 | 117 | return $config['calibre_directory']; |
|
| 98 | } |
||
| 99 | |||
| 100 | |||
| 101 | 98 | public static function getDbFileName ($database = NULL) { |
|
| 104 | |||
| 105 | 2 | private static function error ($database) { |
|
| 111 | |||
| 112 | 159 | public static function getDb ($database = NULL) { |
|
| 129 | |||
| 130 | 4 | public static function checkDatabaseAvailability () { |
|
| 141 | |||
| 142 | 103 | public static function clearDb () { |
|
| 145 | |||
| 146 | 24 | public static function executeQuerySingle ($query, $database = NULL) { |
|
| 149 | |||
| 150 | 19 | public static function getCountGeneric($table, $id, $pageId, $numberOfString = NULL) { |
|
| 161 | |||
| 162 | 35 | public static function getEntryArrayWithBookNumber ($query, $columns, $params, $category) { |
|
| 163 | /* @var $result PDOStatement */ |
||
| 164 | |||
| 183 | |||
| 184 | 75 | public static function executeQuery($query, $columns, $filter, $params, $n, $database = NULL, $numberPerPage = NULL) { |
|
| 212 | |||
| 213 | } |
||
| 214 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.