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