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) { |
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) { |
|
106 | 2 | if (php_sapi_name() != "cli") { |
|
107 | header("location: checkconfig.php?err=1"); |
||
108 | } |
||
109 | 2 | throw new Exception("Database <{$database}> not found."); |
|
110 | } |
||
111 | |||
112 | 159 | public static function getDb ($database = NULL) { |
|
113 | 159 | if (is_null (self::$db)) { |
|
114 | try { |
||
115 | 98 | if (is_readable (self::getDbFileName ($database))) { |
|
116 | 97 | self::$db = new PDO('sqlite:'. self::getDbFileName ($database)); |
|
117 | 97 | if (useNormAndUp ()) { |
|
118 | 97 | self::$db->sqliteCreateFunction ('normAndUp', 'normAndUp', 1); |
|
119 | } |
||
120 | } else { |
||
121 | 98 | self::error ($database); |
|
122 | } |
||
123 | 2 | } catch (Exception $e) { |
|
124 | 2 | self::error ($database); |
|
125 | } |
||
126 | } |
||
127 | 158 | return self::$db; |
|
128 | } |
||
129 | |||
130 | 4 | public static function checkDatabaseAvailability () { |
|
131 | 4 | if (self::noDatabaseSelected ()) { |
|
132 | 3 | for ($i = 0; $i < count (self::getDbList ()); $i++) { |
|
133 | 3 | self::getDb ($i); |
|
134 | 2 | self::clearDb (); |
|
135 | } |
||
136 | } else { |
||
137 | 1 | self::getDb (); |
|
138 | } |
||
139 | 2 | return true; |
|
140 | } |
||
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) { |
|
151 | 19 | if (!$numberOfString) { |
|
152 | 18 | $numberOfString = $table . ".alphabetical"; |
|
153 | } |
||
154 | 19 | $count = self::executeQuerySingle ('select count(*) from ' . $table); |
|
161 | |||
162 | 35 | public static function getEntryArrayWithBookNumber ($query, $columns, $params, $category) { |
|
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.