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