WfsLists::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 8
dl 0
loc 16
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace XoopsModules\Wfdownloads;
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
 * Wfdownloads module
17
 *
18
 * @copyright       XOOPS Project (https://xoops.org)
19
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
20
 * @package         wfdownload
21
 * @since           3.23
22
 * @author          Xoops Development Team
23
 */
24
25
use XoopsModules\Wfdownloads;
26
27
/**
28
 * Class WfsLists
29
 * @package XoopsModules\Wfdownloads
30
 */
31
class WfsLists
32
{
33
    public $value;
34
    public $selected;
35
    public $path = 'uploads';
36
    public $size;
37
    public $emptyselect;
38
    public $type;
39
    public $prefix;
40
    public $suffix;
41
42
    /**
43
     * $value:
44
     * Selection:
45
     * Path:
46
     * Size:
47
     * emptyselect:
48
     * $type: Filter which types of files should be returned
49
     *        Html
50
     *        Images
51
     *        files
52
     *        dir
53
     *
54
     * @param string $path
55
     * @param null   $value
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
56
     * @param string $selected
57
     * @param int    $size
58
     * @param int    $emptyselect
59
     * @param int    $type
60
     * @param string $prefix
61
     * @param string $suffix
62
     */
63
    public function __construct(
64
        $path = 'uploads',
65
        $value = null,
66
        $selected = '',
67
        $size = 1,
68
        $emptyselect = 0,
69
        $type = 0,
70
        $prefix = '',
0 ignored issues
show
Unused Code introduced by
The parameter $prefix is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

70
        /** @scrutinizer ignore-unused */ $prefix = '',

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
        $suffix = ''
0 ignored issues
show
Unused Code introduced by
The parameter $suffix is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

71
        /** @scrutinizer ignore-unused */ $suffix = ''

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
72
    ) {
73
        $this->value       = $value;
74
        $this->selection   = $selected;
0 ignored issues
show
Bug Best Practice introduced by
The property selection does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
75
        $this->path        = $path;
76
        $this->size        = (int)$size;
77
        $this->emptyselect = $emptyselect ? 0 : 1;
78
        $this->type        = $type;
79
    }
80
81
    /**
82
     * @param $this_array
83
     *
84
     * @return string
85
     */
86
    public function getarray($this_array)
87
    {
88
        $ret = "<select size='" . $this->size() . "' name='$this->value()'>";
89
        if ($this->emptyselect) {
90
            $ret .= "<option value='" . $this->value() . "'>----------------------</option>";
91
        }
92
        foreach ($this_array as $content) {
93
            $opt_selected = '';
94
95
            if ($content[0] == $this->isSelected()) {
96
                $opt_selected = 'selected';
97
            }
98
            $ret .= "<option value='" . $content . "' $opt_selected>" . $content . '</option>';
99
        }
100
        $ret .= '</select>';
101
102
        return $ret;
103
    }
104
105
    /**
106
     * Private to be called by other parts of the class
107
     *
108
     * @param $dirname
109
     *
110
     * @return array
111
     */
112
    public function getDirListAsArray($dirname)
113
    {
114
        $dirlist = [];
115
        if (\is_dir($dirname) && $handle = \opendir($dirname)) {
116
            while (false !== ($file = \readdir($handle))) {
117
                if (!\preg_match('/^[.]{1,2}$/', $file)) {
118
                    if ('cvs' !== mb_strtolower($file) && \is_dir($dirname . $file)) {
119
                        $dirlist[$file] = $file;
120
                    }
121
                }
122
            }
123
            \closedir($handle);
124
125
            \reset($dirlist);
126
        }
127
128
        return $dirlist;
129
    }
130
131
    /**
132
     * @param        $dirname
133
     * @param string $type
134
     * @param string $prefix
135
     * @param int    $noselection
136
     *
137
     * @return array
138
     */
139
    public static function getListTypeAsArray($dirname, $type = '', $prefix = '', $noselection = 1)
140
    {
141
        $filelist = [];
142
        switch (\trim($type)) {
143
            case 'images':
144
                $types = '[.gif|.jpg|.png]';
145
                if ($noselection) {
146
                    $filelist[''] = 'Show No Image';
147
                }
148
                break;
149
            case 'html':
150
                $types = '[.htm|.html|.xhtml|.php|.php3|.phtml|.txt|.tpl]';
151
                if ($noselection) {
152
                    $filelist[''] = 'No Selection';
153
                }
154
                break;
155
            default:
156
                $types = '';
157
                if ($noselection) {
158
                    $filelist[''] = 'No Selected File';
159
                }
160
                break;
161
        }
162
163
        if ('/' === mb_substr($dirname, -1)) {
164
            $dirname = mb_substr($dirname, 0, -1);
165
        }
166
167
        if (\is_dir($dirname) && $handle = \opendir($dirname)) {
168
            while (false !== ($file = \readdir($handle))) {
169
                if (!\preg_match('/^[.]{1,2}$/', $file) && \preg_match("/$types$/i", $file)
170
                    && \is_file($dirname . '/' . $file)) {
171
                    if ('blank.png' === mb_strtolower($file)) {
172
                        continue;
173
                    }
174
                    $file            = $prefix . $file;
175
                    $filelist[$file] = $file;
176
                }
177
            }
178
            \closedir($handle);
179
            \asort($filelist);
180
            \reset($filelist);
181
        }
182
183
        return $filelist;
184
    }
185
186
    public function value()
187
    {
188
        return $this->value;
189
    }
190
191
    public function isSelected()
192
    {
193
        return $this->selected;
194
    }
195
196
    /**
197
     * @return string
198
     */
199
    public function paths()
200
    {
201
        return $this->path;
202
    }
203
204
    /**
205
     * @return int
206
     */
207
    public function size()
208
    {
209
        return $this->size;
210
    }
211
212
    /**
213
     * @return int
214
     */
215
    public function emptyselect()
216
    {
217
        return $this->emptyselect;
218
    }
219
220
    /**
221
     * @return int
222
     */
223
    public function type()
224
    {
225
        return $this->type;
226
    }
227
228
    public function getPrefix()
229
    {
230
        return $this->prefix;
231
    }
232
233
    public function getSuffix()
234
    {
235
        return $this->suffix;
236
    }
237
}
238