Conditions | 17 |
Paths | 24 |
Total Lines | 111 |
Code Lines | 71 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 1 | 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 |
||
24 | function protector_oninstall_base($module, $mydirname) |
||
25 | { |
||
26 | /** @var XoopsModule $module */ |
||
27 | // translations on module install |
||
28 | |||
29 | global $ret; // TODO :-D |
||
30 | |||
31 | |||
32 | // Initialize $ret as array if not already an array |
||
33 | if (!isset($ret)) { |
||
34 | $ret = []; |
||
35 | } elseif (!is_array($ret)) { |
||
36 | // Convert to array if it's not one |
||
37 | $ret = [$ret]; |
||
38 | } |
||
39 | |||
40 | /** @var XoopsMySQLDatabase $db */ |
||
41 | $db = XoopsDatabaseFactory::getDatabaseConnection(); |
||
42 | $mid = $module->getVar('mid'); |
||
43 | |||
44 | // TABLES (loading mysql.sql) |
||
45 | $sql_file_path = __DIR__ . '/sql/mysql.sql'; |
||
46 | $prefix_mod = $db->prefix() . '_' . $mydirname; |
||
47 | if (file_exists($sql_file_path)) { |
||
48 | $ret[] = 'SQL file found at <b>' . htmlspecialchars($sql_file_path, ENT_QUOTES | ENT_HTML5) . '</b>.<br> Creating tables...<br>'; |
||
49 | |||
50 | include_once XOOPS_ROOT_PATH . '/class/database/sqlutility.php'; |
||
51 | $sqlutil = new SqlUtility(); //old code is -> $sqlutil =& new SqlUtility ; //hack by Trabis |
||
52 | |||
53 | $sql_query = trim(file_get_contents($sql_file_path)); |
||
54 | $pieces = []; |
||
55 | $sqlutil::splitMySqlFile($pieces, $sql_query); |
||
56 | $created_tables = []; |
||
57 | foreach ($pieces as $piece) { |
||
58 | $prefixed_query = $sqlutil::prefixQuery($piece, $prefix_mod); |
||
59 | if (!$prefixed_query) { |
||
60 | $ret[] = 'Invalid SQL <b>' . htmlspecialchars($piece, ENT_QUOTES | ENT_HTML5) . '</b><br>'; |
||
61 | |||
62 | return false; |
||
63 | } |
||
64 | if (!$db->query($prefixed_query[0])) { |
||
65 | $ret[] = '<b>' . htmlspecialchars($db->error(), ENT_QUOTES | ENT_HTML5) . '</b><br>'; |
||
66 | |||
67 | //var_dump( $db->error() ) ; |
||
68 | return false; |
||
69 | } else { |
||
70 | if (!in_array($prefixed_query[4], $created_tables)) { |
||
71 | $ret[] = 'Table <b>' . htmlspecialchars($prefix_mod . '_' . $prefixed_query[4], ENT_QUOTES | ENT_HTML5) . '</b> created.<br>'; |
||
72 | $created_tables[] = $prefixed_query[4]; |
||
73 | } else { |
||
74 | $ret[] = 'Data inserted to table <b>' . htmlspecialchars($prefix_mod . '_' . $prefixed_query[4], ENT_QUOTES | ENT_HTML5) . '</b>.</br />'; |
||
75 | } |
||
76 | } |
||
77 | } |
||
78 | } |
||
79 | |||
80 | // TEMPLATES |
||
81 | /** @var XoopsTplfileHandler $tplfile_handler */ |
||
82 | $tplfile_handler = xoops_getHandler('tplfile'); |
||
83 | $tpl_path = __DIR__ . '/templates'; |
||
84 | // Check if the directory exists |
||
85 | if (is_dir($tpl_path) && is_readable($tpl_path)) { |
||
86 | // Try to open the directory |
||
87 | if ($handler = opendir($tpl_path . '/')) { |
||
88 | while (false !== ($file = readdir($handler))) { |
||
89 | if ('.' === substr($file, 0, 1)) { |
||
90 | continue; |
||
91 | } |
||
92 | $file_path = $tpl_path . '/' . $file; |
||
93 | if (is_file($file_path) && in_array(strrchr($file, '.'), ['.html', '.css', '.js'])) { |
||
94 | $mtime = (int) (@filemtime($file_path)); |
||
95 | $tplfile = $tplfile_handler->create(); |
||
96 | $tplfile->setVar('tpl_source', file_get_contents($file_path), true); |
||
97 | $tplfile->setVar('tpl_refid', $mid); |
||
98 | $tplfile->setVar('tpl_tplset', 'default'); |
||
99 | $tplfile->setVar('tpl_file', $mydirname . '_' . $file); |
||
100 | $tplfile->setVar('tpl_desc', '', true); |
||
101 | $tplfile->setVar('tpl_module', $mydirname); |
||
102 | $tplfile->setVar('tpl_lastmodified', $mtime); |
||
103 | $tplfile->setVar('tpl_lastimported', 0); |
||
104 | $tplfile->setVar('tpl_type', 'module'); |
||
105 | if (!$tplfile_handler->insert($tplfile)) { |
||
106 | $ret[] = '<span style="color:#ff0000;">ERROR: Could not insert template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES | ENT_HTML5) . '</b> to the database.</span><br>'; |
||
107 | } else { |
||
108 | $tplid = $tplfile->getVar('tpl_id'); |
||
109 | $ret[] = 'Template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES | ENT_HTML5) . '</b> added to the database. (ID: <b>' . $tplid . '</b>)<br>'; |
||
110 | // generate compiled file |
||
111 | include_once XOOPS_ROOT_PATH . '/class/xoopsblock.php'; |
||
112 | include_once XOOPS_ROOT_PATH . '/class/template.php'; |
||
113 | if (!xoops_template_touch((string) $tplid)) { |
||
114 | $ret[] = '<span style="color:#ff0000;">ERROR: Failed compiling template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES | ENT_HTML5) . '</b>.</span><br>'; |
||
115 | } else { |
||
116 | $ret[] = 'Template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES | ENT_HTML5) . '</b> compiled.</span><br>'; |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | } |
||
121 | closedir($handler); |
||
122 | } else { |
||
123 | // Handle the error condition when opendir fails |
||
124 | $ret[] = '<span style="color:#ff0000;">ERROR: Could not open the template directory: <b>' . htmlspecialchars($tpl_path, ENT_QUOTES | ENT_HTML5) . '</b>.</span><br>'; |
||
125 | } |
||
126 | } else { |
||
127 | // Directory does not exist; handle this condition |
||
128 | $ret[] = '<span style="color:#ff0000;">ERROR: The template directory does not exist or is not readable: <b>' . htmlspecialchars($tpl_path, ENT_QUOTES | ENT_HTML5) . '</b>.</span><br>'; |
||
129 | } |
||
130 | include_once XOOPS_ROOT_PATH . '/class/xoopsblock.php'; |
||
131 | include_once XOOPS_ROOT_PATH . '/class/template.php'; |
||
132 | xoops_template_clear_module_cache($mid); |
||
133 | |||
134 | return true; |
||
135 | } |
||
152 |
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.