| Total Complexity | 85 |
| Total Lines | 425 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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 |
||
| 32 | class Upgrade_230 extends XoopsUpgrade |
||
| 33 | { |
||
| 34 | /* |
||
| 35 | * __construct() |
||
| 36 | */ |
||
| 37 | public function __construct() |
||
| 38 | { |
||
| 39 | parent::__construct(basename(__DIR__)); |
||
| 40 | $this->usedFiles = array('mainfile.php'); |
||
| 41 | $this->tasks = array('config', 'cache', 'path', 'db', 'bmlink'); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Check if cpanel config already exists |
||
| 46 | * |
||
| 47 | */ |
||
| 48 | public function check_config() |
||
| 49 | { |
||
| 50 | $sql = 'SELECT COUNT(*) FROM `' . $GLOBALS['xoopsDB']->prefix('config') . "` WHERE `conf_name` IN ('welcome_type', 'cpanel')"; |
||
| 51 | $result = $GLOBALS['xoopsDB']->queryF($sql); |
||
| 52 | if (!$GLOBALS['xoopsDB']->isResultSet($result)) { |
||
| 53 | return false; |
||
| 54 | } |
||
| 55 | list($count) = $GLOBALS['xoopsDB']->fetchRow($result); |
||
| 56 | |||
| 57 | return ($count == 2); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Check if cache_model table already exists |
||
| 62 | * |
||
| 63 | */ |
||
| 64 | public function check_cache() |
||
| 65 | { |
||
| 66 | $sql = "SHOW TABLES LIKE '" . $GLOBALS['xoopsDB']->prefix('cache_model') . "'"; |
||
| 67 | $result = $GLOBALS['xoopsDB']->queryF($sql); |
||
| 68 | if (!$GLOBALS['xoopsDB']->isResultSet($result)) { |
||
| 69 | return false; |
||
| 70 | } |
||
| 71 | |||
| 72 | $temp = $GLOBALS['xoopsDB']->getRowsNum($result) > 0; |
||
| 73 | return $temp; |
||
| 74 | |||
| 75 | /* |
||
| 76 | $sql = "SELECT COUNT(*) FROM `" . $GLOBALS['xoopsDB']->prefix('cache_model') . "`"; |
||
| 77 | if ( !$result = $GLOBALS['xoopsDB']->queryF( $sql ) ) { |
||
| 78 | return false; |
||
| 79 | } |
||
| 80 | |||
| 81 | return true; |
||
| 82 | */ |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Check if primary key for `block_module_link` is already set |
||
| 87 | * |
||
| 88 | */ |
||
| 89 | public function check_bmlink() |
||
| 90 | { |
||
| 91 | // MySQL 5.0+ |
||
| 92 | //$sql = "SHOW KEYS FROM `" . $GLOBALS['xoopsDB']->prefix('block_module_link') . "` WHERE `KEY_NAME` LIKE 'PRIMARY'"; |
||
| 93 | $sql = 'SHOW KEYS FROM `' . $GLOBALS['xoopsDB']->prefix('block_module_link') . '`'; |
||
| 94 | $result = $GLOBALS['xoopsDB']->queryF($sql); |
||
| 95 | if (!$GLOBALS['xoopsDB']->isResultSet($result)) { |
||
| 96 | return false; |
||
| 97 | } |
||
| 98 | while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) { |
||
| 99 | if ($row['Key_name'] === 'PRIMARY') { |
||
| 100 | return true; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | return false; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return bool |
||
| 109 | */ |
||
| 110 | public function apply_bmlink() |
||
| 111 | { |
||
| 112 | $tableName = 'block_module_link'; |
||
| 113 | $tableNameOld = $tableName . '_old'; |
||
| 114 | |||
| 115 | $tables = new Tables(); |
||
| 116 | |||
| 117 | $tables->useTable($tableName); |
||
| 118 | $tables->renameTable($tableName, $tableNameOld); |
||
| 119 | $result = $tables->executeQueue(true); |
||
| 120 | if (true!==$result) { |
||
| 121 | throw new \RuntimeException( |
||
| 122 | __METHOD__ . ' failed.', E_USER_ERROR |
||
| 123 | ); |
||
| 124 | } |
||
| 125 | $tables->resetQueue(); |
||
| 126 | $tables->addTable($tableName); |
||
| 127 | $tables->addColumn($tableName, 'block_id', 'int'); |
||
| 128 | $tables->addColumn($tableName, 'module_id', 'int'); |
||
| 129 | $tables->addPrimaryKey($tableName, 'block_id, module_id'); |
||
| 130 | $result = $tables->executeQueue(true); |
||
| 131 | if (true!==$result) { |
||
| 132 | throw new \RuntimeException( |
||
| 133 | __METHOD__ . ' failed.', E_USER_ERROR |
||
| 134 | ); |
||
| 135 | } |
||
| 136 | $prefixedName = $GLOBALS['xoopsDB']->prefix('block_module_link'); |
||
| 137 | $sql = 'INSERT INTO `' . $prefixedName . '` (`block_id`, `module_id`) ' . |
||
| 138 | 'SELECT DISTINCT `block_id`, `module_id` FROM `' . $prefixedName . '_old`'; |
||
| 139 | $result = $GLOBALS['xoopsDB']->queryF($sql); |
||
| 140 | if (true!==$result) { |
||
| 141 | throw new \RuntimeException( |
||
| 142 | __METHOD__ . ' failed.', E_USER_ERROR |
||
| 143 | ); |
||
| 144 | } |
||
| 145 | |||
| 146 | return true; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @return bool |
||
| 151 | */ |
||
| 152 | public function apply_config() |
||
| 185 | } |
||
| 186 | |||
| 187 | public function apply_cache() |
||
| 188 | { |
||
| 189 | $allowWebChanges = $GLOBALS['xoopsDB']->allowWebChanges; |
||
| 190 | $GLOBALS['xoopsDB']->allowWebChanges = true; |
||
| 191 | $result = $GLOBALS['xoopsDB']->queryFromFile(__DIR__ . '/mysql.structure.sql'); |
||
| 192 | $GLOBALS['xoopsDB']->allowWebChanges = $allowWebChanges; |
||
| 193 | |||
| 194 | return $result; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @return bool |
||
| 199 | */ |
||
| 200 | public function check_path() |
||
| 201 | { |
||
| 202 | if (!(defined('XOOPS_PATH') && defined('XOOPS_VAR_PATH') && defined('XOOPS_TRUST_PATH'))) { |
||
| 203 | return false; |
||
| 204 | } |
||
| 205 | $ctrl = new PathStuffController(); |
||
|
|
|||
| 206 | if (!$ctrl->checkPath()) { |
||
| 207 | return false; |
||
| 208 | } |
||
| 209 | if (!$ctrl->checkPermissions()) { |
||
| 210 | return false; |
||
| 211 | } |
||
| 212 | |||
| 213 | return true; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @return bool |
||
| 218 | */ |
||
| 219 | public function apply_path() |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return bool |
||
| 226 | */ |
||
| 227 | public function check_db() |
||
| 228 | { |
||
| 229 | // mainfile was included to get here, just check for definition |
||
| 230 | if (defined('XOOPS_DB_CHARSET')) { |
||
| 231 | return true; |
||
| 232 | } |
||
| 233 | /* |
||
| 234 | $lines = file(XOOPS_ROOT_PATH . '/mainfile.php'); |
||
| 235 | foreach ($lines as $line) { |
||
| 236 | if (preg_match("/(define\(\s*)([\"'])(XOOPS_DB_CHARSET)\\2,\s*([\"'])([^\"']*?)\\4\s*\);/", $line)) { |
||
| 237 | return true; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | */ |
||
| 241 | return false; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @return bool |
||
| 246 | */ |
||
| 247 | public function apply_db() |
||
| 248 | { |
||
| 249 | return $this->update_configs('db'); |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param $task |
||
| 254 | * |
||
| 255 | * @return bool |
||
| 256 | */ |
||
| 257 | public function update_configs($task) |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param $charset |
||
| 274 | * @param $collation |
||
| 275 | * |
||
| 276 | * @return bool |
||
| 277 | */ |
||
| 278 | public function convert_db($charset, $collation) |
||
| 279 | { |
||
| 280 | $sql = 'ALTER DATABASE `' . XOOPS_DB_NAME . '` DEFAULT CHARACTER SET ' . $GLOBALS['xoopsDB']->quote($charset) . ' COLLATE ' . $GLOBALS['xoopsDB']->quote($collation); |
||
| 281 | $result = $GLOBALS['xoopsDB']->queryF($sql); |
||
| 282 | if (!$GLOBALS['xoopsDB']->isResultSet($result)) { |
||
| 283 | return false; |
||
| 284 | } |
||
| 285 | |||
| 286 | $sql = "SHOW TABLES LIKE '" . XOOPS_DB_PREFIX . "\_%'"; |
||
| 287 | $result = $GLOBALS['xoopsDB']->queryF($sql); |
||
| 288 | if (!$GLOBALS['xoopsDB']->isResultSet($result)) { |
||
| 289 | return false; |
||
| 290 | } |
||
| 291 | $tables = array(); |
||
| 292 | while (false !== (list($table) = $GLOBALS['xoopsDB']->fetchRow($result))) { |
||
| 293 | $tables[] = $table; |
||
| 294 | //$GLOBALS["xoopsDB"]->queryF( "ALTER TABLE `{$table}` DEFAULT CHARACTER SET " . $GLOBALS["xoopsDB"]->quote($charset) . " COLLATE " . $GLOBALS["xoopsDB"]->quote($collation) ); |
||
| 295 | //$GLOBALS["xoopsDB"]->queryF( "ALTER TABLE `{$table}` CONVERT TO CHARACTER SET " . $GLOBALS["xoopsDB"]->quote($charset) . " COLLATE " . $GLOBALS["xoopsDB"]->quote($collation) ); |
||
| 296 | } |
||
| 297 | $this->convert_table($tables, $charset, $collation); |
||
| 298 | return null; |
||
| 299 | } |
||
| 300 | |||
| 301 | // Some code not ready to use |
||
| 302 | /** |
||
| 303 | * @param $tables |
||
| 304 | * @param $charset |
||
| 305 | * @param $collation |
||
| 306 | * |
||
| 307 | * @return array |
||
| 308 | */ |
||
| 309 | public function convert_table($tables, $charset, $collation) |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @param $vars |
||
| 397 | * |
||
| 398 | * @return bool |
||
| 399 | */ |
||
| 400 | public function write_mainfile($vars) |
||
| 401 | { |
||
| 402 | if (empty($vars)) { |
||
| 403 | return false; |
||
| 404 | } |
||
| 405 | |||
| 406 | $file = __DIR__ . '/mainfile.dist.php'; |
||
| 407 | |||
| 408 | $lines = file($file); |
||
| 409 | foreach (array_keys($lines) as $ln) { |
||
| 410 | if (preg_match("/(define\()([\"'])(XOOPS_[^\"']+)\\2,\s*([0-9]+)\s*\)/", $lines[$ln], $matches)) { |
||
| 411 | $val = isset($vars[$matches[3]]) ? (string)constant($matches[3]) : (defined($matches[3]) ? (string)constant($matches[3]) : '0'); |
||
| 412 | $lines[$ln] = preg_replace("/(define\()([\"'])(XOOPS_[^\"']+)\\2,\s*([0-9]+)\s*\)/", "define('" . $matches[3] . "', " . $val . ' )', $lines[$ln]); |
||
| 413 | } elseif (preg_match("/(define\()([\"'])(XOOPS_[^\"']+)\\2,\s*([\"'])([^\"']*?)\\4\s*\)/", $lines[$ln], $matches)) { |
||
| 414 | $val = isset($vars[$matches[3]]) ? (string)$vars[$matches[3]] : (defined($matches[3]) ? (string)constant($matches[3]) : ''); |
||
| 415 | $lines[$ln] = preg_replace("/(define\()([\"'])(XOOPS_[^\"']+)\\2,\s*([\"'])(.*?)\\4\s*\)/", "define('" . $matches[3] . "', '" . $val . "' )", $lines[$ln]); |
||
| 416 | } |
||
| 417 | } |
||
| 418 | |||
| 419 | $fp = fopen(XOOPS_ROOT_PATH . '/mainfile.php', 'wt'); |
||
| 420 | if (!$fp) { |
||
| 421 | echo ERR_COULD_NOT_WRITE_MAINFILE; |
||
| 422 | 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>'; |
||
| 423 | |||
| 424 | return false; |
||
| 425 | } else { |
||
| 426 | $newline = defined(PHP_EOL) ? PHP_EOL : (strpos(php_uname(), 'Windows') ? "\r\n" : "\n"); |
||
| 427 | $content = str_replace(array("\r\n", "\n"), $newline, implode('', $lines)); |
||
| 428 | |||
| 429 | fwrite($fp, $content); |
||
| 430 | fclose($fp); |
||
| 431 | |||
| 432 | return true; |
||
| 433 | } |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @param $task |
||
| 438 | * |
||
| 439 | * @return array|bool |
||
| 440 | */ |
||
| 441 | public function set_configs($task) |
||
| 457 | } |
||
| 458 | } |
||
| 459 | |||
| 460 | $upg = new Upgrade_230(); |
||
| 461 | return $upg; |
||
| 462 |
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.