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() |
|
| 45 | |||
| 46 | 49 | public static function useAbsolutePath() |
|
| 53 | |||
| 54 | 56 | public static function noDatabaseSelected() |
|
| 58 | |||
| 59 | 4 | View Code Duplication | public static function getDbList() |
| 68 | |||
| 69 | 5 | View Code Duplication | public static function getDbNameList() |
| 78 | |||
| 79 | 1 | View Code Duplication | public static function getDbName($database = NULL) |
| 92 | |||
| 93 | 126 | View Code Duplication | public static function getDbDirectory($database = NULL) |
| 106 | |||
| 107 | 98 | public static function getDbFileName($database = NULL) |
|
| 111 | |||
| 112 | 2 | private static function error($database) |
|
| 119 | |||
| 120 | 159 | public static function getDb ($database = NULL) |
|
| 138 | |||
| 139 | 4 | public static function checkDatabaseAvailability() |
|
| 151 | |||
| 152 | 103 | public static function clearDb() |
|
| 156 | |||
| 157 | 24 | public static function executeQuerySingle($query, $database = NULL) |
|
| 161 | |||
| 162 | 19 | public static function getCountGeneric($table, $id, $pageId, $numberOfString = NULL) |
|
| 176 | |||
| 177 | 35 | public static function getEntryArrayWithBookNumber($query, $columns, $params, $category) |
|
| 199 | |||
| 200 | 75 | public static function executeQuery($query, $columns, $filter, $params, $n, $database = NULL, $numberPerPage = NULL) |
|
| 228 | } |
||
| 229 |
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.