Completed
Push — master ( d1a68a...39bb73 )
by Michael
01:32
created

onupdate.php ➔ xoops_module_update_xoopsfaq()   C

Complexity

Conditions 14
Paths 8

Size

Total Lines 72

Duplication

Lines 7
Ratio 9.72 %

Importance

Changes 0
Metric Value
cc 14
nc 8
nop 2
dl 7
loc 72
rs 5.6242
c 0
b 0
f 0

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 View Code Duplication
if ((!defined('XOOPS_ROOT_PATH'))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
    || !($GLOBALS['xoopsUser'] instanceof XoopsUser)
0 ignored issues
show
Bug introduced by
The class XoopsUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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.

This check looks from 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);
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 View Code Duplication
                if (false === ($success = unlink($fObj->getPathname()))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
                if (false === ($delOk = unlink($file))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
                    $module->setErrors(sprintf(_AM_XOOPSFAQ_ERROR_BAD_REMOVE, $file));
130
                }
131
                $success = $success && $delOk;
132
            }
133
        }
134
    }
135
    return $success;
136
}
137