Total Complexity | 72 |
Total Lines | 435 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like SqlFile 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.
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 SqlFile, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class SqlFile extends Files\CreateFile |
||
34 | { |
||
35 | /** |
||
36 | * @public function constructor |
||
37 | * |
||
38 | * @param null |
||
39 | */ |
||
40 | public function __construct() |
||
41 | { |
||
42 | parent::__construct(); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @static function getInstance |
||
47 | * |
||
48 | * @param null |
||
49 | * |
||
50 | * @return SqlFile |
||
51 | */ |
||
52 | public static function getInstance() |
||
53 | { |
||
54 | static $instance = false; |
||
55 | if (!$instance) { |
||
56 | $instance = new self(); |
||
57 | } |
||
58 | |||
59 | return $instance; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @public function write |
||
64 | * |
||
65 | * @param $module |
||
66 | * @param $filename |
||
67 | */ |
||
68 | public function write($module, $filename) |
||
69 | { |
||
70 | $this->setModule($module); |
||
71 | $this->setFileName($filename); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @private function getHeaderSqlComments |
||
76 | * |
||
77 | * @param $moduleName |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | private function getHeaderSqlComments($moduleName) |
||
82 | { |
||
83 | $date = date('D M d, Y'); |
||
84 | $time = date('H:i:s'); |
||
85 | $serverName = \Xmf\Request::getString('SERVER_NAME', '', 'SERVER'); |
||
86 | $serverVersion = $GLOBALS['xoopsDB']->getServerVersion(); |
||
87 | $phpVersion = PHP_VERSION; |
||
88 | // Header Sql Comments |
||
89 | $ret = null; |
||
90 | $arrayServerInfo = [ |
||
91 | "# SQL Dump for {$moduleName} module", |
||
92 | '# PhpMyAdmin Version: 4.0.4', |
||
93 | '# http://www.phpmyadmin.net', |
||
94 | '#', |
||
95 | "# Host: {$serverName}", |
||
96 | "# Generated on: {$date} to {$time}", |
||
97 | "# Server version: {$serverVersion}", |
||
98 | "# PHP Version: {$phpVersion}\n", |
||
99 | ]; |
||
100 | foreach ($arrayServerInfo as $serverInfo) { |
||
101 | $ret .= $this->getSimpleString($serverInfo); |
||
102 | } |
||
103 | |||
104 | return $ret; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @private function getHeadDatabaseTable |
||
109 | * @param $moduleDirname |
||
110 | * @param $tableName |
||
111 | * @param int $fieldsNumb |
||
112 | * |
||
113 | * Unused IF NOT EXISTS |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | private function getHeadDatabaseTable($moduleDirname, $tableName, $fieldsNumb) |
||
118 | { |
||
119 | $ret = null; |
||
120 | $arrayDbTable = [ |
||
121 | '#', |
||
122 | "# Structure table for `{$moduleDirname}_{$tableName}` {$fieldsNumb}", |
||
123 | '#', |
||
124 | "\nCREATE TABLE `{$moduleDirname}_{$tableName}` (", |
||
125 | ]; |
||
126 | foreach ($arrayDbTable as $dbTable) { |
||
127 | $ret .= $this->getSimpleString($dbTable); |
||
128 | } |
||
129 | |||
130 | return $ret; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @private function getDatabaseTables |
||
135 | * |
||
136 | * @param $module |
||
137 | * |
||
138 | * @return null|string |
||
139 | */ |
||
140 | private function getDatabaseTables($module) |
||
141 | { |
||
142 | $ret = null; |
||
143 | $moduleDirname = \mb_strtolower($module->getVar('mod_dirname')); |
||
144 | $tables = $this->getTableTables($module->getVar('mod_id'), 'table_order ASC, table_id'); |
||
145 | $tableMid = 0; |
||
|
|||
146 | $tableId = 0; |
||
147 | $tableName = 0; |
||
148 | $tableAutoincrement = 0; |
||
149 | $fieldsNumb = 0; |
||
150 | $tableRate = 0; |
||
151 | foreach (\array_keys($tables) as $t) { |
||
152 | $tableId = $tables[$t]->getVar('table_id'); |
||
153 | $tableMid = $tables[$t]->getVar('table_mid'); |
||
154 | $tableName = $tables[$t]->getVar('table_name'); |
||
155 | $tableAutoincrement = $tables[$t]->getVar('table_autoincrement'); |
||
156 | $fieldsNumb = $tables[$t]->getVar('table_nbfields'); |
||
157 | if (1 === (int)$tables[$t]->getVar('table_rate')) { |
||
158 | $tableRate = 1; |
||
159 | } |
||
160 | $ret .= $this->getDatabaseFields($moduleDirname, $tableMid, $tableId, $tableName, $tableAutoincrement, $fieldsNumb); |
||
161 | } |
||
162 | |||
163 | if (1 === $tableRate) { |
||
164 | $ret .= $this->getTableRatings($moduleDirname); |
||
165 | } |
||
166 | |||
167 | return $ret; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @private function getDatabaseFields |
||
172 | * |
||
173 | * @param $moduleDirname |
||
174 | * @param $tableMid |
||
175 | * @param $tableId |
||
176 | * @param $tableName |
||
177 | * @param $tableAutoincrement |
||
178 | * @param $fieldsNumb |
||
179 | * @return null|string |
||
180 | */ |
||
181 | private function getDatabaseFields($moduleDirname, $tableMid, $tableId, $tableName, $tableAutoincrement, $fieldsNumb) |
||
182 | { |
||
183 | $helper = Modulebuilder\Helper::getInstance(); |
||
184 | $ret = null; |
||
185 | $j = 0; |
||
186 | $comma = []; |
||
187 | $row = []; |
||
188 | //$type = ''; |
||
189 | $fieldTypeName = ''; |
||
190 | $fields = $this->getTableFields($tableMid, $tableId, 'field_order ASC, field_id'); |
||
191 | foreach (\array_keys($fields) as $f) { |
||
192 | // Creation of database table |
||
193 | $ret = $this->getHeadDatabaseTable($moduleDirname, $tableName, $fieldsNumb); |
||
194 | $fieldName = $fields[$f]->getVar('field_name'); |
||
195 | $fieldType = $fields[$f]->getVar('field_type'); |
||
196 | $fieldValue = \str_replace(''', '', $fields[$f]->getVar('field_value')); //remove single quotes |
||
197 | $fieldAttribute = $fields[$f]->getVar('field_attribute'); |
||
198 | $fieldNull = $fields[$f]->getVar('field_null'); |
||
199 | $fieldDefault = \str_replace(''', '', $fields[$f]->getVar('field_default')); //remove single quotes |
||
200 | $fieldKey = $fields[$f]->getVar('field_key'); |
||
201 | if ($fieldType > 1) { |
||
202 | $fType = $helper->getHandler('Fieldtype')->get($fieldType); |
||
203 | $fieldTypeName = $fType->getVar('fieldtype_name'); |
||
204 | } else { |
||
205 | $fieldType = null; |
||
206 | } |
||
207 | if ($fieldAttribute > 1) { |
||
208 | $fAttribute = $helper->getHandler('Fieldattributes')->get($fieldAttribute); |
||
209 | $fieldAttribute = $fAttribute->getVar('fieldattribute_name'); |
||
210 | } else { |
||
211 | $fieldAttribute = null; |
||
212 | } |
||
213 | if ($fieldNull > 1) { |
||
214 | $fNull = $helper->getHandler('Fieldnull')->get($fieldNull); |
||
215 | $fieldNull = $fNull->getVar('fieldnull_name'); |
||
216 | } else { |
||
217 | $fieldNull = null; |
||
218 | } |
||
219 | if (!empty($fieldName)) { |
||
220 | switch ($fieldType) { |
||
221 | case 2: |
||
222 | case 3: |
||
223 | case 4: |
||
224 | case 5: |
||
225 | $type = $fieldTypeName . '(' . $fieldValue . ')'; |
||
226 | if (empty($fieldDefault)) { |
||
227 | $default = "DEFAULT '0'"; |
||
228 | } else { |
||
229 | $default = "DEFAULT '{$fieldDefault}'"; |
||
230 | } |
||
231 | break; |
||
232 | case 6: |
||
233 | case 7: |
||
234 | case 8: |
||
235 | $type = $fieldTypeName . '(' . $fieldValue . ')'; |
||
236 | if (empty($fieldDefault)) { |
||
237 | $default = "DEFAULT '0'"; // From MySQL 5.7 Manual |
||
238 | } else { |
||
239 | $default = "DEFAULT '{$fieldDefault}'"; |
||
240 | } |
||
241 | break; |
||
242 | case 9: |
||
243 | case 10: |
||
244 | $fValues = \str_replace(',', "', '", \str_replace(' ', '', $fieldValue)); |
||
245 | $type = $fieldTypeName . '(\'' . $fValues . '\')'; // Used with comma separator |
||
246 | $default = "DEFAULT '{$fieldDefault}'"; |
||
247 | break; |
||
248 | case 11: |
||
249 | $type = $fieldTypeName . '(' . $fieldValue . ')'; |
||
250 | if (empty($fieldDefault)) { |
||
251 | $default = "DEFAULT '[email protected]'"; |
||
252 | } else { |
||
253 | $default = "DEFAULT '{$fieldDefault}'"; |
||
254 | } |
||
255 | break; |
||
256 | case 12: |
||
257 | $type = $fieldTypeName . '(' . $fieldValue . ')'; |
||
258 | if (empty($fieldDefault)) { |
||
259 | $default = "DEFAULT 'https:\\'"; |
||
260 | } else { |
||
261 | $default = "DEFAULT '{$fieldDefault}'"; |
||
262 | } |
||
263 | break; |
||
264 | case 13: |
||
265 | case 14: |
||
266 | $type = $fieldTypeName . '(' . $fieldValue . ')'; |
||
267 | $default = "DEFAULT '{$fieldDefault}'"; |
||
268 | break; |
||
269 | case 15: |
||
270 | case 16: |
||
271 | case 17: |
||
272 | case 18: |
||
273 | $type = $fieldTypeName; |
||
274 | $default = null; |
||
275 | break; |
||
276 | case 19: |
||
277 | case 20: |
||
278 | case 21: |
||
279 | case 22: |
||
280 | $type = $fieldTypeName . '(' . $fieldValue . ')'; |
||
281 | $default = "DEFAULT '{$fieldDefault}'"; |
||
282 | break; |
||
283 | case 23: |
||
284 | $type = $fieldTypeName; |
||
285 | if (empty($fieldDefault)) { |
||
286 | $default = "DEFAULT '1970'"; // From MySQL 5.7 Manual |
||
287 | } else { |
||
288 | $default = "DEFAULT '{$fieldDefault}'"; |
||
289 | } |
||
290 | break; |
||
291 | default: |
||
292 | $type = $fieldTypeName . '(' . $fieldValue . ')'; |
||
293 | $default = "DEFAULT '{$fieldDefault}'"; |
||
294 | break; |
||
295 | } |
||
296 | if ((0 == $f) && (1 == $tableAutoincrement)) { |
||
297 | $row[] = $this->getFieldRow($fieldName, $type, $fieldAttribute, $fieldNull, null, 'AUTO_INCREMENT'); |
||
298 | $comma[$j] = $this->getKey(2, $fieldName); |
||
299 | ++$j; |
||
300 | } elseif ((0 == $f) && (0 == $tableAutoincrement)) { |
||
301 | $row[] = $this->getFieldRow($fieldName, $type, $fieldAttribute, $fieldNull, $default); |
||
302 | $comma[$j] = $this->getKey(2, $fieldName); |
||
303 | ++$j; |
||
304 | } else { |
||
305 | if (3 == $fieldKey || 4 == $fieldKey || 5 == $fieldKey || 6 == $fieldKey) { |
||
306 | switch ($fieldKey) { |
||
307 | case 3: |
||
308 | $row[] = $this->getFieldRow($fieldName, $type, $fieldAttribute, $fieldNull, $default); |
||
309 | $comma[$j] = $this->getKey(3, $fieldName); |
||
310 | ++$j; |
||
311 | break; |
||
312 | case 4: |
||
313 | $row[] = $this->getFieldRow($fieldName, $type, $fieldAttribute, $fieldNull, $default); |
||
314 | $comma[$j] = $this->getKey(4, $fieldName); |
||
315 | ++$j; |
||
316 | break; |
||
317 | case 5: |
||
318 | $row[] = $this->getFieldRow($fieldName, $type, $fieldAttribute, $fieldNull, $default); |
||
319 | $comma[$j] = $this->getKey(5, $fieldName); |
||
320 | ++$j; |
||
321 | break; |
||
322 | case 6: |
||
323 | $row[] = $this->getFieldRow($fieldName, $type, $fieldAttribute, $fieldNull, $default); |
||
324 | $comma[$j] = $this->getKey(6, $fieldName); |
||
325 | ++$j; |
||
326 | break; |
||
327 | } |
||
328 | } else { |
||
329 | $row[] = $this->getFieldRow($fieldName, $type, $fieldAttribute, $fieldNull, $default); |
||
330 | } |
||
331 | } |
||
332 | } |
||
333 | } |
||
334 | // ================= COMMA ================= // |
||
335 | for ($i = 0; $i < $j; ++$i) { |
||
336 | if ($i != $j - 1) { |
||
337 | $row[] = $comma[$i] . ','; |
||
338 | } else { |
||
339 | $row[] = $comma[$i]; |
||
340 | } |
||
341 | } |
||
342 | // ================= COMMA CICLE ================= // |
||
343 | $ret .= \implode("\n", $row); |
||
344 | unset($j); |
||
345 | $ret .= $this->getFootDatabaseTable(); |
||
346 | |||
347 | return $ret; |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * @private function getDatabaseFields |
||
352 | * |
||
353 | * @param $moduleDirname |
||
354 | * @return null|string |
||
355 | */ |
||
356 | private function getTableRatings($moduleDirname) |
||
357 | { |
||
358 | $row = []; |
||
359 | $ret = $this->getHeadDatabaseTable($moduleDirname, 'ratings', 6); |
||
360 | $row[] = $this->getFieldRow('rate_id', 'INT(8)', 'UNSIGNED', 'NOT NULL', null, 'AUTO_INCREMENT'); |
||
361 | $row[] = $this->getFieldRow('rate_itemid', 'INT(8)', null, 'NOT NULL', "DEFAULT '0'"); |
||
362 | $row[] = $this->getFieldRow('rate_source', 'INT(8)', null, 'NOT NULL', "DEFAULT '0'"); |
||
363 | $row[] = $this->getFieldRow('rate_value', 'INT(1)', null, 'NOT NULL', "DEFAULT '0'"); |
||
364 | $row[] = $this->getFieldRow('rate_uid', 'INT(8)', null, 'NOT NULL', "DEFAULT '0'"); |
||
365 | $row[] = $this->getFieldRow('rate_ip', 'VARCHAR(60)', null, 'NOT NULL', "DEFAULT ''"); |
||
366 | $row[] = $this->getFieldRow('rate_date', 'INT(8)', null, 'NOT NULL', "DEFAULT '0'"); |
||
367 | $row[] = $this->getKey(2, 'rate_id'); |
||
368 | |||
369 | $ret .= \implode("\n", $row); |
||
370 | $ret .= $this->getFootDatabaseTable(); |
||
371 | |||
372 | return $ret; |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * @private function getFootDatabaseTable |
||
377 | * |
||
378 | * @param null |
||
379 | * |
||
380 | * @return string |
||
381 | */ |
||
382 | private function getFootDatabaseTable() |
||
383 | { |
||
384 | return "\n) ENGINE=InnoDB;\n\n"; |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * @private function getFieldRow |
||
389 | * |
||
390 | * @param $fieldName |
||
391 | * @param $fieldTypeValue |
||
392 | * @param $fieldAttribute |
||
393 | * @param $fieldNull |
||
394 | * @param $fieldDefault |
||
395 | * @param $autoincrement |
||
396 | * |
||
397 | * @return string |
||
398 | */ |
||
399 | private function getFieldRow($fieldName, $fieldTypeValue, $fieldAttribute = null, $fieldNull = null, $fieldDefault = null, $autoincrement = null) |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * @private function getKey |
||
420 | * |
||
421 | * @param $key |
||
422 | * @param $fieldName |
||
423 | * @return string |
||
424 | */ |
||
425 | private function getKey($key, $fieldName) |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * @public function render |
||
451 | * |
||
452 | * @param null |
||
453 | * |
||
454 | * @return bool|string |
||
455 | */ |
||
456 | public function render() |
||
468 | } |
||
469 | } |
||
470 |