Conditions | 17 |
Paths | 14 |
Total Lines | 103 |
Code Lines | 50 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
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 |
||
56 | function xoops_module_update_xsitemap(\XoopsModule $module, $previousVersion = null) |
||
57 | { |
||
58 | /*====================================================================== |
||
59 | //---------------------------------------------------------------- |
||
60 | // Remove xSitemap uploads folder (and all subfolders) if they exist |
||
61 | //----------------------------------------------------------------* |
||
62 | $utility = new Utility(); |
||
63 | if (!class_exists($utility)) { |
||
64 | xoops_load('utility', $moduleDirName); |
||
65 | } |
||
66 | |||
67 | // Recursively delete directories |
||
68 | $xsUploadDir = realpath(XOOPS_UPLOAD_PATH . "/" . $module->dirname()); |
||
69 | $success = $utility::rrmdir($xsUploadDir); |
||
70 | if (true !== $success) { |
||
71 | \Xmf\Language::load('admin', $module->dirname()); |
||
72 | $module->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_DEL_PATH, $xsUploadDir)); |
||
73 | } |
||
74 | return $success; |
||
75 | ======================================================================*/ |
||
76 | $moduleDirName = \basename(\dirname(__DIR__)); |
||
77 | $moduleDirNameUpper = mb_strtoupper($moduleDirName); |
||
78 | $helper = Helper::getInstance(); |
||
79 | $utility = new Utility(); |
||
80 | //----------------------------------------------------------------------- |
||
81 | // Upgrade for Xsitemap < 1.54 |
||
82 | //----------------------------------------------------------------------- |
||
83 | $success = true; |
||
84 | $helper->loadLanguage('modinfo'); |
||
85 | $helper->loadLanguage('admin'); |
||
86 | if ($previousVersion < 154) { |
||
87 | //---------------------------------------------------------------- |
||
88 | // Remove previous css & images directories since they've been relocated to ./assets |
||
89 | // Also remove uploads directories since they're no longer used |
||
90 | //---------------------------------------------------------------- |
||
91 | $old_directories = [ |
||
92 | $helper->path('css/'), |
||
93 | $helper->path('js/'), |
||
94 | $helper->path('images/'), |
||
95 | XOOPS_UPLOAD_PATH . '/' . $module->dirname(), |
||
96 | ]; |
||
97 | foreach ($old_directories as $old_dir) { |
||
98 | $dirInfo = new \SplFileInfo($old_dir); |
||
99 | if ($dirInfo->isDir()) { |
||
100 | // The directory exists so delete it |
||
101 | if (false === $utility::rrmdir($old_dir)) { |
||
102 | $module->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_DEL_PATH, $old_dir)); |
||
103 | return false; |
||
104 | } |
||
105 | } |
||
106 | unset($dirInfo); |
||
107 | } |
||
108 | //----------------------------------------------------------------------- |
||
109 | // Remove ./template/*.html (except index.html) files since they've |
||
110 | // been replaced by *.tpl files |
||
111 | // Note: this will also remove /template/xsitemap_style.html since it's no longer used |
||
112 | //----------------------------------------------------------------------- |
||
113 | $path = $helper->path('templates/'); |
||
114 | $unfiltered = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); |
||
115 | $iterator = new RegexIterator($unfiltered, '/.*\.html/'); |
||
116 | foreach ($iterator as $name => $fObj) { |
||
117 | if ($fObj->isFile() && ('index.html' !== $fObj->getFilename())) { |
||
118 | if (false === ($success = unlink($fObj->getPathname()))) { |
||
119 | $module->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_REMOVE, $fObj->getPathname())); |
||
120 | return false; |
||
121 | } |
||
122 | } |
||
123 | } |
||
124 | //----------------------------------------------------------------------- |
||
125 | // Now remove a some misc files that were renamed or deprecated |
||
126 | //----------------------------------------------------------------------- |
||
127 | $oldFiles = [ |
||
128 | $helper->path('include/install.php'), |
||
129 | $helper->path('class/module.php'), |
||
130 | $helper->path('class/menu.php'), |
||
131 | ]; |
||
132 | foreach ($oldFiles as $file) { |
||
133 | if (is_file($file)) { |
||
134 | if (false === ($delOk = unlink($file))) { |
||
135 | $module->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_REMOVE, $file)); |
||
136 | } |
||
137 | $success = $success && $delOk; |
||
138 | } |
||
139 | } |
||
140 | } |
||
141 | if ($previousVersion < 156) { |
||
142 | // update table (add new field) |
||
143 | $table = $GLOBALS['xoopsDB']->prefix('xsitemap_plugin'); |
||
144 | $field = 'plugin_where'; |
||
145 | $check = $GLOBALS['xoopsDB']->queryF('SHOW COLUMNS FROM `' . $table . "` LIKE '" . $field . "'"); |
||
146 | $numRows = $GLOBALS['xoopsDB']->getRowsNum($check); |
||
147 | if (!$numRows) { |
||
148 | $sql = "ALTER TABLE `$table` ADD `$field` VARCHAR(255) NOT NULL AFTER `plugin_weight`;"; |
||
149 | if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) { |
||
150 | xoops_error($GLOBALS['xoopsDB']->error() . '<br>' . $sql); |
||
151 | $module->setErrors("Error when adding '$field' to table '$table'."); |
||
152 | $ret = false; |
||
153 | } |
||
154 | $success = $success && $result; |
||
155 | } |
||
156 | } |
||
157 | |||
158 | return $success; |
||
159 | } |
||
160 |