Total Complexity | 44 |
Total Lines | 238 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Lists often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Lists, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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 |
||
|
|||
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 = '', |
||
44 | $suffix = '' |
||
45 | ) { |
||
46 | $this->value = $value; |
||
47 | $this->selection = $selected; |
||
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() . "'>"; |
||
62 | if ($this->emptyselect) { |
||
63 | $ret .= "<option value='" . $this->getValue() . "'>----------------------</option>"; |
||
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) |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @param int $type |
||
159 | * @param $selected |
||
160 | * |
||
161 | * @return mixed |
||
162 | */ |
||
163 | public static function getForum($type, $selected) |
||
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() |
||
253 | } |
||
254 | } |
||
255 |