| Conditions | 26 |
| Paths | 132 |
| Total Lines | 119 |
| Code Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 9 | ||
| Bugs | 3 | Features | 2 |
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 |
||
| 24 | function protector_onupdate_base($module, $mydirname) |
||
| 25 | { |
||
| 26 | /** @var XoopsModule $module */ |
||
| 27 | // translations on module update |
||
| 28 | |||
| 29 | global $msgs; // TODO :-D |
||
| 30 | |||
| 31 | if (!is_array($msgs)) { |
||
| 32 | $msgs = []; |
||
| 33 | } |
||
| 34 | |||
| 35 | /** @var XoopsMySQLDatabase $db */ |
||
| 36 | $db = XoopsDatabaseFactory::getDatabaseConnection(); |
||
| 37 | $mid = $module->getVar('mid'); |
||
| 38 | |||
| 39 | // TABLES (write here ALTER TABLE etc. if necessary) |
||
| 40 | |||
| 41 | // configs (Though I know it is not a recommended way...) |
||
| 42 | $sql = 'SHOW COLUMNS FROM ' . $db->prefix('config') . " LIKE 'conf_title'"; |
||
| 43 | $result = $db->query($sql); |
||
| 44 | if ($result !== false && $db->isResultSet($result)) { |
||
| 45 | if ($result instanceof mysqli_result && ($myrow = $db->fetchArray($result)) && isset($myrow['Type']) && $myrow['Type'] === 'varchar(30)') { |
||
| 46 | $db->queryF('ALTER TABLE ' . $db->prefix('config') . " MODIFY `conf_title` varchar(255) NOT NULL default '', MODIFY `conf_desc` varchar(255) NOT NULL default ''"); |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | $sql = 'SHOW CREATE TABLE ' . $db->prefix('config'); |
||
| 51 | $result = $db->query($sql); |
||
| 52 | if (false === $result || !($result instanceof mysqli_result) || !$db->isResultSet($result)) { |
||
| 53 | throw new \RuntimeException( |
||
| 54 | \sprintf(_DB_QUERY_ERROR, $sql) . $db->error(), |
||
| 55 | E_USER_ERROR, |
||
| 56 | ); |
||
| 57 | } else { |
||
| 58 | [, $create_string] = $db->fetchRow($result); |
||
| 59 | } |
||
| 60 | |||
| 61 | |||
| 62 | foreach (explode('KEY', $create_string) as $line) { |
||
| 63 | if (preg_match('/(\`conf\_title_\d+\`) \(\`conf\_title\`\)/', $line, $regs)) { |
||
| 64 | $db->query('ALTER TABLE ' . $db->prefix('config') . ' DROP KEY ' . $regs[1]); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | $db->query('ALTER TABLE ' . $db->prefix('config') . ' ADD KEY `conf_title` (`conf_title`)'); |
||
| 68 | |||
| 69 | // 2.x -> 3.0 |
||
| 70 | $sql = 'SHOW CREATE TABLE ' . $db->prefix($mydirname . '_log'); |
||
| 71 | $result = $db->query($sql); |
||
| 72 | |||
| 73 | if (false === $result || !($result instanceof mysqli_result) || !$db->isResultSet($result)) { |
||
| 74 | throw new \RuntimeException( |
||
| 75 | \sprintf(_DB_QUERY_ERROR, $sql) . $db->error(), |
||
| 76 | E_USER_ERROR, |
||
| 77 | ); |
||
| 78 | } else { |
||
| 79 | [, $create_string] = $db->fetchRow($result); |
||
| 80 | } |
||
| 81 | |||
| 82 | |||
| 83 | if (preg_match('/timestamp\(/i', $create_string)) { |
||
| 84 | $db->query('ALTER TABLE ' . $db->prefix($mydirname . '_log') . ' MODIFY `timestamp` DATETIME'); |
||
| 85 | } |
||
| 86 | |||
| 87 | // TEMPLATES (all templates have been already removed by modulesadmin) |
||
| 88 | /** @var XoopsTplfileHandler $tplfile_handler */ |
||
| 89 | $tplfile_handler = xoops_getHandler('tplfile'); |
||
| 90 | $tpl_path = __DIR__ . '/templates'; |
||
| 91 | // Check if the directory exists |
||
| 92 | if (is_dir($tpl_path) && is_readable($tpl_path)) { |
||
| 93 | // Try to open the directory |
||
| 94 | if ($handler = opendir($tpl_path . '/')) { |
||
| 95 | while (($file = readdir($handler)) !== false) { |
||
| 96 | if (substr($file, 0, 1) === '.') { |
||
| 97 | continue; |
||
| 98 | } |
||
| 99 | $file_path = $tpl_path . '/' . $file; |
||
| 100 | if (is_file($file_path) && in_array(strrchr($file, '.'), ['.html', '.css', '.js'])) { |
||
| 101 | $mtime = (int) (@filemtime($file_path)); |
||
| 102 | $tplfile = $tplfile_handler->create(); |
||
| 103 | $tplfile->setVar('tpl_source', file_get_contents($file_path), true); |
||
| 104 | $tplfile->setVar('tpl_refid', $mid); |
||
| 105 | $tplfile->setVar('tpl_tplset', 'default'); |
||
| 106 | $tplfile->setVar('tpl_file', $mydirname . '_' . $file); |
||
| 107 | $tplfile->setVar('tpl_desc', '', true); |
||
| 108 | $tplfile->setVar('tpl_module', $mydirname); |
||
| 109 | $tplfile->setVar('tpl_lastmodified', $mtime); |
||
| 110 | $tplfile->setVar('tpl_lastimported', 0); |
||
| 111 | $tplfile->setVar('tpl_type', 'module'); |
||
| 112 | if (!$tplfile_handler->insert($tplfile)) { |
||
| 113 | $ret[] = '<span style="color:#ff0000;">ERROR: Could not insert template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES | ENT_HTML5) . '</b> to the database.</span><br>'; |
||
| 114 | } else { |
||
| 115 | $tplid = $tplfile->getVar('tpl_id'); |
||
| 116 | $msgs[] = 'Template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES | ENT_HTML5) . '</b> added to the database. (ID: <b>' . $tplid . '</b>)<br>'; |
||
| 117 | // generate compiled file |
||
| 118 | include_once XOOPS_ROOT_PATH . '/class/xoopsblock.php'; |
||
| 119 | include_once XOOPS_ROOT_PATH . '/class/template.php'; |
||
| 120 | if (!xoops_template_touch((string) $tplid)) { |
||
| 121 | $msgs[] = '<span style="color:#ff0000;">ERROR: Failed compiling template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES | ENT_HTML5) . '</b>.</span><br>'; |
||
| 122 | } else { |
||
| 123 | $msgs[] = 'Template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES | ENT_HTML5) . '</b> compiled.</span><br>'; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } |
||
| 127 | } |
||
| 128 | closedir($handler); |
||
| 129 | } else { |
||
| 130 | // Handle the error condition when opendir fails |
||
| 131 | $msgs[] = '<span style="color:#ff0000;">ERROR: Could not open the template directory: <b>' . htmlspecialchars($tpl_path) . '</b>.</span>'; |
||
| 132 | } |
||
| 133 | } else { |
||
| 134 | // Directory does not exist; handle this condition |
||
| 135 | $msgs[] = '<span style="color:#ff0000;">ERROR: The template directory does not exist or is not readable: <b>' . htmlspecialchars($tpl_path) . '</b>.</span><br>'; |
||
| 136 | } |
||
| 137 | |||
| 138 | include_once XOOPS_ROOT_PATH . '/class/xoopsblock.php'; |
||
| 139 | include_once XOOPS_ROOT_PATH . '/class/template.php'; |
||
| 140 | xoops_template_clear_module_cache($mid); |
||
| 141 | |||
| 142 | return true; |
||
| 143 | } |
||
| 160 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.