Lists::__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\Wflinks;
4
5
/**
6
 * Class: Lists
7
 *
8
 * Module: WF-Links
9
 * Version: v1.0.3
10
 * Release Date: 21 June 2005
11
 * Developer: John N
12
 * Team: WF-Projects
13
 * Licence: GNU
14
 */
15
class Lists
16
{
17
    public $value;
18
    public $selected;
19
    public $path = 'uploads';
20
    public $size;
21
    public $emptyselect;
22
    public $type;
23
    public $prefix;
24
    public $suffix;
25
26
    /**
27
     * @param string $path
28
     * @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...
29
     * @param string $selected
30
     * @param int    $size
31
     * @param int    $emptyselect
32
     * @param int    $type
33
     * @param string $prefix
34
     * @param string $suffix
35
     */
36
    public function __construct(
37
        $path = 'uploads',
38
        $value = null,
39
        $selected = '',
40
        $size = 1,
41
        $emptyselect = 0,
42
        $type = 0,
43
        $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

43
        /** @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...
44
        $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

44
        /** @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...
45
    ) {
46
        $this->value       = $value;
47
        $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...
48
        $this->path        = $path;
49
        $this->size        = (int)$size;
50
        $this->emptyselect = $emptyselect ? 0 : 1;
51
        $this->type        = $type;
52
    }
53
54
    /**
55
     * @param array $this_array
56
     *
57
     * @return string
58
     */
59
    public function getArray($this_array)
60
    {
61
        $ret = "<select size='" . $this->getSize() . "' name='" . $this->getValue() . "'>";
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getValue() targeting XoopsModules\Wflinks\Lists::getValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
62
        if ($this->emptyselect) {
63
            $ret .= "<option value='" . $this->getValue() . "'>----------------------</option>";
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getValue() targeting XoopsModules\Wflinks\Lists::getValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
64
        }
65
        foreach ($this_array as $content) {
66
            $opt_selected = '';
67
68
            if ($content[0] == $this->getSelected()) {
69
                $opt_selected = 'selected';
70
            }
71
            $ret .= "<option value='" . $content . "' $opt_selected>" . $content . '</option>';
72
        }
73
        $ret .= '</select>';
74
75
        return $ret;
76
    }
77
78
    /**
79
     * Private to be called by other parts of the class
80
     * @param $dirname
81
     * @return array
82
     */
83
    public function getDirListAsArray($dirname)
84
    {
85
        $dirlist = [];
86
        if (\is_dir($dirname) && $handle = \opendir($dirname)) {
87
            while (false !== ($file = \readdir($handle))) {
88
                if (!\preg_match('/^[.]{1,2}$/', $file)) {
89
                    if ('cvs' !== mb_strtolower($file) && \is_dir($dirname . $file)) {
90
                        $dirlist[$file] = $file;
91
                    }
92
                }
93
            }
94
            \closedir($handle);
95
96
            \reset($dirlist);
97
        }
98
99
        return $dirlist;
100
    }
101
102
    /**
103
     * @param        $dirname
104
     * @param string $type
105
     * @param string $prefix
106
     * @param int    $noselection
107
     *
108
     * @return array
109
     */
110
    public static function getListTypeAsArray($dirname, $type = '', $prefix = '', $noselection = 1)
111
    {
112
        $filelist = [];
113
        switch (\trim($type)) {
114
            case 'images':
115
                $types = '[.gif|.jpg|.png]';
116
                if ($noselection) {
117
                    $filelist[''] = _AM_WFL_SHOWNOIMAGE;
118
                }
119
                break;
120
            case 'html':
121
                $types = '[.htm|.html|.xhtml|.php|.php3|.phtml|.txt]';
122
                if ($noselection) {
123
                    $filelist[''] = 'No Selection';
124
                }
125
                break;
126
            default:
127
                $types = '';
128
                if ($noselection) {
129
                    $filelist[''] = 'No Selected File';
130
                }
131
                break;
132
        }
133
134
        if ('/' === mb_substr($dirname, -1)) {
135
            $dirname = mb_substr($dirname, 0, -1);
136
        }
137
138
        if (\is_dir($dirname) && $handle = \opendir($dirname)) {
139
            while (false !== ($file = \readdir($handle))) {
140
                if (!\preg_match('/^[.]{1,2}$/', $file) && \preg_match("/$types$/i", $file)
141
                    && \is_file($dirname . '/' . $file)) {
142
                    if ('blank.gif' === mb_strtolower($file)) {
143
                        continue;
144
                    }
145
                    $file            = $prefix . $file;
146
                    $filelist[$file] = $file;
147
                }
148
            }
149
            \closedir($handle);
150
            \asort($filelist);
151
            \reset($filelist);
152
        }
153
154
        return $filelist;
155
    }
156
157
    /**
158
     * @param int $type
159
     * @param     $selected
160
     *
161
     * @return mixed
162
     */
163
    public static function getForum($type, $selected)
164
    {
165
        global $xoopsDB;
166
        switch (\xoops_trim($type)) {
167
            case 2:
168
                $sql = 'SELECT id, name FROM ' . $xoopsDB->prefix('ibf_forums') . ' ORDER BY id';
169
                break;
170
            case 3:
171
                $sql = 'SELECT forum_id, forum_name FROM ' . $xoopsDB->prefix('pbb_forums') . ' ORDER BY forum_id';
172
                break;
173
            case 4:
174
                $sql = 'SELECT forum_id, forum_name FROM ' . $xoopsDB->prefix('bbex_forums') . ' ORDER BY forum_id';
175
                break;
176
            case 1:
177
            case 0:
178
            default:
179
                $sql = 'SELECT forum_id, forum_name FROM ' . $xoopsDB->prefix('newbb_forums') . ' ORDER BY forum_id';
180
                break;
181
        }
182
        $result = $xoopsDB->query($sql);
183
184
        $noforum = \defined('_WFL_NO_FORUM') ? _WFL_NO_FORUM : _AM_WFL_NO_FORUM;
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Wflinks\_WFL_NO_FORUM was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
185
186
        echo "<select size='1' name='forumid'>";
187
        echo "<option value='0'>" . $noforum . '</option>';
188
        while (list($forum_id, $forum_name) = $xoopsDB->fetchRow($result)) {
189
            $opt_selected = '';
190
            if ($forum_id == $selected) {
191
                $opt_selected = 'selected';
192
            }
193
            echo "<option value='" . $forum_id . "' $opt_selected>" . $forum_name . '</option>';
194
        }
195
        echo '</select>';
196
197
        return $noforum;
198
    }
199
200
    /**
201
     * @return null
202
     */
203
    public function getValue()
204
    {
205
        return $this->value;
206
    }
207
208
    public function getSelected()
209
    {
210
        return $this->selected;
211
    }
212
213
    /**
214
     * @return string
215
     */
216
    public function getPath()
217
    {
218
        return $this->path;
219
    }
220
221
    /**
222
     * @return int
223
     */
224
    public function getSize()
225
    {
226
        return $this->size;
227
    }
228
229
    /**
230
     * @return int
231
     */
232
    public function getEmptySelect()
233
    {
234
        return $this->emptyselect;
235
    }
236
237
    /**
238
     * @return int
239
     */
240
    public function getType()
241
    {
242
        return $this->type;
243
    }
244
245
    public function getPrefix()
246
    {
247
        return $this->prefix;
248
    }
249
250
    public function getSuffix()
251
    {
252
        return $this->suffix;
253
    }
254
}
255