| Conditions | 14 |
| Paths | 8 |
| Total Lines | 94 |
| Code Lines | 38 |
| Lines | 14 |
| Ratio | 14.89 % |
| Changes | 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 |
||
| 108 | function xoops_module_update_xsitemap($module, $curr_version = null) |
||
| 109 | { |
||
| 110 | /*====================================================================== |
||
| 111 | //---------------------------------------------------------------- |
||
| 112 | // Remove xSitemap uploads folder (and all subfolders) if they exist |
||
| 113 | //----------------------------------------------------------------* |
||
| 114 | $utilsClass = ucfirst($moduleDirName) . 'Utilities'; |
||
| 115 | if (!class_exists($utilsClass)) { |
||
| 116 | xoops_load('utilities', $moduleDirName); |
||
| 117 | } |
||
| 118 | |||
| 119 | // Recursively delete directories |
||
| 120 | $xsUploadDir = realpath(XOOPS_UPLOAD_PATH . "/" . $module->dirname()); |
||
| 121 | $success = $utilsClass::rrmdir($xsUploadDir); |
||
| 122 | if (true !== $success) { |
||
| 123 | \Xmf\Language::load('admin', $module->dirname()); |
||
| 124 | $module->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_DEL_PATH, $xsUploadDir)); |
||
| 125 | } |
||
| 126 | return $success; |
||
| 127 | ======================================================================*/ |
||
| 128 | |||
| 129 | $moduleDirName = $module->getVar('dirname'); |
||
| 130 | $xsHelper = \Xmf\Module\Helper::getHelper($moduleDirName); |
||
| 131 | |||
| 132 | $utilsClass = ucfirst($moduleDirName) . 'Utilities'; |
||
| 133 | if (!class_exists($utilsClass)) { |
||
| 134 | xoops_load('utilities', $moduleDirName); |
||
| 135 | } |
||
| 136 | //----------------------------------------------------------------------- |
||
| 137 | // Upgrade for Xsitemap < 1.54 |
||
| 138 | //----------------------------------------------------------------------- |
||
| 139 | |||
| 140 | $success = true; |
||
| 141 | |||
| 142 | $xsHelper->loadLanguage('modinfo'); |
||
| 143 | $xsHelper->loadLanguage('admin'); |
||
| 144 | |||
| 145 | if ($prev_version < 154) { |
||
| 146 | //---------------------------------------------------------------- |
||
| 147 | // Remove previous css & images directories since they've been relocated to ./assets |
||
| 148 | // Also remove uploads directories since they're no longer used |
||
| 149 | //---------------------------------------------------------------- |
||
| 150 | $old_directories = array($xsHelper->path('css/'), |
||
| 151 | $xsHelper->path('js/'), |
||
| 152 | $xsHelper->path('images/'), |
||
| 153 | XOOPS_UPLOAD_PATH . "/" . $module->dirname() |
||
| 154 | ); |
||
| 155 | View Code Duplication | foreach ($old_directories as $old_dir) { |
|
| 156 | $dirInfo = new SplFileInfo($old_dir); |
||
| 157 | if ($dirInfo->isDir()) { |
||
| 158 | // The directory exists so delete it |
||
| 159 | if (false === $utilsClass::rrmdir($old_dir)) { |
||
| 160 | $xoopsModule->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_DEL_PATH, $old_dir)); |
||
| 161 | return false; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | unset($dirInfo); |
||
| 165 | } |
||
| 166 | |||
| 167 | //----------------------------------------------------------------------- |
||
| 168 | // Remove ./template/*.html (except index.html) files since they've |
||
| 169 | // been replaced by *.tpl files |
||
| 170 | // Note: this will also remove /template/xsitemap_style.html since it's no longer used |
||
| 171 | //----------------------------------------------------------------------- |
||
| 172 | $path = $xsHelper->path('templates/'); |
||
| 173 | $unfiltered = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); |
||
| 174 | $iterator = new RegexIterator($unfiltered, "/.*\.html/"); |
||
| 175 | foreach ($iterator as $name => $fObj) { |
||
| 176 | if (($fObj->isFile()) && ('index.html' !== $fObj->getFilename())) { |
||
| 177 | if (false === ($success = unlink($fObj->getPathname()))) { |
||
| 178 | $xoopsModule->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_REMOVE, $fObj->getPathname())); |
||
| 179 | return false; |
||
| 180 | } |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | //----------------------------------------------------------------------- |
||
| 185 | // Now remove a some misc files that were renamed or deprecated |
||
| 186 | //----------------------------------------------------------------------- |
||
| 187 | $oldFiles = array($xsHelper->path('include/install.php'), |
||
| 188 | $xsHelper->path('class/module.php'), |
||
| 189 | $xsHelper->path('class/menu.php') |
||
| 190 | ); |
||
| 191 | foreach($oldFiles as $file) { |
||
| 192 | if (is_file($file)) { |
||
| 193 | View Code Duplication | if (false === ($delOk = unlink($file))) { |
|
| 194 | $module->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_REMOVE, $file)); |
||
| 195 | } |
||
| 196 | $success = $success && $delOk; |
||
| 197 | } |
||
| 198 | } |
||
| 199 | } |
||
| 200 | return $success; |
||
| 201 | } |
||
| 202 | |||
| 264 |
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.