| Total Complexity | 88 |
| Total Lines | 415 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Upgrade_230 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 Upgrade_230, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class Upgrade_230 extends XoopsUpgrade |
||
| 31 | { |
||
| 32 | /* |
||
| 33 | * __construct() |
||
| 34 | */ |
||
| 35 | public function __construct() |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Check if cpanel config already exists |
||
| 44 | * |
||
| 45 | */ |
||
| 46 | public function check_config() |
||
| 47 | { |
||
| 48 | $sql = 'SELECT COUNT(*) FROM `' . $GLOBALS['xoopsDB']->prefix('config') . "` WHERE `conf_name` IN ('welcome_type', 'cpanel')"; |
||
| 49 | $result = $GLOBALS['xoopsDB']->queryF($sql); |
||
| 50 | if (!$GLOBALS['xoopsDB']->isResultSet($result)) { |
||
| 51 | return false; |
||
| 52 | } |
||
| 53 | list($count) = $GLOBALS['xoopsDB']->fetchRow($result); |
||
| 54 | |||
| 55 | return ($count == 2); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Check if cache_model table already exists |
||
| 60 | * |
||
| 61 | */ |
||
| 62 | public function check_cache() |
||
| 71 | |||
| 72 | /* |
||
| 73 | $sql = "SELECT COUNT(*) FROM `" . $GLOBALS['xoopsDB']->prefix('cache_model') . "`"; |
||
| 74 | if ( !$result = $GLOBALS['xoopsDB']->queryF( $sql ) ) { |
||
| 75 | return false; |
||
| 76 | } |
||
| 77 | |||
| 78 | return true; |
||
| 79 | */ |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Check if primary key for `block_module_link` is already set |
||
| 84 | * |
||
| 85 | */ |
||
| 86 | public function check_bmlink() |
||
| 87 | { |
||
| 88 | // MySQL 5.0+ |
||
| 89 | //$sql = "SHOW KEYS FROM `" . $GLOBALS['xoopsDB']->prefix('block_module_link') . "` WHERE `KEY_NAME` LIKE 'PRIMARY'"; |
||
| 90 | $sql = 'SHOW KEYS FROM `' . $GLOBALS['xoopsDB']->prefix('block_module_link') . '`'; |
||
| 91 | $result = $GLOBALS['xoopsDB']->queryF($sql); |
||
| 92 | if (!$GLOBALS['xoopsDB']->isResultSet($result)) { |
||
| 93 | return false; |
||
| 94 | } |
||
| 95 | while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) { |
||
| 96 | if ($row['Key_name'] === 'PRIMARY') { |
||
| 97 | return true; |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | return false; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return bool |
||
| 106 | */ |
||
| 107 | public function apply_bmlink() |
||
| 108 | { |
||
| 109 | $sql = 'SHOW KEYS FROM `' . $GLOBALS['xoopsDB']->prefix('block_module_link') . '`'; |
||
| 110 | $result = $GLOBALS['xoopsDB']->queryF($sql); |
||
| 111 | if (!$GLOBALS['xoopsDB']->isResultSet($result)) { |
||
| 112 | return false; |
||
| 113 | } |
||
| 114 | $keys_drop = array(); |
||
| 115 | $primary_add = true; |
||
| 116 | while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) { |
||
| 117 | if ($row['Key_name'] === 'PRIMARY') { |
||
| 118 | $primary_add = false; |
||
| 119 | } |
||
| 120 | if (in_array($row['Key_name'], array('block_id', 'module_id'))) { |
||
| 121 | $keys_drop[] = $row['Key_name']; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | foreach ($keys_drop as $drop) { |
||
| 125 | $sql = 'ALTER TABLE `' . $GLOBALS['xoopsDB']->prefix('block_module_link') . "` DROP KEY `{$drop}`"; |
||
| 126 | $GLOBALS['xoopsDB']->queryF($sql); |
||
| 127 | } |
||
| 128 | if ($primary_add) { |
||
| 129 | $sql = 'ALTER IGNORE TABLE `' . $GLOBALS['xoopsDB']->prefix('block_module_link') . '` ADD PRIMARY KEY (`block_id`, `module_id`)'; |
||
| 130 | |||
| 131 | return $GLOBALS['xoopsDB']->queryF($sql); |
||
| 132 | } |
||
| 133 | |||
| 134 | return true; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | public function apply_config() |
||
| 173 | } |
||
| 174 | |||
| 175 | public function apply_cache() |
||
| 176 | { |
||
| 177 | $allowWebChanges = $GLOBALS['xoopsDB']->allowWebChanges; |
||
| 178 | $GLOBALS['xoopsDB']->allowWebChanges = true; |
||
| 179 | $result = $GLOBALS['xoopsDB']->queryFromFile(__DIR__ . '/mysql.structure.sql'); |
||
| 180 | $GLOBALS['xoopsDB']->allowWebChanges = $allowWebChanges; |
||
| 181 | |||
| 182 | return $result; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @return bool |
||
| 187 | */ |
||
| 188 | public function check_path() |
||
| 189 | { |
||
| 190 | if (!(defined('XOOPS_PATH') && defined('XOOPS_VAR_PATH') && defined('XOOPS_TRUST_PATH'))) { |
||
| 191 | return false; |
||
| 192 | } |
||
| 193 | $ctrl = new PathStuffController(); |
||
|
|
|||
| 194 | if (!$ctrl->checkPath()) { |
||
| 195 | return false; |
||
| 196 | } |
||
| 197 | if (!$ctrl->checkPermissions()) { |
||
| 198 | return false; |
||
| 199 | } |
||
| 200 | |||
| 201 | return true; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return bool |
||
| 206 | */ |
||
| 207 | public function apply_path() |
||
| 208 | { |
||
| 209 | return $this->update_configs('path'); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return bool |
||
| 214 | */ |
||
| 215 | public function check_db() |
||
| 216 | { |
||
| 217 | // mainfile was included to get here, just check for definition |
||
| 218 | if (defined('XOOPS_DB_CHARSET')) { |
||
| 219 | return true; |
||
| 220 | } |
||
| 221 | /* |
||
| 222 | $lines = file(XOOPS_ROOT_PATH . '/mainfile.php'); |
||
| 223 | foreach ($lines as $line) { |
||
| 224 | if (preg_match("/(define\(\s*)([\"'])(XOOPS_DB_CHARSET)\\2,\s*([\"'])([^\"']*?)\\4\s*\);/", $line)) { |
||
| 225 | return true; |
||
| 226 | } |
||
| 227 | } |
||
| 228 | */ |
||
| 229 | return false; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @return bool |
||
| 234 | */ |
||
| 235 | public function apply_db() |
||
| 236 | { |
||
| 237 | return $this->update_configs('db'); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param $task |
||
| 242 | * |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | public function update_configs($task) |
||
| 246 | { |
||
| 247 | if (!$vars = $this->set_configs($task)) { |
||
| 248 | return false; |
||
| 249 | } |
||
| 250 | if ($task === 'db' && !empty($vars['XOOPS_DB_COLLATION'])) { |
||
| 251 | if ($pos = strpos($vars['XOOPS_DB_COLLATION'], '_')) { |
||
| 252 | $vars['XOOPS_DB_CHARSET'] = substr($vars['XOOPS_DB_COLLATION'], 0, $pos); |
||
| 253 | $this->convert_db($vars['XOOPS_DB_CHARSET'], $vars['XOOPS_DB_COLLATION']); |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | return $this->write_mainfile($vars); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param $charset |
||
| 262 | * @param $collation |
||
| 263 | * |
||
| 264 | * @return bool |
||
| 265 | */ |
||
| 266 | public function convert_db($charset, $collation) |
||
| 267 | { |
||
| 268 | $sql = 'ALTER DATABASE `' . XOOPS_DB_NAME . '` DEFAULT CHARACTER SET ' . $GLOBALS['xoopsDB']->quote($charset) . ' COLLATE ' . $GLOBALS['xoopsDB']->quote($collation); |
||
| 269 | $result = $GLOBALS['xoopsDB']->queryF($sql); |
||
| 270 | if (!$GLOBALS['xoopsDB']->isResultSet($result)) { |
||
| 271 | return false; |
||
| 272 | } |
||
| 273 | |||
| 274 | $sql = "SHOW TABLES LIKE '" . XOOPS_DB_PREFIX . "\_%'"; |
||
| 275 | $result = $GLOBALS['xoopsDB']->queryF($sql); |
||
| 276 | if (!$GLOBALS['xoopsDB']->isResultSet($result)) { |
||
| 277 | return false; |
||
| 278 | } |
||
| 279 | $tables = array(); |
||
| 280 | while (false !== (list($table) = $GLOBALS['xoopsDB']->fetchRow($result))) { |
||
| 281 | $tables[] = $table; |
||
| 282 | //$GLOBALS["xoopsDB"]->queryF( "ALTER TABLE `{$table}` DEFAULT CHARACTER SET " . $GLOBALS["xoopsDB"]->quote($charset) . " COLLATE " . $GLOBALS["xoopsDB"]->quote($collation) ); |
||
| 283 | //$GLOBALS["xoopsDB"]->queryF( "ALTER TABLE `{$table}` CONVERT TO CHARACTER SET " . $GLOBALS["xoopsDB"]->quote($charset) . " COLLATE " . $GLOBALS["xoopsDB"]->quote($collation) ); |
||
| 284 | } |
||
| 285 | $this->convert_table($tables, $charset, $collation); |
||
| 286 | return null; |
||
| 287 | } |
||
| 288 | |||
| 289 | // Some code not ready to use |
||
| 290 | /** |
||
| 291 | * @param $tables |
||
| 292 | * @param $charset |
||
| 293 | * @param $collation |
||
| 294 | * |
||
| 295 | * @return array |
||
| 296 | */ |
||
| 297 | public function convert_table($tables, $charset, $collation) |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @param $vars |
||
| 385 | * |
||
| 386 | * @return bool |
||
| 387 | */ |
||
| 388 | public function write_mainfile($vars) |
||
| 389 | { |
||
| 390 | if (empty($vars)) { |
||
| 391 | return false; |
||
| 392 | } |
||
| 393 | |||
| 394 | $file = __DIR__ . '/mainfile.dist.php'; |
||
| 395 | |||
| 396 | $lines = file($file); |
||
| 397 | foreach (array_keys($lines) as $ln) { |
||
| 398 | if (preg_match("/(define\()([\"'])(XOOPS_[^\"']+)\\2,\s*([0-9]+)\s*\)/", $lines[$ln], $matches)) { |
||
| 399 | $val = isset($vars[$matches[3]]) ? (string)constant($matches[3]) : (defined($matches[3]) ? (string)constant($matches[3]) : '0'); |
||
| 400 | $lines[$ln] = preg_replace("/(define\()([\"'])(XOOPS_[^\"']+)\\2,\s*([0-9]+)\s*\)/", "define('" . $matches[3] . "', " . $val . ' )', $lines[$ln]); |
||
| 401 | } elseif (preg_match("/(define\()([\"'])(XOOPS_[^\"']+)\\2,\s*([\"'])([^\"']*?)\\4\s*\)/", $lines[$ln], $matches)) { |
||
| 402 | $val = isset($vars[$matches[3]]) ? (string)$vars[$matches[3]] : (defined($matches[3]) ? (string)constant($matches[3]) : ''); |
||
| 403 | $lines[$ln] = preg_replace("/(define\()([\"'])(XOOPS_[^\"']+)\\2,\s*([\"'])(.*?)\\4\s*\)/", "define('" . $matches[3] . "', '" . $val . "' )", $lines[$ln]); |
||
| 404 | } |
||
| 405 | } |
||
| 406 | |||
| 407 | $fp = fopen(XOOPS_ROOT_PATH . '/mainfile.php', 'wt'); |
||
| 408 | if (!$fp) { |
||
| 409 | echo ERR_COULD_NOT_WRITE_MAINFILE; |
||
| 410 | echo "<pre style='border: 1px solid black; width: 80%; overflow: auto;'><div style='color: #ff0000; font-weight: bold;'><div>" . implode('</div><div>', array_map('htmlspecialchars', $lines)) . '</div></div></pre>'; |
||
| 411 | |||
| 412 | return false; |
||
| 413 | } else { |
||
| 414 | $newline = defined(PHP_EOL) ? PHP_EOL : (strpos(php_uname(), 'Windows') ? "\r\n" : "\n"); |
||
| 415 | $content = str_replace(array("\r\n", "\n"), $newline, implode('', $lines)); |
||
| 416 | |||
| 417 | fwrite($fp, $content); |
||
| 418 | fclose($fp); |
||
| 419 | |||
| 420 | return true; |
||
| 421 | } |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @param $task |
||
| 426 | * |
||
| 427 | * @return array|bool |
||
| 428 | */ |
||
| 429 | public function set_configs($task) |
||
| 445 | } |
||
| 446 | } |
||
| 447 | |||
| 448 | $upg = new Upgrade_230(); |
||
| 449 | return $upg; |
||
| 450 |
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.