Passed
Pull Request — master (#19)
by Michael
02:30
created

Lists::__construct()   A

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\Xoopstube;
4
5
/**
6
 * Module: XoopsTube
7
 *
8
 * You may not change or alter any portion of this comment or credits
9
 * of supporting developers from this source code or any supporting source code
10
 * which is considered copyrighted (c) material of the original comment or credit authors.
11
 *
12
 * PHP version 5
13
 *
14
 * @category        Module
15
 * @package         Xoopstube
16
 * @author          XOOPS Development Team
17
 * @copyright       2001-2016 XOOPS Project (https://xoops.org)
18
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
19
 * @link            https://xoops.org/
20
 * @since           1.0.6
21
 */
22
23
use XoopsModules\Xoopstube;
24
25
/**
26
 * Class Lists
27
 * @package XoopsModules\Xoopstube
28
 */
29
class Lists
30
{
31
    public $value;
32
    public $selected;
33
    public $path = 'uploads';
34
    public $size;
35
    public $emptyselect;
36
    public $type;
37
    public $prefix;
38
    public $suffix;
39
40
    /**
41
     * @param string $path
42
     * @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...
43
     * @param string $selected
44
     * @param int    $size
45
     * @param int    $emptyselect
46
     * @param int    $type
47
     * @param string $prefix
48
     * @param string $suffix
49
     */
50
    public function __construct(
51
        $path = 'uploads',
52
        $value = null,
53
        $selected = '',
54
        $size = 1,
55
        $emptyselect = 0,
56
        $type = 0,
57
        $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

57
        /** @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...
58
        $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

58
        /** @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...
59
    ) {
60
        $this->value       = $value;
61
        $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...
62
        $this->path        = $path;
63
        $this->size        = (int)$size;
64
        $this->emptyselect = $emptyselect ? 0 : 1;
65
        $this->type        = $type;
66
    }
67
68
    /**
69
     * @param array $this_array
70
     *
71
     * @return string
72
     */
73
    public function getarray($this_array)
74
    {
75
        $ret = "<select size='" . $this->size() . "' name='$this->value()'>";
76
        if ($this->emptyselect) {
77
            $ret .= "<option value='" . $this->value() . "'>----------------------</option>";
78
        }
79
        foreach ($this_array as $content) {
80
            $opt_selected = '';
81
82
            if ($content[0] == $this->isSelected()) {
83
                $opt_selected = "selected='selected'";
84
            }
85
            $ret .= "<option value='" . $content . "' $opt_selected>" . $content . '</option>';
86
        }
87
        $ret .= '</select>';
88
89
        return $ret;
90
    }
91
92
    /**
93
     * Private to be called by other parts of the class
94
     *
95
     * @param $dirname
96
     *
97
     * @return array
98
     */
99
    public function getDirListAsArray($dirname)
100
    {
101
        $dirlist = [];
102
        if (\is_dir($dirname) && $handle = \opendir($dirname)) {
103
            while (false !== ($file = \readdir($handle))) {
104
                if (!\preg_match('/^[.]{1,2}$/', $file)) {
105
                    if ('cvs' !== mb_strtolower($file) && \is_dir($dirname . $file)) {
106
                        $dirlist[$file] = $file;
107
                    }
108
                }
109
            }
110
            \closedir($handle);
111
112
            \reset($dirlist);
113
        }
114
115
        return $dirlist;
116
    }
117
118
    /**
119
     * @param        $dirname
120
     * @param string $type
121
     * @param string $prefix
122
     * @param int    $noselection
123
     *
124
     * @return array
125
     */
126
    public static function getListTypeAsArray($dirname, $type = '', $prefix = '', $noselection = 1)
127
    {
128
        $filelist = [];
129
        switch (\trim($type)) {
130
            case 'images':
131
                $types = '[.gif|.jpg|.png]';
132
                if ($noselection) {
133
                    $filelist[''] = \_AM_XOOPSTUBE_NOIMAGE;
134
                }
135
                break;
136
            case 'media':
137
                $types = '[.aac|.flv|.mp3|.mp4|.swf]';
138
                if ($noselection) {
139
                    $filelist[''] = \_AM_XOOPSTUBE_NOVIDEO;
140
                }
141
                break;
142
            case 'html':
143
                $types = '[.htm|.tpl|.html|.xhtml|.php|.php3|.phtml|.txt]';
144
                if ($noselection) {
145
                    $filelist[''] = \_AM_XOOPSTUBE_NOSELECT;
146
                }
147
                break;
148
            default:
149
                $types = '';
150
                if ($noselection) {
151
                    $filelist[''] = \_AM_XOOPSTUBE_NOFILESELECT;
152
                }
153
                break;
154
        }
155
156
        if ('/' === mb_substr($dirname, -1)) {
157
            $dirname = mb_substr($dirname, 0, -1);
158
        }
159
160
        if (\is_dir($dirname) && $handle = \opendir($dirname)) {
161
            while (false !== ($file = \readdir($handle))) {
162
                if (!\preg_match('/^[.]{1,2}$/', $file) && \preg_match("/$types$/i", $file) && \is_file($dirname . '/' . $file)) {
163
                    if ('blank.gif' === mb_strtolower($file)) {
164
                        continue;
165
                    }
166
                    $file            = $prefix . $file;
167
                    $filelist[$file] = $file;
168
                }
169
            }
170
            \closedir($handle);
171
            \asort($filelist);
172
            \reset($filelist);
173
        }
174
175
        return $filelist;
176
    }
177
178
    public function value()
179
    {
180
        return $this->value;
181
    }
182
183
    public function isSelected()
184
    {
185
        return $this->selected;
186
    }
187
188
    /**
189
     * @return string
190
     */
191
    public function paths()
192
    {
193
        return $this->path;
194
    }
195
196
    /**
197
     * @return int
198
     */
199
    public function size()
200
    {
201
        return $this->size;
202
    }
203
204
    /**
205
     * @return int
206
     */
207
    public function isEmptySelect()
208
    {
209
        return $this->emptyselect;
210
    }
211
212
    /**
213
     * @return int
214
     */
215
    public function getType()
216
    {
217
        return $this->type;
218
    }
219
220
    public function getPrefix()
221
    {
222
        return $this->prefix;
223
    }
224
225
    public function getSuffix()
226
    {
227
        return $this->suffix;
228
    }
229
}
230