Total Complexity | 41 |
Total Lines | 217 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like FileList 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 FileList, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class FileList |
||
30 | { |
||
31 | public $filelist = []; |
||
32 | public $value; |
||
33 | public $selected; |
||
34 | public $path = 'uploads'; |
||
35 | public $size; |
||
36 | public $emptySelect; |
||
37 | public $type; |
||
38 | public $prefix; |
||
39 | public $suffix; |
||
40 | public $noSelection; |
||
41 | |||
42 | /** |
||
43 | * fileList::construct() |
||
44 | * |
||
45 | * @param string $path |
||
46 | * @param null $value |
||
|
|||
47 | * @param string $selected |
||
48 | * @param int $size |
||
49 | * |
||
50 | * @internal param int $emptySelect |
||
51 | * @internal param int $type |
||
52 | * @internal param string $prefix |
||
53 | * @internal param string $suffix |
||
54 | */ |
||
55 | public function __construct($path = 'uploads', $value = null, $selected = '', $size = 1) |
||
56 | { |
||
57 | $this->value = $value; |
||
58 | $this->selected = $selected; |
||
59 | $this->size = (int)$size; |
||
60 | |||
61 | $pathToCheck = XOOPS_ROOT_PATH . "/{$path}"; |
||
62 | if (!\is_dir($pathToCheck) && !\mkdir($pathToCheck, 0777) && !\is_dir($pathToCheck)) { |
||
63 | /** @var \XoopsLogger $logger */ |
||
64 | $logger = \XoopsLogger::getInstance(); |
||
65 | $logger->handleError(\E_USER_WARNING, $pathToCheck . \_AM_XOOPSTUBE_DOESNOTEXIST, __FILE__, __LINE__); |
||
66 | |||
67 | return false; |
||
68 | } |
||
69 | $this->path = $path; |
||
70 | |||
71 | return true; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * SpotList::setNoSelection() |
||
76 | * |
||
77 | * @param int $value |
||
78 | */ |
||
79 | public function setEmptySelect($value = 0) |
||
80 | { |
||
81 | $this->emptySelect = (1 !== (int)$value) ? 0 : 1; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param int $value |
||
86 | */ |
||
87 | public function setNoSelection($value = 0) |
||
88 | { |
||
89 | $this->noSelection = (1 !== (int)$value) ? 0 : 1; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param string $value |
||
94 | */ |
||
95 | public function setPrefix($value = '') |
||
96 | { |
||
97 | $this->prefix = '' !== ((string)$value) ? (string)$value : ''; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param string $value |
||
102 | */ |
||
103 | public function setSuffix($value = '') |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param string $value |
||
110 | */ |
||
111 | public function setListType($value = 'images') |
||
112 | { |
||
113 | $this->type = mb_strtolower($value); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * SpotList::showSelection() |
||
118 | * |
||
119 | * @return string |
||
120 | */ |
||
121 | public function &showSelection() |
||
122 | { |
||
123 | $ret = "<select size='" . $this->size() . "' name='$this->value()'>"; |
||
124 | if ($this->emptySelect) { |
||
125 | $ret .= "<option value='" . $this->value() . "'>----------------------</option>"; |
||
126 | } |
||
127 | foreach ($this->filelist as $content) { |
||
128 | $optSelected = ''; |
||
129 | |||
130 | if ($content[0] == $this->isSelected()) { |
||
131 | $optSelected = "selected='selected'"; |
||
132 | } |
||
133 | $ret .= "<option value='" . $content . "' $optSelected>" . $content . '</option>'; |
||
134 | } |
||
135 | $ret .= '</select>'; |
||
136 | |||
137 | return $ret; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * SpotList::getListTypeAsArray() |
||
142 | * |
||
143 | * @return array |
||
144 | */ |
||
145 | public function &getListTypeAsArray() |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @return null |
||
201 | */ |
||
202 | public function value() |
||
203 | { |
||
205 | } |
||
206 | |||
207 | public function isSelected() |
||
208 | { |
||
209 | return $this->selected; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * @return string |
||
214 | */ |
||
215 | public function paths() |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @return int |
||
222 | */ |
||
223 | public function size() |
||
224 | { |
||
225 | return $this->size; |
||
226 | } |
||
227 | |||
228 | public function isEmptySelect() |
||
229 | { |
||
230 | return $this->emptySelect; |
||
231 | } |
||
232 | |||
233 | public function getType() |
||
234 | { |
||
235 | return $this->type; |
||
236 | } |
||
237 | |||
238 | public function getPrefix() |
||
239 | { |
||
240 | return $this->prefix; |
||
241 | } |
||
242 | |||
243 | public function getSuffix() |
||
246 | } |
||
247 | } |
||
248 |