xoops_module_update_waiting()   B
last analyzed

Complexity

Conditions 9
Paths 1

Size

Total Lines 64
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 9
eloc 46
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 64
rs 7.6226

How to fix   Long Method   

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 declare(strict_types=1);
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: Waiting
15
 *
16
 * @category        Module
17
 * @author          XOOPS Module Development Team
18
 * @copyright       {@link https://xoops.org 2001-2016 XOOPS Project}
19
 * @license         {@link https://www.fsf.org/copyleft/gpl.html GNU public license}
20
 * @link            https://xoops.org XOOPS
21
 */
22
if ((!defined('XOOPS_ROOT_PATH')) || !($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) {
23
    exit('Restricted Access - ' . basename($_SERVER['SCRIPT_NAME']) . PHP_EOL);
24
}
25
/**
26
 * Waiting Block installation update
27
 * @param \XoopsModule $module  {@see XoopsModule}
28
 * @param int          $version currently installed module version
29
 *
30
 * @return bool success
31
 */
32
function xoops_module_update_waiting($module, $version)
0 ignored issues
show
Unused Code introduced by
The parameter $module 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

32
function xoops_module_update_waiting(/** @scrutinizer ignore-unused */ $module, $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...
Unused Code introduced by
The parameter $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

32
function xoops_module_update_waiting($module, /** @scrutinizer ignore-unused */ $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...
33
{
34
    return true;  // force return - following code is really only needed for XOOPS < 2.3
35
    // Keep Block option values when update (by nobunobu)
36
    $mid = $module->mid();
0 ignored issues
show
Unused Code introduced by
$mid = $module->mid() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
37
    if ($mid) {
38
        /** @var \XoopsConfigHandler $configHandler */
39
        $configHandler       = xoops_getHandler('config');
40
        $waitingModuleConfig = $configHandler->getConfigsByCat(0, $mid);
41
42
        $count = count($waitingModuleConfig['blocks']);
43
44
        /** @var \XoopsBlockHandler $blockHandler */
45
        $blockHandler = xoops_getHandler('block');
46
        $criteria     = new \CriteriaCompo();
47
        $criteria->add(new \Criteria('mid', $mid));
48
        $criteria->add(new \Criteria('block_type', 'D'));
49
        $criteria->add(new \Criteria('func_num', $count, '>'));
50
        $blockObjs = $blockHandler->getAll($criteria);
51
52
        foreach ($blockObjs as $blockObj) {
53
            $local_msgs[] = "Non Defined Block <b>{$fblock['name']}</b> will be deleted";
54
            $success      = $blockHandler->delete($blockObj); // remove the invalid block
55
        }
56
57
        $fieldsArray = ['func_num', 'name', 'options'];
58
        $criteria    = new \CriteriaCompo();
59
        $criteria->add(new \Criteria('mid', $mid));
60
        //        $criteria->add(new \Criteria('func_num', $i));
61
        $criteria->add(new \Criteria('show_func'), addslashes($waitingModuleConfig['blocks'][$i]['show_func']));
62
        $criteria->add(new \Criteria('func_file', addslashes($waitingModuleConfig['blocks'][$i]['file'])));
63
        $fblockObjs = $blockHandler->getObjects($criteria);
64
        foreach ($fblockObjs as $fblockObj) {
65
            if (!empty($fblockObj->options())) {
66
                $old_vals = explode('|', $fblockObj->getVar('options'));
67
                $def_vals = explode('|', $modversion['blocks'][$fblockObj->getVar('func_num')]['options']);
68
                if (count($old_vals) == count($def_vals)) {
69
                    $modversion['blocks'][$fblock->getVar('func_num')]['options'] = $fblockObj->getVar('options');
70
                    $local_msgs[]                                                 = "Option's values of the block <b>" . $fblockObj->getVar('name') . '</b> will be kept. (value = <b>' . $fblockObj->getVar('options') . '</b>)';
71
                } elseif (count($old_vals) < count($def_vals)) {
72
                    $def_vals                                                        = array_merge($old_vals, $def_vals); //merges prev. values with new - older are preserved
73
                    $modversion['blocks'][$fblockObj->getVar('func_num')]['options'] = implode('|', $def_vals);
74
                    $local_msgs[]                                                    = "Option's values of the block "
75
                                                                                       . '<b>'
76
                                                                                       . $fblockObj->getVar('name')
77
                                                                                       . '</b> '
78
                                                                                       . 'will be kept and new option(s) are added. (value = '
79
                                                                                       . '<b>'
80
                                                                                       . $waitingModuleConfig['blocks'][$fblockObj->getVar('options')]
81
                                                                                       . '</b>)';
82
                } else {
83
                    $local_msgs[] = "Option's values of the block " . '<b>' . $fblockObj->getVar('name') . '</b> ' . 'will be reset to the default, because of some decrease of options. (value = ' . '<b>' . $waitingModuleConfig['blocks'][$fblockObj->getVar('func_num')]['options'] . '</b>)';
84
                }
85
            }
86
        }
87
    }
88
89
    global $msgs, $myblocksadmin_parsed_updateblock;
90
    if (!empty($msgs) && empty($myblocksadmin_parsed_updateblock)) {
91
        $msgs                             = array_merge($msgs, $local_msgs);
92
        $myblocksadmin_parsed_updateblock = true;
93
    }
94
95
    return true;  //@todo: for now force success - eventually send 'actual' results
96
}
97