Passed
Push — master ( a734e1...3b98a6 )
by Michael
01:55
created

b_waiting_waiting_show()   C

Complexity

Conditions 16
Paths 146

Size

Total Lines 103
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

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

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