Completed
Branch master (ddc2b8)
by Michael
02:46
created

FileList::setEmptySelect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Module: XoopsTube
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
 * PHP version 5
11
 *
12
 * @category        Module
13
 * @package         Xoopstube
14
 * @author          XOOPS Development Team
15
 * @copyright       2001-2016 XOOPS Project (http://xoops.org)
16
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
17
 * @link            http://xoops.org/
18
 * @since           1.0.6
19
 */
20
class FileList
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...
21
{
22
    public $filelist = array();
23
24
    public $value;
25
    public $selected;
26
    public $path = 'uploads';
27
    public $size;
28
    public $emptySelect;
29
    public $type;
30
    public $prefix;
31
    public $suffix;
32
    public $noSelection;
33
34
    /**
35
     * fileList::construct()
36
     *
37
     * @param string  $path
38
     * @param null    $value
39
     * @param string  $selected
40
     * @param integer $size
41
     *
42
     * @internal param int $emptySelect
43
     * @internal param int $type
44
     * @internal param string $prefix
45
     * @internal param string $suffix
46
     * @return \fileList
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
47
     */
48
    public function __construct($path = 'uploads', $value = null, $selected = '', $size = 1)
49
    {
50
        $this->value     = $value;
51
        $this->selection = $selected;
0 ignored issues
show
Bug introduced by
The property selection does not seem to exist. Did you mean noSelection?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
52
        $this->size      = (int)$size;
53
54
        $pathToCheck = XOOPS_ROOT_PATH . "/{$path}";
55
        if (!is_dir($pathToCheck) && false === @mkdir("$pathToCheck", 0777)) {
56
            XoopsErrorHandler_HandleError(E_USER_WARNING, $pathToCheck . _AM_XOOPSTUBE_DOESNOTEXIST, __FILE__, __LINE__);
57
58
            return false;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
59
        }
60
        $this->path = $path;
61
62
        return true;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
63
    }
64
65
    /**
66
     * SpotList::setNoSelection()
67
     *
68
     * @param integer $value
69
     *
70
     * @return void
71
     */
72
73
    public function setEmptySelect($value = 0)
74
    {
75
        $this->emptySelect = ((int)$value !== 1) ? 0 : 1;
76
    }
77
78
    /**
79
     * @param int $value
80
     */
81
    public function setNoSelection($value = 0)
82
    {
83
        $this->noSelection = ((int)$value !== 1) ? 0 : 1;
84
    }
85
86
    /**
87
     * @param string $value
88
     */
89
    public function setPrefix($value = '')
90
    {
91
        $this->prefix = ((string)$value) !== '' ? (string)$value : '';
92
    }
93
94
    /**
95
     * @param string $value
96
     */
97
    public function setSuffix($value = '')
98
    {
99
        $this->suffix = ((string)$value) !== '' ? (string)$value : '';
100
    }
101
102
    /**
103
     * @param string $value
104
     */
105
    public function setListType($value = 'images')
106
    {
107
        $this->type = (string)strtolower($value);
108
    }
109
110
    /**
111
     * SpotList::showSelection()
112
     *
113
     * @return string
114
     */
115 View Code Duplication
    public function &showSelection()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
116
    {
117
        $ret = "<select size='" . $this->size() . "' name='$this->value()'>";
118
        if ($this->emptySelect) {
119
            $ret .= "<option value='" . $this->value() . "'>----------------------</option>";
120
        }
121
        foreach ($this->filelist as $content) {
122
            $optSelected = '';
123
124
            if ($content[0] == $this->isSelected()) {
125
                $optSelected = "selected='selected'";
126
            }
127
            $ret .= "<option value='" . $content . "' $optSelected>" . $content . '</option>';
128
        }
129
        $ret .= '</select>';
130
131
        return $ret;
132
    }
133
134
    /**
135
     * SpotList::getListTypeAsArray()
136
     *
137
     * @return array
138
     */
139
    public function &getListTypeAsArray()
140
    {
141
        $filelist = array();
0 ignored issues
show
Unused Code introduced by
$filelist is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
142
        switch (trim($this->type)) {
143
            case 'images':
144
                $types = '[.gif|.jpg|.png]';
145
                if ($this->noSelection) {
146
                    $this->filelist[0] = _AM_XOOPSTUBE_NOIMAGE;
147
                }
148
                break;
149
            case 'media':
150
                $types = '[.aac|.flv|.mp3|.mp4|.swf]';
151
                if ($this->noSelection) {
152
                    $this->filelist[0] = _AM_XOOPSTUBE_NOVIDEO;
153
                }
154
                break;
155
156
            case 'html':
157
                $types = '[.htm|.tpl|.html|.xhtml|.php|.php3|.phtml|.txt]';
158
                if ($this->noSelection) {
159
                    $this->filelist[0] = _AM_XOOPSTUBE_NOSELECT;
160
                }
161
                break;
162
163
            default:
164
                $types = '';
165
                if ($this->noSelection) {
166
                    $this->filelist[0] = _AM_XOOPSTUBE_NOFILESELECT;
167
                }
168
                break;
169
        }
170
171
        if ('/' === substr($this->path, -1)) {
172
            $this->path = substr($this->path, 0, -1);
173
        }
174
175
        $_full_path = XOOPS_ROOT_PATH . "/{$this->path}";
176
177
        if (is_dir($_full_path) && $handle = opendir($_full_path)) {
178
            while (false !== ($file = readdir($handle))) {
179
                if (!preg_match("/^[.]{1,2}$/", $file) && preg_match("/$types$/i", $file) && is_file($_full_path . '/' . $file)) {
180
                    if ('blank.gif' === strtolower($file)) {
181
                        continue;
182
                    }
183
                    $file                  = $this->prefix . $file;
184
                    $this->filelist[$file] = $file;
185
                }
186
            }
187
            closedir($handle);
188
            asort($this->filelist);
189
            reset($this->filelist);
190
        }
191
192
        return $this->filelist;
193
    }
194
195
    /**
196
     * @return null
197
     */
198
    public function value()
199
    {
200
        return $this->value;
201
    }
202
203
    public function isSelected()
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...
204
    {
205
        return $this->selected;
206
    }
207
208
    /**
209
     * @return string
210
     */
211
    public function paths()
212
    {
213
        return $this->path;
214
    }
215
216
    /**
217
     * @return int
218
     */
219
    public function size()
220
    {
221
        return $this->size;
222
    }
223
224
    public function isEmptySelect()
225
    {
226
        return $this->emptySelect;
227
    }
228
229
    public function getType()
230
    {
231
        return $this->type;
232
    }
233
234
    public function getPrefix()
235
    {
236
        return $this->prefix;
237
    }
238
239
    public function getSuffix()
240
    {
241
        return $this->suffix;
242
    }
243
}
244