Passed
Push — master ( cf57bc...9eba79 )
by Michael
01:41
created

xoops_module_update_gbook()   C

Complexity

Conditions 14
Paths 5

Size

Total Lines 75
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 14
eloc 41
nc 5
nop 2
dl 0
loc 75
rs 5.3217
c 2
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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 32 and the first side effect is on line 24.

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.

Loading history...
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright    XOOPS Project https://xoops.org/
14
 * @license      GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package
16
 * @author       XOOPS Development Team
17
 */
18
19
use XoopsModules\Gbook;
20
21
if ((!defined('XOOPS_ROOT_PATH')) || !($GLOBALS['xoopsUser'] instanceof \XoopsUser)
0 ignored issues
show
Bug introduced by
The type XoopsUser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
    || !$GLOBALS['xoopsUser']->IsAdmin()
23
) {
24
    exit('Restricted access' . PHP_EOL);
25
}
26
27
/**
28
 * @param string $tablename
29
 *
30
 * @return bool
31
 */
32
function tableExists($tablename)
33
{
34
    $result = $GLOBALS['xoopsDB']->queryF("SHOW TABLES LIKE '$tablename'");
35
36
    return $GLOBALS['xoopsDB']->getRowsNum($result) > 0;
37
}
38
39
/**
40
 *
41
 * Prepares system prior to attempting to install module
42
 * @param XoopsModule $module {@link XoopsModule}
43
 *
44
 * @return bool true if ready to install, false if not
45
 */
46
function xoops_module_pre_update_gbook(\XoopsModule $module)
47
{
48
    /** @var Gbook\Helper $helper */
49
    /** @var Gbook\Utility $utility */
50
    $moduleDirName = basename(dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirName is dead and can be removed.
Loading history...
51
    $helper       = Gbook\Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
52
    $utility      = new Gbook\Utility();
53
54
    $xoopsSuccess = $utility::checkVerXoops($module);
55
    $phpSuccess   = $utility::checkVerPhp($module);
56
    return $xoopsSuccess && $phpSuccess;
57
}
58
59
/**
60
 *
61
 * Performs tasks required during update of the module
62
 * @param XoopsModule $module {@link XoopsModule}
63
 * @param null        $previousVersion
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $previousVersion is correct as it would always require null to be passed?
Loading history...
64
 *
65
 * @return bool true if update successful, false if not
66
 */
67
68
function xoops_module_update_gbook(\XoopsModule $module, $previousVersion = null)
69
{
70
    global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
71
72
    $moduleDirName = basename(dirname(__DIR__));
73
    $capsDirName   = strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
The assignment to $capsDirName is dead and can be removed.
Loading history...
74
75
    /** @var Gbook\Helper $helper */
76
    /** @var Gbook\Utility $utility */
77
    /** @var Gbook\Common\Configurator $configurator */
78
    $helper  = Gbook\Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
79
    $utility = new Gbook\Utility();
0 ignored issues
show
Unused Code introduced by
The assignment to $utility is dead and can be removed.
Loading history...
80
    $configurator = new Gbook\Configurator();
0 ignored issues
show
Unused Code introduced by
The assignment to $configurator is dead and can be removed.
Loading history...
Bug introduced by
The type XoopsModules\Gbook\Configurator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
81
82
   
83
    if ($previousVersion < 111) {
84
        // delete old HTML template files ============================
85
        $templateDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . '/templates/');
86
        if (is_dir($templateDirectory)) {
87
            $templateList = array_diff(scandir($templateDirectory, SCANDIR_SORT_NONE), ['..', '.']);
88
            foreach ($templateList as $k => $v) {
89
                $fileInfo = new \SplFileInfo($templateDirectory . $v);
90
                if ('html' === $fileInfo->getExtension() && 'index.html' !== $fileInfo->getFilename()) {
91
                    if (file_exists($templateDirectory . $v)) {
92
                        unlink($templateDirectory . $v);
93
                    }
94
                }
95
            }
96
        }
97
        // delete old block html template files ============================
98
        $templateDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n')
99
                                                     . '/templates/blocks/');
100
        if (is_dir($templateDirectory)) {
101
            $templateList = array_diff(scandir($templateDirectory, SCANDIR_SORT_NONE), ['..', '.']);
102
            foreach ($templateList as $k => $v) {
103
                $fileInfo = new \SplFileInfo($templateDirectory . $v);
104
                if ('html' === $fileInfo->getExtension() && 'index.html' !== $fileInfo->getFilename()) {
105
                    if (file_exists($templateDirectory . $v)) {
106
                        unlink($templateDirectory . $v);
107
                    }
108
                }
109
            }
110
        }
111
        //delete old files: ===================
112
        $oldFiles = [
113
            '/assets/images/logo_module.png',
114
            '/templates/gbook.css'
115
        ];
116
117
        foreach (array_keys($oldFiles) as $file) {
118
            if (is_file($file)) {
119
                unlink($GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . $oldFiles[$file]));
120
            }
121
        }
122
123
        //delete .html entries from the tpl table
124
        $sql = 'DELETE FROM ' . $xoopsDB->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar(
125
            'dirname',
126
                                                                                                          'n'
127
        )
128
               . "' AND `tpl_file` LIKE '%.html%'";
129
        $xoopsDB->queryF($sql);
130
131
        // Load class XoopsFile ====================
132
        xoops_load('XoopsFile');
0 ignored issues
show
Bug introduced by
The function xoops_load was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

132
        /** @scrutinizer ignore-call */ 
133
        xoops_load('XoopsFile');
Loading history...
133
134
        //delete /images directory ============
135
        $imagesDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . '/images/');
136
        $folderHandler   = XoopsFile::getHandler('folder', $imagesDirectory);
0 ignored issues
show
Bug introduced by
The type XoopsFile was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
137
        $folderHandler->delete($imagesDirectory);
138
    }
139
140
    $gpermHandler = xoops_getHandler('groupperm');
0 ignored issues
show
Bug introduced by
The function xoops_getHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

140
    $gpermHandler = /** @scrutinizer ignore-call */ xoops_getHandler('groupperm');
Loading history...
141
142
    return $gpermHandler->deleteByModule($module->getVar('mid'), 'item_read');
143
}
144