Passed
Push — master ( 158267...cd0d6c )
by Michael
02:28
created

b_waiting_waiting_show()   F

Complexity

Conditions 22
Paths 274

Size

Total Lines 96
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 22
eloc 51
nc 274
nop 1
dl 0
loc 96
rs 2.4083
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
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
 * Module: Waiting
13
 *
14
 * @category        Module
15
 * @package         waiting
16
 * @author          Kazumi Ono (AKA onokazu)
17
 * @author          XOOPS Module Development Team
18
 * @copyright       {@link https://xoops.org 2001-2016 XOOPS Project}
19
 * @license         {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
20
 * @link            http://www.myweb.ne.jp/
21
 * @link            https://xoops.org XOOPS
22
 * @since           2.00
23
 */
24
25
use XoopsModules\Waiting;
26
27
// EXTENSIBLE "waiting block" by plugins in both waiting and modules
28
29
/**
30
 * @param $options
31
 * @return array
32
 */
33
function b_waiting_waiting_show($options)
34
{
35
    $userLang = $GLOBALS['xoopsConfig']['language'];
36
37
    $sql_cache_min  = empty($options[1]) ? 0 : (int)$options[1];
38
    $sql_cache_file = XOOPS_CACHE_PATH . '/waiting_touch';
39
40
    // SQL cache check (you have to use this cache with block's cache by system)
41
    if (file_exists($sql_cache_file)) {
42
        $sql_cache_mtime = filemtime($sql_cache_file);
43
        if (time() < $sql_cache_mtime + $sql_cache_min * 60) {
44
            return [];
45
        } else {
46
            unlink($sql_cache_file);
47
        }
48
    }
49
50
    require_once dirname(__DIR__) . '/include/functions.php';
51
52
    /** @var \XoopsModules\Waiting\Helper $helper */
53
    $helper      = \XoopsModules\Waiting\Helper::getInstance();
54
    // read language files for plugins
55
    $helper->loadLanguage('plugins');
56
57
    $plugins_path = XOOPS_ROOT_PATH . '/modules/waiting/plugins';
0 ignored issues
show
Unused Code introduced by
The assignment to $plugins_path is dead and can be removed.
Loading history...
58
    /** @var \XoopsMySQLDatabase $xoopsDB */
59
    $xoopsDB      = \XoopsDatabaseFactory::getDatabaseConnection();
0 ignored issues
show
Unused Code introduced by
The assignment to $xoopsDB is dead and can be removed.
Loading history...
60
    /** @var \XoopsModuleHandler $moduleHandler */
61
    $moduleHandler = xoops_getHandler('module');
62
    $block         = [];
63
64
    // get module's list installed
65
    $mod_lists = $moduleHandler->getList(new \Criteria(''), true);
66
    foreach ($mod_lists as $dirname => $name) {
67
        $plugin_info = waiting_get_plugin_info($dirname, $userLang);
68
        if (empty($plugin_info) || empty($plugin_info['plugin_path'])) {
69
            continue;
70
        }
71
72
        if (!empty($plugin_info['langfile_path'])) {
73
            require_once $plugin_info['langfile_path'];
74
        }
75
        require_once $plugin_info['plugin_path'];
76
77
        // call the plugin
78
        if (function_exists(@$plugin_info['func'])) {
79
            // get the list of waitings
80
            $_tmp = call_user_func($plugin_info['func'], $dirname);
81
            if (\Xmf\Request::hasVar('lang_linkname', 'tmp')) {
82
                if (@$_tmp['pendingnum'] > 0 || $options[0] > 0) {
83
                    $block['modules'][$dirname]['pending'][] = $_tmp;
84
                }
85
                unset($_tmp);
86
            } else {
87
                // Judging the plugin returns multiple items
88
                // if lang_linkname does not exist
89
                foreach ($_tmp as $_one) {
90
                    if (@$_one['pendingnum'] > 0 || $options[0] > 0) {
91
                        $block['modules'][$dirname]['pending'][] = $_one;
92
                    }
93
                }
94
            }
95
        }
96
97
        // for older compatibilities
98
        // Hacked by GIJOE
99
        $i = 0;
100
        while (1) {
101
            $function_name = "b_waiting_{$dirname}_$i";
102
            if (function_exists($function_name)) {
103
                $_tmp = call_user_func($function_name);
104
                ++$i;
105
                if ($_tmp['pendingnum'] > 0 || $options[0] > 0) {
106
                    $block['modules'][$dirname]['pending'][] = $_tmp;
107
                }
108
                unset($_tmp);
109
            } else {
110
                break;
111
            }
112
        }
113
        // End of Hack
114
115
        // if(count($block["modules"][$dirname]) > 0){
116
        if (!empty($block['modules'][$dirname])) {
117
            $block['modules'][$dirname]['name'] = $name;
118
        }
119
    }
120
    //print_r($block);
121
122
    // SQL cache touch (you have to use this cache with block's cache by system)
123
    if (empty($block) && $sql_cache_min > 0) {
124
        $fp = fopen($sql_cache_file, 'wb');
125
        fclose($fp);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, 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

125
        fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
126
    }
127
128
    return $block;
129
}
130
131
/**
132
 * @param $options
133
 * @return string
134
 */
135
function b_waiting_waiting_edit($options)
136
{
137
    $mod_url = XOOPS_URL . '/modules/waiting';
138
139
    $sql_cache_min = empty($options[1]) ? 0 : (int)$options[1];
140
141
    $form = _MB_WAITING_NOWAITING_DISPLAY . ":&nbsp;<input type='radio' name='options[0]' value='1'";
142
    if (1 == $options[0]) {
143
        $form .= ' checked';
144
    }
145
    $form .= '>&nbsp;' . _YES . "<input type='radio' name='options[0]' value='0'";
146
    if (0 == $options[0]) {
147
        $form .= ' checked';
148
    }
149
    $form .= '>&nbsp;' . _NO . "<br>\n";
150
    $form .= sprintf(_MINUTES, _MB_WAITING_SQL_CACHE . ":&nbsp;<input type='text' name='options[1]' value='{$sql_cache_min}' size='2'>");
151
    $form .= "<br>\n<br>\n<a href='{$mod_url}/admin/index.php'><img src='{$mod_url}/assets/images/folder16.gif'>" . _MB_WAITING_LINKTOPLUGINCHECK . '</a>';
152
153
    return $form;
154
}
155