onupdate.php ➔ xoops_module_update_randomquote()   D
last analyzed

Complexity

Conditions 18
Paths 33

Size

Total Lines 83

Duplication

Lines 13
Ratio 15.66 %

Importance

Changes 0
Metric Value
cc 18
nc 33
nop 2
dl 13
loc 83
rs 4.8666
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
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
*/
12
13
/**
14
 * Module: randomquote
15
 *
16
 * @category        Module
17
 * @package         randomquote
18
 * @author          XOOPS Development Team <[email protected]> - <https://xoops.org>
19
 * @copyright       {@link https://xoops.org/ XOOPS Project}
20
 * @license         GPL 2.0 or later
21
 * @link            https://xoops.org/
22
 * @since           1.0.0
23
 */
24
25
use Xoopsmodules\randomquote;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, randomquote.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
26
27
if ((!defined('XOOPS_ROOT_PATH')) || !$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...
28
    || !$GLOBALS['xoopsUser']->IsAdmin()) {
29
    exit('Restricted access' . PHP_EOL);
30
}
31
32
/**
33
 * @param string $tablename
34
 *
35
 * @return bool
36
 */
37
function tableExists($tablename)
38
{
39
    $result = $GLOBALS['xoopsDB']->queryF("SHOW TABLES LIKE '$tablename'");
40
41
    return ($GLOBALS['xoopsDB']->getRowsNum($result) > 0);
42
}
43
44
/**
45
 *
46
 * Prepares system prior to attempting to install module
47
 * @param \XoopsModule $module {@link XoopsModule}
48
 *
49
 * @return bool true if ready to install, false if not
50
 */
51
function xoops_module_pre_update_randomquote(\XoopsModule $module)
52
{
53
    /** @var randomquote\Helper $helper */
54
    /** @var randomquote\Utility $utility */
55
    $helper  = randomquote\Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
$helper is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
56
    $utility = new randomquote\Utility();
57
58
    $xoopsSuccess = $utility::checkVerXoops($module);
59
    $phpSuccess   = $utility::checkVerPhp($module);
60
    return $xoopsSuccess && $phpSuccess;
61
}
62
63
/**
64
 *
65
 * Performs tasks required during update of the module
66
 * @param \XoopsModule $module {@link XoopsModule}
67
 * @param null         $previousVersion
68
 *
69
 * @return bool true if update successful, false if not
70
 */
71
72
function xoops_module_update_randomquote(\XoopsModule $module, $previousVersion = null)
73
{
74
    $moduleDirName      = basename(dirname(__DIR__));
75
    $moduleDirNameUpper = strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
$moduleDirNameUpper is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
76
77
    /** @var randomquote\Helper $helper */
78
    /** @var randomquote\Utility $utility */
79
    /** @var randomquote\common\Configurator $configurator */
80
    $helper       = randomquote\Helper::getInstance();
81
    $utility      = new randomquote\Utility();
82
    $configurator = new randomquote\common\Configurator();
83
    $helper->loadLanguage('common');
84
85
    if ($previousVersion < 240) {
86
87
        //delete old HTML templates
88
        if (count($configurator->templateFolders) > 0) {
89
            foreach ($configurator->templateFolders as $folder) {
90
                $templateFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $folder);
91
                if (is_dir($templateFolder)) {
92
                    $templateList = array_diff(scandir($templateFolder, SCANDIR_SORT_NONE), ['..', '.']);
93
                    foreach ($templateList as $k => $v) {
94
                        $fileInfo = new SplFileInfo($templateFolder . $v);
95
                        if ('html' === $fileInfo->getExtension() && 'index.html' !== $fileInfo->getFilename()) {
96
                            if (file_exists($templateFolder . $v)) {
97
                                unlink($templateFolder . $v);
98
                            }
99
                        }
100
                    }
101
                }
102
            }
103
        }
104
105
        //  ---  DELETE OLD FILES ---------------
106
        if (count($configurator->oldFiles) > 0) {
107
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
108
            foreach (array_keys($configurator->oldFiles) as $i) {
109
                $tempFile = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFiles[$i]);
110
                if (is_file($tempFile)) {
111
                    unlink($tempFile);
112
                }
113
            }
114
        }
115
116
        //  ---  DELETE OLD FOLDERS ---------------
117
        xoops_load('XoopsFile');
118
        if (count($configurator->oldFolders) > 0) {
119
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
120
            foreach (array_keys($configurator->oldFolders) as $i) {
121
                $tempFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFolders[$i]);
122
                /** @var XoopsObjectHandler $folderHandler */
123
                $folderHandler = \XoopsFile::getHandler('folder', $tempFolder);
124
                $folderHandler->delete($tempFolder);
125
            }
126
        }
127
128
        //  ---  CREATE FOLDERS ---------------
129 View Code Duplication
        if (count($configurator->uploadFolders) > 0) {
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...
130
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
131
            foreach (array_keys($configurator->uploadFolders) as $i) {
132
                $utility::createFolder($configurator->uploadFolders[$i]);
133
            }
134
        }
135
136
        //  ---  COPY blank.png FILES ---------------
137 View Code Duplication
        if (count($configurator->copyBlankFiles) > 0) {
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...
138
            $file = __DIR__ . '/../assets/images/blank.png';
139
            foreach (array_keys($configurator->copyBlankFiles) as $i) {
140
                $dest = $configurator->copyBlankFiles[$i] . '/blank.png';
141
                $utility::copyFile($file, $dest);
142
            }
143
        }
144
145
        //delete .html entries from the tpl table
146
        $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . "' AND `tpl_file` LIKE '%.html%'";
147
        $GLOBALS['xoopsDB']->queryF($sql);
148
149
        /** @var XoopsGroupPermHandler $gpermHandler */
150
        $gpermHandler = xoops_getHandler('groupperm');
151
        return $gpermHandler->deleteByModule($module->getVar('mid'), 'item_read');
152
    }
153
    return true;
154
}
155