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