Completed
Branch master (9ae314)
by Michael
02:19 queued 31s
created

WflLists::getListTypeAsArray()   C

Complexity

Conditions 14
Paths 24

Size

Total Lines 46
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 32
nc 24
nop 4
dl 0
loc 46
rs 5.0744
c 0
b 0
f 0

How to fix   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
/**
4
 * Class: WflLists
5
 *
6
 * Module: WF-Links
7
 * Version: v1.0.3
8
 * Release Date: 21 June 2005
9
 * Developer: John N
10
 * Team: WF-Projects
11
 * Licence: GNU
12
 */
13
class WflLists
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
14
{
15
    public $value;
16
    public $selected;
17
    public $path = 'uploads';
18
    public $size;
19
    public $emptyselect;
20
    public $type;
21
    public $prefix;
22
    public $suffix;
23
24
    /**
25
     * @param string $path
26
     * @param null   $value
27
     * @param string $selected
28
     * @param int    $size
29
     * @param int    $emptyselect
30
     * @param int    $type
31
     * @param string $prefix
32
     * @param string $suffix
33
     */
34
    public function __construct(
35
        $path = 'uploads',
36
        $value = null,
37
        $selected = '',
38
        $size = 1,
39
        $emptyselect = 0,
40
        $type = 0,
41
        $prefix = '',
0 ignored issues
show
Unused Code introduced by
The parameter $prefix is not used and could be removed.

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

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

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

Loading history...
43
    {
44
        $this->value       = $value;
45
        $this->selection   = $selected;
0 ignored issues
show
Bug introduced by
The property selection does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
46
        $this->path        = $path;
47
        $this->size        = (int)$size;
48
        $this->emptyselect = $emptyselect ? 0 : 1;
49
        $this->type        = $type;
50
    }
51
52
    /**
53
     * @param array $this_array
54
     *
55
     * @return string
56
     */
57
    public function getarray($this_array)
58
    {
59
        $ret = "<select size='" . $this->getSize() . "' name='$this->getValue()'>";
0 ignored issues
show
Bug introduced by
The property getValue does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
60
        if ($this->emptyselect) {
61
            $ret .= "<option value='" . $this->getValue() . "'>----------------------</option>";
62
        }
63
        foreach ($this_array as $content) {
64
            $opt_selected = '';
65
66
            if ($content[0] == $this->getSelected()) {
67
                $opt_selected = 'selected';
68
            }
69
            $ret .= "<option value='" . $content . "' $opt_selected>" . $content . '</option>';
70
        }
71
        $ret .= '</select>';
72
73
        return $ret;
74
    }
75
76
    /**
77
     * Private to be called by other parts of the class
78
     * @param $dirname
79
     * @return array
80
     */
81
    public function getDirListAsArray($dirname)
82
    {
83
        $dirlist = array();
84
        if (is_dir($dirname) && $handle = opendir($dirname)) {
85
            while (false !== ($file = readdir($handle))) {
86
                if (!preg_match('/^[.]{1,2}$/', $file)) {
87
                    if (strtolower($file) !== 'cvs' && is_dir($dirname . $file)) {
88
                        $dirlist[$file] = $file;
89
                    }
90
                }
91
            }
92
            closedir($handle);
93
94
            reset($dirlist);
95
        }
96
97
        return $dirlist;
98
    }
99
100
    /**
101
     * @param        $dirname
102
     * @param string $type
103
     * @param string $prefix
104
     * @param int    $noselection
105
     *
106
     * @return array
107
     */
108
    public static function getListTypeAsArray($dirname, $type = '', $prefix = '', $noselection = 1)
109
    {
110
        $filelist = array();
111
        switch (trim($type)) {
112
            case 'images':
113
                $types = '[.gif|.jpg|.png]';
114
                if ($noselection) {
115
                    $filelist[''] = _AM_WFL_SHOWNOIMAGE;
116
                }
117
                break;
118
            case 'html':
119
                $types = '[.htm|.html|.xhtml|.php|.php3|.phtml|.txt]';
120
                if ($noselection) {
121
                    $filelist[''] = 'No Selection';
122
                }
123
                break;
124
            default:
125
                $types = '';
126
                if ($noselection) {
127
                    $filelist[''] = 'No Selected File';
128
                }
129
                break;
130
        }
131
132
        if (substr($dirname, -1) === '/') {
133
            $dirname = substr($dirname, 0, -1);
134
        }
135
136
        if (is_dir($dirname) && $handle = opendir($dirname)) {
137
            while (false !== ($file = readdir($handle))) {
138
                if (!preg_match('/^[.]{1,2}$/', $file) && preg_match("/$types$/i", $file)
139
                    && is_file($dirname . '/' . $file)) {
140
                    if (strtolower($file) === 'blank.gif') {
141
                        continue;
142
                    }
143
                    $file            = $prefix . $file;
144
                    $filelist[$file] = $file;
145
                }
146
            }
147
            closedir($handle);
148
            asort($filelist);
149
            reset($filelist);
150
        }
151
152
        return $filelist;
153
    }
154
155
    /**
156
     * @param int $type
157
     * @param     $selected
158
     *
159
     * @return mixed
160
     */
161
    public static function getForum($type = 1, $selected)
162
    {
163
        global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
164
        switch (xoops_trim($type)) {
165
            case 2:
166
                $sql = 'SELECT id, name FROM ' . $xoopsDB->prefix('ibf_forums') . ' ORDER BY id';
167
                break;
168
            case 3:
169
                $sql = 'SELECT forum_id, forum_name FROM ' . $xoopsDB->prefix('pbb_forums') . ' ORDER BY forum_id';
170
                break;
171
            case 4:
172
                $sql = 'SELECT forum_id, forum_name FROM ' . $xoopsDB->prefix('bbex_forums') . ' ORDER BY forum_id';
173
                break;
174
            case 1:
175
            case 0:
176
            default:
177
                $sql = 'SELECT forum_id, forum_name FROM ' . $xoopsDB->prefix('newbb_forums') . ' ORDER BY forum_id';
178
                break;
179
        }
180
        $result = $xoopsDB->query($sql);
181
182
        $noforum = defined('_WFL_NO_FORUM') ? _WFL_NO_FORUM : _AM_WFL_NO_FORUM;
183
184
        echo "<select size='1' name='forumid'>";
185
        echo "<option value='0'>" . $noforum . '</option>';
186 View Code Duplication
        while (list($forum_id, $forum_name) = $xoopsDB->fetchRow($result)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
            $opt_selected = '';
188
            if ($forum_id == $selected) {
189
                $opt_selected = 'selected';
190
            }
191
            echo "<option value='" . $forum_id . "' $opt_selected>" . $forum_name . '</option>';
192
        }
193
        echo '</select>';
194
195
        return $forum_array;
0 ignored issues
show
Bug introduced by
The variable $forum_array does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
196
    }
197
198
    /**
199
     * @return null
200
     */
201
    public function getValue()
202
    {
203
        return $this->value;
204
    }
205
206
    public function getSelected()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
207
    {
208
        return $this->selected;
209
    }
210
211
    /**
212
     * @return string
213
     */
214
    public function getPath()
215
    {
216
        return $this->path;
217
    }
218
219
    /**
220
     * @return int
221
     */
222
    public function getSize()
223
    {
224
        return $this->size;
225
    }
226
227
    /**
228
     * @return int
229
     */
230
    public function getEmptySelect()
231
    {
232
        return $this->emptyselect;
233
    }
234
235
    /**
236
     * @return int
237
     */
238
    public function getType()
239
    {
240
        return $this->type;
241
    }
242
243
    public function getPrefix()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
244
    {
245
        return $this->prefix;
246
    }
247
248
    public function getSuffix()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
249
    {
250
        return $this->suffix;
251
    }
252
}
253