| Conditions | 21 |
| Paths | 531 |
| Total Lines | 156 |
| Code Lines | 108 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 97 | public static function importTables($moduleId, $moduleName) |
||
| 98 | { |
||
| 99 | $helper = Helper::getInstance(); |
||
| 100 | $tablesHandler = $helper->getHandler('Tables'); |
||
| 101 | $fieldsHandler = $helper->getHandler('Fields'); |
||
| 102 | |||
| 103 | /** @var \XoopsModuleHandler $moduleHandler */ |
||
| 104 | $moduleHandler = xoops_getHandler('module'); |
||
| 105 | $module = $moduleHandler->getByDirname($moduleName); |
||
| 106 | $moduleTables = $module->getInfo('tables'); |
||
| 107 | |||
| 108 | $tables = []; |
||
| 109 | |||
| 110 | if (false !== $moduleTables && is_array($moduleTables)) { |
||
| 111 | $currentTableNumber = 0; |
||
| 112 | foreach ($moduleTables as $table) { |
||
| 113 | //create a new tablesholder |
||
| 114 | $newTable = $tablesHandler->create(); |
||
| 115 | $newTable->setVar('table_mid', $moduleId); |
||
| 116 | |||
| 117 | $newTable->setVar('table_name', $table); |
||
| 118 | $newTable->setVar('table_image', 'alert.png'); |
||
| 119 | |||
| 120 | //get all the fields for this table |
||
| 121 | $importedFields = self::importFields($table); |
||
| 122 | |||
| 123 | //set the number of fields for this table |
||
| 124 | $countFields = count($importedFields); |
||
| 125 | $newTable->setVar('table_nbfields', $countFields); |
||
| 126 | $newTable->setVar('table_order', $currentTableNumber); |
||
| 127 | $tablesHandler->insert($newTable); |
||
| 128 | |||
| 129 | $currentFieldNumber = 0; |
||
| 130 | foreach ($importedFields as $t) { |
||
| 131 | $fieldsObj = $fieldsHandler->create(); |
||
| 132 | $fieldsObj->setVar('field_mid', $moduleId); |
||
| 133 | $fieldsObj->setVar('field_tid', $newTable->getVar('table_id')); |
||
| 134 | $fieldsObj->setVar('field_order', $currentFieldNumber); |
||
| 135 | $fieldsObj->setVar('field_name', $t['Field']); |
||
| 136 | |||
| 137 | |||
| 138 | $type = '1'; |
||
| 139 | if (isset($t['Type'])) { |
||
| 140 | $types = [ |
||
| 141 | 2 => 'INT', |
||
| 142 | 3 => 'TINYINT', |
||
| 143 | 4 => 'MEDIUMINT', |
||
| 144 | 5 => 'SMALLINT', |
||
| 145 | 6 => 'FLOAT', |
||
| 146 | 7 => 'DOUBLE', |
||
| 147 | 8 => 'DECIMAL', |
||
| 148 | 9 => 'SET', |
||
| 149 | 10 => 'ENUM', |
||
| 150 | 11 => 'EMAIL', |
||
| 151 | 12 => 'URL', |
||
| 152 | 13 => 'CHAR', |
||
| 153 | 14 => 'VARCHAR', |
||
| 154 | 15 => 'TEXT', |
||
| 155 | 16 => 'TINYTEXT', |
||
| 156 | 17 => 'MEDIUMTEXT', |
||
| 157 | 18 => 'LONGTEXT', |
||
| 158 | 19 => 'DATE', |
||
| 159 | 20 => 'DATETIME', |
||
| 160 | 21 => 'TIMESTAMP', |
||
| 161 | 22 => 'TIME', |
||
| 162 | 23 => 'YEAR', |
||
| 163 | ]; |
||
| 164 | $type = array_search(strtolower($t['Type']), array_map('strtolower', $types)); |
||
| 165 | } |
||
| 166 | $fieldsObj->setVar('field_type', $type); |
||
| 167 | $fieldsObj->setVar('field_value', $t['Len']); |
||
| 168 | |||
| 169 | $attr = '1'; |
||
| 170 | if (isset($t['Signed'])) { |
||
| 171 | $attribs = [ |
||
| 172 | 2 => 'BINARY', |
||
| 173 | 3 => 'UNSIGNED', |
||
| 174 | 4 => 'UNSIGNED_ZEROFILL', |
||
| 175 | 5 => 'SMALLINT', |
||
| 176 | 6 => 'CURRENT_TIMESTAMP', |
||
| 177 | ]; |
||
| 178 | $attr = array_search(strtolower($t['Signed']), array_map('strtolower', $attribs)); |
||
| 179 | } |
||
| 180 | $fieldsObj->setVar('field_attribute', $attr); |
||
| 181 | |||
| 182 | // $fieldsObj->setVar('field_null', $t['Null'] ?? ''); |
||
| 183 | $null = '1'; |
||
| 184 | if ('NOT NULL' === $t['Null']) { |
||
| 185 | $null = '2'; |
||
| 186 | } elseif ('NULL' === $t['Null']) { |
||
| 187 | $null = '3'; |
||
| 188 | } |
||
| 189 | $fieldsObj->setVar('field_null', $null); |
||
| 190 | $fieldsObj->setVar('field_default', $t['Default']); |
||
| 191 | |||
| 192 | $key = 1; |
||
| 193 | if (isset($t['Key'])) { |
||
| 194 | $keys = [ |
||
| 195 | 2 => 'PRI', |
||
| 196 | 3 => 'UNI', |
||
| 197 | 4 => 'KEY', |
||
| 198 | 5 => 'IND', |
||
| 199 | 6 => 'FUL', |
||
| 200 | ]; |
||
| 201 | $key = array_search(strtolower($t['Key']), array_map('strtolower', $keys)); |
||
| 202 | } |
||
| 203 | $fieldsObj->setVar('field_key', $key); |
||
| 204 | $fieldsObj->setVar('field_element', $t['Field']); |
||
| 205 | |||
| 206 | if ($currentFieldNumber < $countFields - 1) { |
||
| 207 | // |
||
| 208 | } |
||
| 209 | |||
| 210 | if (0 == $currentFieldNumber) { |
||
| 211 | if (in_array($t['Type'], ['blob', 'text', 'mediumblob', 'mediumtext', 'longblob', 'longtext', 'enum', 'set',])) { |
||
| 212 | // XoopsFormTextArea |
||
| 213 | $fieldsObj->setVar('field_element', '3'); |
||
| 214 | } elseif (in_array($t['Type'], ['int', 'integer', 'tinyint', 'smallint', 'mediumint', 'bigint', 'float', 'double', 'real', 'char', 'varchar',])) { |
||
| 215 | //XoopsFormText |
||
| 216 | $fieldsObj->setVar('field_element', '2'); |
||
| 217 | } elseif ('datetime' === $t['Type']) { |
||
| 218 | //XoopsFormDateTime //XoopsFormDatePicker |
||
| 219 | $fieldsObj->setVar('field_element', '21'); |
||
| 220 | } elseif ('date' === $t['Type']) { |
||
| 221 | //XoopsFormTextDateSelect |
||
| 222 | $fieldsObj->setVar('field_element', '15'); |
||
| 223 | } |
||
| 224 | } elseif ($currentFieldNumber > 0) { |
||
| 225 | if (in_array($t['Type'], ['blob', 'text', 'mediumblob', 'mediumtext', 'longblob', 'longtext', 'enum', 'set',])) { |
||
| 226 | //XoopsFormTextArea |
||
| 227 | $fieldsObj->setVar('field_element', '3'); |
||
| 228 | } elseif (in_array($t['Type'], ['int', 'integer', 'tinyint', 'smallint', 'mediumint', 'bigint', 'float', 'double', 'real', 'char', 'varchar',])) { |
||
| 229 | //XoopsFormText |
||
| 230 | $fieldsObj->setVar('field_element', '2'); |
||
| 231 | } elseif ('datetime' === $t['Type']) { |
||
| 232 | //XoopsFormDateTime //XoopsFormDatePicker |
||
| 233 | $fieldsObj->setVar('field_element', '21'); |
||
| 234 | } elseif ('date' === $t['Type']) { |
||
| 235 | //XoopsFormTextDateSelect |
||
| 236 | $fieldsObj->setVar('field_element', '15'); |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | ++$currentFieldNumber; |
||
| 241 | |||
| 242 | $fieldsHandler->insert($fieldsObj); |
||
| 243 | } |
||
| 244 | |||
| 245 | $tables[] = \_AM_MODULEBUILDER_SUCCESS_IMPTABLES . $table; |
||
| 246 | |||
| 247 | ++$currentTableNumber; |
||
| 248 | } |
||
| 249 | } else { |
||
| 250 | return false; |
||
| 251 | } |
||
| 252 | return $tables; |
||
| 253 | } |
||
| 308 |