Passed
Pull Request — master (#7)
by Michael
03:59
created

xoops_module_update_xoopsfaq()   C

Complexity

Conditions 14
Paths 8

Size

Total Lines 71
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 36
c 0
b 0
f 0
nc 8
nop 2
dl 0
loc 71
rs 6.2666

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits of
4
 supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit
6
 authors.
7
8
 This program is distributed in the hope that it will be useful, but
9
 WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * Module: XoopsFAQ
15
 *
16
 * @package   module\xoopsfaq\include
17
 * @author    Richard Griffith <[email protected]>
18
 * @author    trabis <[email protected]>
19
 * @author    XOOPS Module Development Team
20
 * @copyright Copyright (c) 2001-2017 {@link http://xoops.org XOOPS Project}
21
 * @license   http://www.gnu.org/licenses/gpl-2.0.html GNU Public License
22
 * @since     File available since version 1.25
23
 */
24
25
use XoopsModules\Xoopsfaq;
26
27
/* @internal {Make sure you PROTECT THIS FILE} */
28
29
if ((!defined('XOOPS_ROOT_PATH'))
30
    || !($GLOBALS['xoopsUser'] instanceof XoopsUser)
31
    || !($GLOBALS['xoopsUser']->isAdmin())) {
32
    exit('Restricted access' . PHP_EOL);
33
}
34
35
/**
36
 * Pre-installation checks before installation of Xoopsfaq
37
 *
38
 * @param \XoopsModule $module
39
 * @param string       $prev_version version * 100
40
 *
41
 * @return bool success ok to install
42
 *
43
 * @see Xoopsfaq\Utility
44
 *
45
 */
46
function xoops_module_pre_update_xoopsfaq(\XoopsModule $module, $prev_version)
0 ignored issues
show
Unused Code introduced by
The parameter $prev_version is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

46
function xoops_module_pre_update_xoopsfaq(\XoopsModule $module, /** @scrutinizer ignore-unused */ $prev_version)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
47
{
48
    $xoopsSuccess = Xoopsfaq\Utility::checkVerXoops($module);
49
    $phpSuccess   = Xoopsfaq\Utility::checkVerPHP($module);
50
    return $xoopsSuccess && $phpSuccess;
51
}
52
53
/**
54
 * Upgrade works to update Xoopsfaq from previous versions
55
 *
56
 * @param XoopsModule $module
57
 * @param string      $prev_version version * 100
58
 *
59
 * @return bool
60
 *
61
 * @see Xoopsfaq\Utility
62
 *
63
 * @see Xmf\Module\Admin
64
 */
65
function xoops_module_update_xoopsfaq(XoopsModule $module, $prev_version)
66
{
67
    $moduleDirName = $module->getVar('dirname');
68
    $helper        = \XoopsModules\Xoopsfaq\Helper::getInstance();
69
    if (!class_exists('Xoopsfaq\Utility')) {
70
        xoops_load('utility', $moduleDirName);
0 ignored issues
show
Bug introduced by
It seems like $moduleDirName can also be of type array and array; however, parameter $type of xoops_load() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
        xoops_load('utility', /** @scrutinizer ignore-type */ $moduleDirName);
Loading history...
71
    }
72
73
    //----------------------------------------------------------------
74
    // Upgrade for Xoopsfaq < 1.25
75
    //----------------------------------------------------------------
76
    $success = true;
77
78
    $helper->loadLanguage('modinfo');
79
    $helper->loadLanguage('admin');
80
81
    if ($prev_version < 125) {
82
        //----------------------------------------------------------------
83
        // Remove previous .css, .js and .images directories since they've
84
        // been relocated to ./assets
85
        //----------------------------------------------------------------
86
        $old_directories = [
87
            $helper->path('css/'),
88
            $helper->path('js/'),
89
            $helper->path('images/'),
90
        ];
91
        foreach ($old_directories as $old_dir) {
92
            $dirInfo = new SplFileInfo($old_dir);
93
            if ($dirInfo->isDir()) {
94
                // The directory exists so delete it
95
                if (false === Xoopsfaq\Utility::rrmdir($old_dir)) {
96
                    $module->setErrors(sprintf(_AM_XOOPSFAQ_ERROR_BAD_DEL_PATH, $old_dir));
97
                    return false;
98
                }
99
            }
100
            unset($dirInfo);
101
        }
102
103
        //-----------------------------------------------------------------------
104
        // Remove ./template/*.html (except index.html) files since they've
105
        // been replaced by *.tpl files
106
        //-----------------------------------------------------------------------
107
        $path       = $helper->path('templates/');
108
        $unfiltered = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
109
        $iterator   = new RegexIterator($unfiltered, "/.*\.html/");
110
        foreach ($iterator as $name => $fObj) {
111
            if (($fObj->isFile()) && ('index.html' !== $fObj->getFilename())) {
112
                if (false === ($success = unlink($fObj->getPathname()))) {
113
                    $module->setErrors(sprintf(_AM_XOOPSFAQ_ERROR_BAD_REMOVE, $fObj->getPathname()));
114
                    return false;
115
                }
116
            }
117
        }
118
119
        //-----------------------------------------------------------------------
120
        // Now remove a some misc files that were renamed or deprecated
121
        //-----------------------------------------------------------------------
122
        $oldFiles = [
123
            $helper->path('include/functions.php'),
124
            $helper->path('class/utilities.php'),
125
        ];
126
        foreach ($oldFiles as $file) {
127
            if (is_file($file)) {
128
                if (false === ($delOk = unlink($file))) {
129
                    $module->setErrors(sprintf(_AM_XOOPSFAQ_ERROR_BAD_REMOVE, $file));
130
                }
131
                $success = $success && $delOk;
132
            }
133
        }
134
    }
135
    return $success;
136
}
137