b_wgfilemanager_dirfavlist_show()   B
last analyzed

Complexity

Conditions 10
Paths 120

Size

Total Lines 72
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 50
c 1
b 0
f 0
nc 120
nop 1
dl 0
loc 72
rs 7.0909

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
declare(strict_types=1);
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * wgFileManager module for xoops
17
 *
18
 * @copyright    2021 XOOPS Project (https://xoops.org)
19
 * @license      GPL 2.0 or later
20
 * @package      wgfilemanager
21
 * @author       Goffy - Wedega - Email:[email protected] - Website:https://xoops.wedega.com
22
 */
23
24
//use XoopsModules\Wgfilemanager;
25
use XoopsModules\Wgfilemanager\Helper;
26
//use XoopsModules\Wgfilemanager\Constants;
27
use Xmf\Request;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Request. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
28
29
require_once \XOOPS_ROOT_PATH . '/modules/wgfilemanager/include/common.php';
30
31
/**
32
 * Function show block
33
 * @param  $options
34
 * @return array
35
 */
36
function b_wgfilemanager_dirfavlist_show($options)
37
{
38
    global $xoopsModule;
39
40
    $helper           = Helper::getInstance();
41
    $directoryHandler = $helper->getHandler('Directory');
42
    $fileHandler      = $helper->getHandler('File');
43
44
    $typeBlock  = $options[0];
45
    $lengthName = (int)$options[1];
46
    $typeList   = 0;
47
    if (isset($options[2])) {
48
        $typeList = (int)$options[2];
49
    }
50
    $GLOBALS['xoopsTpl']->assign('wgfilemanager_typeblock', $typeBlock);
51
    $GLOBALS['xoTheme']->addStylesheet(\WGFILEMANAGER_URL . '/assets/css/default.css');
52
53
    $block = [];
54
55
    $dirId = Request::getInt('dir_id', 0);
56
    if ((!empty($xoopsModule))) {
57
        $moduleDirName = \basename(\dirname(__DIR__));
58
/*        echo "<br>xoopsModule:".$moduleDirName. " " . $xoopsModule->getByDirname($moduleDirName)->getVar('mid');
59
        echo "<br>xoopsModule:".$xoopsModule->getVar('mid');*/
60
        if ($xoopsModule->getByDirname($moduleDirName)->getVar('mid') === $xoopsModule->getVar('mid')) {
61
            $dirId = Request::getInt('dir_id', 1);
62
        }
63
    }
64
    $collapseFav = false;
65
    $collapseDir = false;
66
    if ('collapsable' === (string)$options[0]) {
67
        switch ($typeList) {
68
            case 1:
69
                $collapseFav = true;
70
                $collapseDir = false;
71
                break;
72
            case 2:
73
                $collapseFav = false;
74
                $collapseDir = true;
75
                break;
76
            case 0:
77
            default:
78
                $collapseFav = true;
79
                $collapseDir = true;
80
                break;
81
        }
82
    }
83
    $GLOBALS['xoopsTpl']->assign('collapseFav', $collapseFav);
84
    $GLOBALS['xoopsTpl']->assign('collapseDir', $collapseDir);
85
    if (0 === $lengthName) {
86
        $lengthName = 1000;
87
    }
88
    $GLOBALS['xoopsTpl']->assign('lengthName', $lengthName);
89
90
    //get directory list
91
    $block['dir_list'] = $directoryHandler->getDirList(0, $dirId, 1,'weight ASC, id',  'ASC', $lengthName);
92
93
    $block['fav_list'] = [];
94
    if ((bool)$helper->getConfig('use_favorites')) {
95
        //get fav list
96
        $favList = [];
97
        //get directory fav list
98
        $favList['dirs'] = $directoryHandler->getFavDirList($lengthName);
99
        //get directory fav list
100
        $favList['files'] = $fileHandler->getFavFileList($lengthName);
101
        $block['fav_list'] = $favList;
102
        $GLOBALS['xoopsTpl']->assign('countFavlist', count($favList['dirs']) + count($favList['files']));
103
    }
104
105
    $GLOBALS['xoopsTpl']->assign('wgfilemanager_url', \WGFILEMANAGER_URL);
106
    $GLOBALS['xoopsTpl']->assign('wgfilemanager_icon_bi_url', \WGFILEMANAGER_ICONS_URL . '/bootstrap/');
107
    return $block;
108
109
}
110
111
/**
112
 * Function edit block
113
 * @param  $options
114
 * @return string
115
 */
116
function b_wgfilemanager_dirfavlist_edit($options)
117
{
118
    $GLOBALS['xoopsTpl']->assign('wgfilemanager_upload_url', \WGFILEMANAGER_UPLOAD_URL);
119
    $form = "<input type='hidden' name='options[0]' value='".$options[0]."' >";
120
    $form .= \_MB_WGFILEMANAGER_NAME_LENGTH . " : <input type='text' name='options[1]' size='5' maxlength='255' value='" . $options[1] . "' ><br><br>";
121
    if ('collapsable' === (string)$options[0]) {
122
        $form .= \_MB_WGFILEMANAGER_DIRFAV_COL_TYPE . ": <select name='options[2]' size='3'>";
123
        $form .= "<option value='0' " . (0 === (int)$options[2] ? "selected='selected'" : '') . '>' . \_MB_WGFILEMANAGER_DIRFAV_COL_TYPE_0 . '</option>';
124
        $form .= "<option value='1' " . (1 === (int)$options[2] ? "selected='selected'" : '') . '>' . \_MB_WGFILEMANAGER_DIRFAV_COL_TYPE_1 . '</option>';
125
        $form .= "<option value='2' " . (2 === (int)$options[2] ? "selected='selected'" : '') . '>' . \_MB_WGFILEMANAGER_DIRFAV_COL_TYPE_2 . '</option>';
126
        $form .= '</select><br>';
127
    }
128
129
    return $form;
130
131
}
132