Passed
Push — master ( 22887c...b0a7cb )
by Michael
04:05 queued 02:12
created

xlanguage_blocks.php ➔ b_xlanguage_select_show()   C

Complexity

Conditions 16
Paths 81

Size

Total Lines 62
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 43
nc 81
nop 1
dl 0
loc 62
rs 6.1338
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
 * xLanguage module (eXtensible Language Management For XOOPS)
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
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
 * @copyright    XOOPS Project (https://xoops.org)
13
 * @license      {@link http://www.gnu.org/licenses/gpl-2.0.html GNU Public License}
14
 * @package      xlanguage
15
 * @since        2.0
16
 * @author       D.J.(phppp) [email protected]
17
 * @param $options
18
 * @return array
19
 */
20
function b_xlanguage_select_show($options)
21
{
22
    global $xlanguage;
23
24
    $block = [];
25
26
    /** @var \XoopsModules\Xlanguage\Helper $helper */
27
    $helper = \XoopsModules\Xlanguage\Helper::getInstance();
28
29
    /** @var \XoopsModules\Xlanguage\LanguageHandler $xlanguageHandler */
30
    $xlanguageHandler = $helper->getHandler('Language');
31
    $xlanguageHandler->loadConfig();
32
    $lang_list = $xlanguageHandler->getAllList();
33
    if (!is_array($lang_list) || (count($lang_list) < 1)) {
0 ignored issues
show
introduced by
The condition is_array($lang_list) is always true.
Loading history...
34
        return $block;
35
    }
36
37
    $languages = [];
38
    foreach ($lang_list as $lang_name => $lang) {
39
        if (!isset($lang['base'])) {
40
            continue;
41
        }
42
        $languages[$lang_name]['name']  = $lang_name;
43
        $languages[$lang_name]['desc']  = $lang['base']->getVar('lang_desc');
44
        $languages[$lang_name]['image'] = XOOPS_URL . '/modules/xlanguage/assets/images/' . $lang['base']->getVar('lang_image');
45
        if (!isset($lang['ext']) || count($lang['ext']) < 1) {
46
            continue;
47
        }
48
        foreach ($lang['ext'] as $ext) {
49
            $langName = $ext->getVar('lang_name');
50
            $languages[$langName]['name']  = $langName;
51
            $languages[$langName]['desc']  = $ext->getVar('lang_desc');
52
            $languages[$langName]['image'] = XOOPS_URL . '/modules/xlanguage/assets/images/' . $ext->getVar('lang_image');
53
        }
54
    }
55
56
    $QUERY_STRING_array = array_filter(explode('&', xoops_getenv('QUERY_STRING')));
57
    $QUERY_STRING_new   = [];
58
    foreach ($QUERY_STRING_array as $QUERY) {
59
        if (0 !== mb_strpos($QUERY, XLANGUAGE_LANG_TAG . '=')) {
60
            $vals = explode('=', $QUERY);
61
            foreach (array_keys($vals) as $key) {
62
                if (preg_match('/^a-z0-9$/i', $vals[$key])) {
63
                    $vals[$key] = urlencode($vals[$key]);
64
                }
65
            }
66
            $QUERY_STRING_new[] = implode('=', $vals);
67
        }
68
    }
69
70
    $block['display']   = $options[0];
71
    $block['delimitor'] = $options[1];
72
    $block['number']    = $options[2];
73
    $block['selected']  = $xlanguage['lang'];
74
    if ('images' === $options[0] || 'text' === $options[0]) {
75
        $query_string = htmlspecialchars(implode('&', $QUERY_STRING_new), ENT_QUOTES | ENT_HTML5);
76
        $query_string .= empty($query_string) ? '' : '&amp;';
77
    } else {
78
        $query_string = implode('&', array_map('htmlspecialchars', $QUERY_STRING_new));
79
        $query_string .= empty($query_string) ? '' : '&';
80
    }
81
    $block['url']       = xoops_getenv('SCRIPT_NAME') . '?' . $query_string . XLANGUAGE_LANG_TAG . '=';
82
    $block['languages'] = &$languages;
83
84
    return $block;
85
}
86
87
/**
88
 * @param $options
89
 * @return string
90
 */
91
function b_xlanguage_select_edit($options)
92
{
93
    $form = _MB_XLANGUAGE_DISPLAY_METHOD . "&nbsp;<select name='options[]'>";
94
    $form .= "<option value='images'";
95
    if ('images' === $options[0]) {
96
        $form .= ' selected';
97
    }
98
    $form .= '>' . _MB_XLANGUAGE_DISPLAY_FLAGLIST . "</option>\n";
99
    $form .= "<option value='text'";
100
    if ('text' === $options[0]) {
101
        $form .= ' selected';
102
    }
103
    $form .= '>' . _MB_XLANGUAGE_DISPLAY_TEXTLIST . "</option>\n";
104
    $form .= "<option value='dropdown'";
105
    if ('dropdown' === $options[0]) {
106
        $form .= ' selected';
107
    }
108
    $form .= '>' . _MB_XLANGUAGE_DISPLAY_DROPDOWNLIST . "</option>\n";
109
    $form .= "</select>\n";
110
    $form .= '<br>' . _MB_XLANGUAGE_IMAGE_SEPARATOR . ' (' . _MB_XLANGUAGE_OPTIONAL . "):&nbsp;<input type='text' name='options[]' value='" . $options[1] . "'>";
111
    $form .= '<br>' . _MB_XLANGUAGE_IMAGE_PERROW . ' (' . _MB_XLANGUAGE_OPTIONAL . "):&nbsp;<input type='text' name='options[]' value='" . $options[2] . "'>";
112
113
    return $form;
114
}
115