Conditions | 30 |
Paths | 4080 |
Total Lines | 161 |
Code Lines | 89 |
Lines | 53 |
Ratio | 32.92 % |
Changes | 1 | ||
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 |
||
72 | function xoops_module_update_extgallery(XoopsModule $module, $previousVersion = null) |
||
73 | { |
||
74 | global $xoopsDB; |
||
75 | |||
76 | $moduleDirName = basename(dirname(__DIR__)); |
||
77 | |||
78 | $catHandler = xoops_getModuleHandler('publiccat', $moduleDirName); |
||
79 | $catHandler->rebuild(); |
||
80 | |||
81 | View Code Duplication | if ($previousVersion < 101) { |
|
82 | $db = XoopsDatabaseFactory::getDatabaseConnection(); |
||
83 | // Remove the UNIQUE key on the rating table. This constraint is software cheked now |
||
84 | $sql = 'ALTER TABLE `' . $db->prefix($moduleDirName . '_publicrating') . '` DROP INDEX `photo_rate` ;'; |
||
85 | $db->query($sql); |
||
86 | } |
||
87 | |||
88 | if ($previousVersion < 102) { |
||
89 | $db = XoopsDatabaseFactory::getDatabaseConnection(); |
||
90 | |||
91 | $sql = 'ALTER TABLE `' . $db->prefix($moduleDirName . '_publiccat') . '` ADD `cat_imgurl` VARCHAR(150) NOT NULL AFTER `cat_nb_photo` ;'; |
||
92 | $db->query($sql); |
||
93 | |||
94 | $sql = 'ALTER TABLE `' . $db->prefix($moduleDirName . '_publicphoto') . '` ADD `photo_title` VARCHAR(150) NOT NULL AFTER `photo_id` ;'; |
||
95 | $db->query($sql); |
||
96 | |||
97 | $sql = 'ALTER TABLE `' . $db->prefix($moduleDirName . '_publicphoto') . '` ADD `photo_weight` int(11) NOT NULL AFTER `photo_extra` ;'; |
||
98 | $db->query($sql); |
||
99 | } |
||
100 | |||
101 | if ($previousVersion < 104) { |
||
102 | $db = XoopsDatabaseFactory::getDatabaseConnection(); |
||
103 | |||
104 | $sql = 'ALTER TABLE `' . $db->prefix($moduleDirName . '_publicphoto') . "` ADD `dohtml` BOOL NOT NULL DEFAULT '0';"; |
||
105 | $db->query($sql); |
||
106 | |||
107 | $sql = 'ALTER TABLE `' . $db->prefix($moduleDirName . '_publicphoto') . '` CHANGE `photo_desc` `photo_desc` TEXT;'; |
||
108 | $db->query($sql); |
||
109 | |||
110 | // Set display parmission for all XOOPS base Groups |
||
111 | $sql = 'SELECT cat_id FROM `' . $db->prefix($moduleDirName . '_publiccat') . '`;'; |
||
112 | $result = $db->query($sql); |
||
113 | $module_id = $xoopsModule->getVar('mid'); |
||
114 | /** @var XoopsGroupPermHandler $gpermHandler */ |
||
115 | $gpermHandler = xoops_getHandler('groupperm'); |
||
116 | while ($cat = $db->fetchArray($result)) { |
||
117 | $gpermHandler->addRight('public_displayed', $cat['cat_id'], XOOPS_GROUP_ADMIN, $module_id); |
||
118 | $gpermHandler->addRight('public_displayed', $cat['cat_id'], XOOPS_GROUP_USERS, $module_id); |
||
119 | $gpermHandler->addRight('public_displayed', $cat['cat_id'], XOOPS_GROUP_ANONYMOUS, $module_id); |
||
120 | } |
||
121 | } |
||
122 | |||
123 | if ($previousVersion < 106) { |
||
124 | View Code Duplication | if (!file_exists(XOOPS_ROOT_PATH . '/uploads/extgallery/index.html')) { |
|
125 | $indexFile = XOOPS_ROOT_PATH . '/modules/extgallery/include/index.html'; |
||
126 | copy($indexFile, XOOPS_ROOT_PATH . '/uploads/extgallery/index.html'); |
||
127 | } |
||
128 | |||
129 | View Code Duplication | if (!file_exists(XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/index.html')) { |
|
130 | $indexFile = XOOPS_ROOT_PATH . '/modules/extgallery/include/index.html'; |
||
131 | copy($indexFile, XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/index.html'); |
||
132 | } |
||
133 | } |
||
134 | |||
135 | if ($previousVersion < 107) { |
||
136 | |||
137 | // Fix extension Bug if it's installed |
||
138 | if (file_exists(XOOPS_ROOT_PATH . '/class/textsanitizer/gallery/gallery.php')) { |
||
139 | $conf = include XOOPS_ROOT_PATH . '/class/textsanitizer/config.php'; |
||
140 | $conf['extensions']['gallery'] = 1; |
||
141 | file_put_contents(XOOPS_ROOT_PATH . '/class/textsanitizer/config.custom.php', "<?php\rreturn \$config = " . var_export($conf, true) . "\r?>"); |
||
142 | } |
||
143 | } |
||
144 | |||
145 | View Code Duplication | if ($previousVersion < 109) { |
|
146 | $db = XoopsDatabaseFactory::getDatabaseConnection(); |
||
147 | |||
148 | $sql = 'ALTER TABLE `' . $db->prefix($moduleDirName . '_publiccat') . "` CHANGE `cat_weight` `cat_weight` INT( 11 ) NOT NULL DEFAULT '0' ;"; |
||
149 | $db->query($sql); |
||
150 | } |
||
151 | |||
152 | |||
153 | |||
154 | |||
155 | if ($previousVersion < 111) { |
||
156 | // delete old HTML template files ============================ |
||
157 | $templateDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . '/templates/'); |
||
158 | View Code Duplication | if (is_dir($templateDirectory)) { |
|
159 | $templateList = array_diff(scandir($templateDirectory), array('..', '.')); |
||
160 | foreach ($templateList as $k => $v) { |
||
161 | $fileInfo = new SplFileInfo($templateDirectory . $v); |
||
162 | if ($fileInfo->getExtension() === 'html' && $fileInfo->getFilename() !== 'index.html') { |
||
163 | if (file_exists($templateDirectory . $v)) { |
||
164 | unlink($templateDirectory . $v); |
||
165 | } |
||
166 | } |
||
167 | } |
||
168 | } |
||
169 | // delete old block html template files ============================ |
||
170 | $templateDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') |
||
171 | . '/templates/blocks/'); |
||
172 | View Code Duplication | if (is_dir($templateDirectory)) { |
|
173 | $templateList = array_diff(scandir($templateDirectory), array('..', '.')); |
||
174 | foreach ($templateList as $k => $v) { |
||
175 | $fileInfo = new SplFileInfo($templateDirectory . $v); |
||
176 | if ($fileInfo->getExtension() === 'html' && $fileInfo->getFilename() !== 'index.html') { |
||
177 | if (file_exists($templateDirectory . $v)) { |
||
178 | unlink($templateDirectory . $v); |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | |||
184 | |||
185 | // delete old admin html template files ============================ |
||
186 | $templateDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') |
||
187 | . '/templates/admin/'); |
||
188 | View Code Duplication | if (is_dir($templateDirectory)) { |
|
189 | $templateList = array_diff(scandir($templateDirectory), array('..', '.')); |
||
190 | foreach ($templateList as $k => $v) { |
||
191 | $fileInfo = new SplFileInfo($templateDirectory . $v); |
||
192 | if ($fileInfo->getExtension() === 'html' && $fileInfo->getFilename() !== 'index.html') { |
||
193 | if (file_exists($templateDirectory . $v)) { |
||
194 | unlink($templateDirectory . $v); |
||
195 | } |
||
196 | } |
||
197 | } |
||
198 | } |
||
199 | |||
200 | //delete old files: =================== |
||
201 | // $oldFiles = array( |
||
202 | // '/include/update_functions.php', |
||
203 | // '/include/install_functions.php' |
||
204 | // ); |
||
205 | include_once __DIR__ . '/config.php'; |
||
206 | if (count($oldFiles) > 0) { |
||
207 | foreach (array_keys($oldFiles) as $file) { |
||
208 | if (is_file($file)) { |
||
209 | unlink($GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . $oldFiles[$file])); |
||
210 | } |
||
211 | } |
||
212 | } |
||
213 | |||
214 | //delete .html entries from the tpl table |
||
215 | $sql = 'DELETE FROM ' . $xoopsDB->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', |
||
216 | 'n') |
||
217 | . "' AND `tpl_file` LIKE '%.html%'"; |
||
218 | $xoopsDB->queryF($sql); |
||
219 | |||
220 | // Load class XoopsFile ==================== |
||
221 | xoops_load('XoopsFile'); |
||
222 | |||
223 | //delete /images directory ============ |
||
224 | $imagesDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . '/images/'); |
||
225 | $folderHandler = XoopsFile::getHandler('folder', $imagesDirectory); |
||
226 | $folderHandler->delete($imagesDirectory); |
||
227 | } |
||
228 | |||
229 | $gpermHandler = xoops_getHandler('groupperm'); |
||
230 | |||
231 | return $gpermHandler->deleteByModule($module->getVar('mid'), 'item_read'); |
||
232 | } |
||
233 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.