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 () { |
64 | |||
65 | 5 | View Code Duplication | public static function getDbNameList () { |
73 | |||
74 | 1 | View Code Duplication | public static function getDbName ($database = NULL) { |
86 | |||
87 | 126 | View Code Duplication | public static function getDbDirectory ($database = NULL) { |
99 | |||
100 | // -DC- Add image directory |
||
101 | 98 | public static function getImgDirectory ($database = NULL) { |
|
110 | |||
111 | public static function getDbFileName ($database = NULL) { |
||
114 | |||
115 | 98 | private static function error ($database) { |
|
121 | 98 | ||
122 | public static function getDb ($database = NULL) { |
||
139 | 2 | ||
140 | public static function checkDatabaseAvailability () { |
||
151 | 19 | ||
152 | 18 | public static function clearDb () { |
|
155 | 19 | ||
156 | 19 | public static function executeQuerySingle ($query, $database = NULL) { |
|
159 | 19 | ||
160 | public static function getCountGeneric($table, $id, $pageId, $numberOfString = NULL) { |
||
171 | 25 | ||
172 | 25 | public static function getEntryArrayWithBookNumber ($query, $columns, $params, $category) { |
|
193 | 73 | ||
194 | public static function executeQuery($query, $columns, $filter, $params, $n, $database = NULL, $numberPerPage = NULL) { |
||
222 | |||
223 | } |
||
224 |
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.