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 XoopsDatabaseManager 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 XoopsDatabaseManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class XoopsDatabaseManager |
||
28 | { |
||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | private $s_tables = array(); |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | private $f_tables = array(); |
||
38 | |||
39 | /** |
||
40 | * @var XoopsDatabase |
||
41 | */ |
||
42 | public $db; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | public $successStrings = array(); |
||
48 | |||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | public $failureStrings = array(); |
||
53 | |||
54 | /** |
||
55 | *Construct declaration |
||
56 | */ |
||
57 | 1 | public function __construct() |
|
58 | { |
||
59 | 1 | $xoops = Xoops::getInstance(); |
|
60 | 1 | $xoops->db(); |
|
61 | 1 | global $xoopsDB; |
|
|
|||
62 | 1 | $this->db = $xoopsDB; |
|
63 | 1 | $this->db->setPrefix(\XoopsBaseConfig::get('db-prefix')); |
|
64 | 1 | $this->successStrings = array( |
|
65 | 'create' => XoopsLocale::SF_TABLE_CREATED, |
||
66 | 'insert' => XoopsLocale::SF_ENTRIES_INSERTED_TO_TABLE, |
||
67 | 'alter' => XoopsLocale::SF_TABLE_UPDATED, |
||
68 | 'drop' => XoopsLocale::SF_TABLE_DROPPED, |
||
69 | ); |
||
70 | 1 | $this->failureStrings = array( |
|
71 | 'create' => XoopsLocale::EF_TABLE_NOT_CREATED, |
||
72 | 'insert' => XoopsLocale::EF_ENTRIES_NOT_INSERTED_TO_TABLE, |
||
73 | 'alter' => XoopsLocale::EF_TABLE_NOT_UPDATED, |
||
74 | 'drop' => XoopsLocale::EF_TABLE_NOT_DROPPED, |
||
75 | ); |
||
76 | 1 | } |
|
77 | |||
78 | /** |
||
79 | * Is the database connectable? |
||
80 | * |
||
81 | * @return bool is it connectable? |
||
82 | */ |
||
83 | public function isConnectable() |
||
87 | |||
88 | /** |
||
89 | * Checks if a database exists |
||
90 | * |
||
91 | * @return bool returns if exists |
||
92 | */ |
||
93 | public function dbExists() |
||
97 | |||
98 | /** |
||
99 | * creates a database table |
||
100 | * |
||
101 | * @return bool return if successful |
||
102 | */ |
||
103 | View Code Duplication | public function createDB() |
|
111 | |||
112 | /** |
||
113 | * Loads a query from a file |
||
114 | * |
||
115 | * @param string $sql_file_path name of file to read |
||
116 | * @param bool $force weither to force the query or not |
||
117 | * |
||
118 | * @return bool |
||
119 | */ |
||
120 | public function queryFromFile($sql_file_path, $force = false) |
||
121 | { |
||
122 | if (!XoopsLoad::fileExists($sql_file_path)) { |
||
123 | return false; |
||
124 | } |
||
125 | $queryFunc = (bool)$force ? "queryF" : "query"; |
||
126 | $sql_query = trim(fread(fopen($sql_file_path, 'r'), filesize($sql_file_path))); |
||
127 | SqlUtility::splitMySqlFile($pieces, $sql_query); |
||
128 | $this->db->connect(); |
||
129 | foreach ($pieces as $piece) { |
||
130 | $piece = trim($piece); |
||
131 | // [0] contains the prefixed query |
||
132 | // [4] contains unprefixed table name |
||
133 | $prefixed_query = SqlUtility::prefixQuery($piece, $this->db->prefix()); |
||
134 | if ($prefixed_query != false) { |
||
135 | $table = $this->db->prefix($prefixed_query[4]); |
||
136 | if ($prefixed_query[1] === 'CREATE TABLE') { |
||
137 | View Code Duplication | if ($this->db->$queryFunc($prefixed_query[0]) != false) { |
|
138 | if (!isset($this->s_tables['create'][$table])) { |
||
139 | $this->s_tables['create'][$table] = 1; |
||
140 | } |
||
141 | } else { |
||
142 | if (!isset($this->f_tables['create'][$table])) { |
||
143 | $this->f_tables['create'][$table] = 1; |
||
144 | } |
||
145 | } |
||
146 | } else { |
||
147 | if ($prefixed_query[1] === 'INSERT INTO') { |
||
148 | View Code Duplication | if ($this->db->$queryFunc($prefixed_query[0]) != false) { |
|
149 | if (!isset($this->s_tables['insert'][$table])) { |
||
150 | $this->s_tables['insert'][$table] = 1; |
||
151 | } else { |
||
152 | $this->s_tables['insert'][$table]++; |
||
153 | } |
||
154 | } else { |
||
155 | if (!isset($this->f_tables['insert'][$table])) { |
||
156 | $this->f_tables['insert'][$table] = 1; |
||
157 | } else { |
||
158 | $this->f_tables['insert'][$table]++; |
||
159 | } |
||
160 | } |
||
161 | } else { |
||
162 | if ($prefixed_query[1] === 'ALTER TABLE') { |
||
163 | View Code Duplication | if ($this->db->$queryFunc($prefixed_query[0]) != false) { |
|
164 | if (!isset($this->s_tables['alter'][$table])) { |
||
165 | $this->s_tables['alter'][$table] = 1; |
||
166 | } |
||
167 | } else { |
||
168 | if (!isset($this->s_tables['alter'][$table])) { |
||
169 | $this->f_tables['alter'][$table] = 1; |
||
170 | } |
||
171 | } |
||
172 | View Code Duplication | } else { |
|
173 | if ($prefixed_query[1] === 'DROP TABLE') { |
||
174 | if ($this->db->$queryFunc('DROP TABLE ' . $table) != false) { |
||
175 | if (!isset($this->s_tables['drop'][$table])) { |
||
176 | $this->s_tables['drop'][$table] = 1; |
||
177 | } |
||
178 | } else { |
||
179 | if (!isset($this->s_tables['drop'][$table])) { |
||
180 | $this->f_tables['drop'][$table] = 1; |
||
181 | } |
||
182 | } |
||
183 | } |
||
184 | } |
||
185 | } |
||
186 | } |
||
187 | } |
||
188 | } |
||
189 | return true; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * returns a report |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | View Code Duplication | public function report() |
|
198 | { |
||
199 | $commands = array('create', 'insert', 'alter', 'drop'); |
||
200 | $content = '<ul class="log">'; |
||
201 | foreach ($commands as $cmd) { |
||
202 | if (!@empty($this->s_tables[$cmd])) { |
||
203 | foreach ($this->s_tables[$cmd] as $key => $val) { |
||
204 | $content .= '<li class="success">'; |
||
205 | $content .= ($cmd !== 'insert') |
||
206 | ? sprintf($this->successStrings[$cmd], $key) |
||
207 | : sprintf($this->successStrings[$cmd], $val, $key); |
||
208 | $content .= "</li>\n"; |
||
209 | } |
||
210 | } |
||
211 | } |
||
212 | foreach ($commands as $cmd) { |
||
213 | if (!@empty($this->f_tables[$cmd])) { |
||
214 | foreach ($this->f_tables[$cmd] as $key => $val) { |
||
215 | $content .= '<li class="failure">'; |
||
216 | $content .= ($cmd !== 'insert') |
||
217 | ? sprintf($this->failureStrings[$cmd], $key) |
||
218 | : sprintf($this->failureStrings[$cmd], $val, $key); |
||
219 | $content .= "</li>\n"; |
||
220 | } |
||
221 | } |
||
222 | } |
||
223 | $content .= '</ul>'; |
||
224 | return $content; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * runs a query |
||
229 | * |
||
230 | * @param string $sql sql statement to perform |
||
231 | * |
||
232 | * @return resource |
||
233 | */ |
||
234 | public function query($sql) |
||
239 | |||
240 | /** |
||
241 | * Setup prefix table |
||
242 | * |
||
243 | * @param string $table table to prefix |
||
244 | * |
||
245 | * @return string prefixed table |
||
246 | */ |
||
247 | public function prefix($table) |
||
252 | |||
253 | /** |
||
254 | * fetches an array |
||
255 | * |
||
256 | * @param string $ret resource that was returned from query |
||
257 | * |
||
258 | * @return array returns the array |
||
259 | */ |
||
260 | public function fetchArray($ret) |
||
265 | |||
266 | /** |
||
267 | * Inserts into a table |
||
268 | * |
||
269 | * @param string $table table to insert into |
||
270 | * @param string $query query to use to insert |
||
271 | * |
||
272 | * @return bool|void |
||
273 | */ |
||
274 | View Code Duplication | public function insert($table, $query) |
|
295 | |||
296 | /** |
||
297 | * return the error |
||
298 | * |
||
299 | * @return bool |
||
300 | */ |
||
301 | public function isError() |
||
305 | |||
306 | /** |
||
307 | * Deletes tables |
||
308 | * |
||
309 | * No callers found in core. foreach loop is suspect, using key and value for |
||
310 | * a list of tables? No docs or examples about what it is supposed to do. |
||
311 | * |
||
312 | * @param array $tables table to delete |
||
313 | * |
||
314 | * @return array list of dropped tables |
||
315 | */ |
||
316 | View Code Duplication | public function deleteTables($tables) |
|
328 | |||
329 | /** |
||
330 | * Checks to see if table exists |
||
331 | * |
||
332 | * @param string $table name of database table looking for |
||
333 | * |
||
334 | * @return bool true if exists or false if doesnt |
||
335 | */ |
||
336 | View Code Duplication | public function tableExists($table) |
|
347 | |||
348 | /** |
||
349 | * This method allows to copy fields from one table to another |
||
350 | * |
||
351 | * @param array $fieldsMap Map of the fields |
||
352 | * ex: array('oldfieldname' => 'newfieldname'); |
||
353 | * @param string $oTableName Old Table |
||
354 | * @param string $nTableName New Table |
||
355 | * @param bool $dropTable Drop old Table |
||
356 | * |
||
357 | * @return this does not return anything |
||
358 | */ |
||
359 | public function copyFields( |
||
385 | } |
||
386 |
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state