| Total Complexity | 44 | 
| Total Lines | 235 | 
| 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) | ||
| 111 |     { | ||
| 112 | $filelist = []; | ||
| 113 |         switch (\trim($type)) { | ||
| 114 | case 'images': | ||
| 115 | $types = '[.gif|.jpg|.png]'; | ||
| 116 |                 if ($noselection) { | ||
| 117 | $filelist[''] = _AM_WFL_SHOWNOIMAGE; | ||
| 118 | } | ||
| 119 | break; | ||
| 120 | case 'html': | ||
| 121 | $types = '[.htm|.html|.xhtml|.php|.php3|.phtml|.txt]'; | ||
| 122 |                 if ($noselection) { | ||
| 123 | $filelist[''] = 'No Selection'; | ||
| 124 | } | ||
| 125 | break; | ||
| 126 | default: | ||
| 127 | $types = ''; | ||
| 128 |                 if ($noselection) { | ||
| 129 | $filelist[''] = 'No Selected File'; | ||
| 130 | } | ||
| 131 | break; | ||
| 132 | } | ||
| 133 | |||
| 134 |         if ('/' === mb_substr($dirname, -1)) { | ||
| 135 | $dirname = mb_substr($dirname, 0, -1); | ||
| 136 | } | ||
| 137 | |||
| 138 |         if (\is_dir($dirname) && $handle = \opendir($dirname)) { | ||
| 139 |             while (false !== ($file = \readdir($handle))) { | ||
| 140 |                 if (!\preg_match('/^[.]{1,2}$/', $file) && \preg_match("/$types$/i", $file) | ||
| 141 |                     && \is_file($dirname . '/' . $file)) { | ||
| 142 |                     if ('blank.gif' === mb_strtolower($file)) { | ||
| 143 | continue; | ||
| 144 | } | ||
| 145 | $file = $prefix . $file; | ||
| 146 | $filelist[$file] = $file; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | \closedir($handle); | ||
| 150 | \asort($filelist); | ||
| 151 | \reset($filelist); | ||
| 152 | } | ||
| 153 | |||
| 154 | return $filelist; | ||
| 155 | } | ||
| 156 | |||
| 157 | /** | ||
| 158 | * @param int $type | ||
| 159 | * @param $selected | ||
| 160 | * | ||
| 161 | * @return mixed | ||
| 162 | */ | ||
| 163 | public static function getForum($type, $selected) | ||
| 164 |     { | ||
| 165 | global $xoopsDB; | ||
| 166 |         switch (\xoops_trim($type)) { | ||
| 167 | case 2: | ||
| 168 |                 $sql = 'SELECT id, name FROM ' . $xoopsDB->prefix('ibf_forums') . ' ORDER BY id'; | ||
| 169 | break; | ||
| 170 | case 3: | ||
| 171 |                 $sql = 'SELECT forum_id, forum_name FROM ' . $xoopsDB->prefix('pbb_forums') . ' ORDER BY forum_id'; | ||
| 172 | break; | ||
| 173 | case 4: | ||
| 174 |                 $sql = 'SELECT forum_id, forum_name FROM ' . $xoopsDB->prefix('bbex_forums') . ' ORDER BY forum_id'; | ||
| 175 | break; | ||
| 176 | case 1: | ||
| 177 | case 0: | ||
| 178 | default: | ||
| 179 |                 $sql = 'SELECT forum_id, forum_name FROM ' . $xoopsDB->prefix('newbb_forums') . ' ORDER BY forum_id'; | ||
| 180 | break; | ||
| 181 | } | ||
| 182 | $result = $xoopsDB->query($sql); | ||
| 183 | |||
| 184 |         $noforum = \defined('_WFL_NO_FORUM') ? _WFL_NO_FORUM : _AM_WFL_NO_FORUM; | ||
| 185 | |||
| 186 | echo "<select size='1' name='forumid'>"; | ||
| 187 | echo "<option value='0'>" . $noforum . '</option>'; | ||
| 188 |         while (list($forum_id, $forum_name) = $xoopsDB->fetchRow($result)) { | ||
| 189 | $opt_selected = ''; | ||
| 190 |             if ($forum_id == $selected) { | ||
| 191 | $opt_selected = 'selected'; | ||
| 192 | } | ||
| 193 | echo "<option value='" . $forum_id . "' $opt_selected>" . $forum_name . '</option>'; | ||
| 194 | } | ||
| 195 | echo '</select>'; | ||
| 196 | |||
| 197 | return $noforum; | ||
| 198 | } | ||
| 199 | |||
| 200 | public function getValue() | ||
| 201 |     { | ||
| 202 | return $this->value; | ||
| 203 | } | ||
| 204 | |||
| 205 | public function getSelected() | ||
| 206 |     { | ||
| 207 | return $this->selected; | ||
| 208 | } | ||
| 209 | |||
| 210 | /** | ||
| 211 | * @return string | ||
| 212 | */ | ||
| 213 | public function getPath() | ||
| 214 |     { | ||
| 215 | return $this->path; | ||
| 216 | } | ||
| 217 | |||
| 218 | /** | ||
| 219 | * @return int | ||
| 220 | */ | ||
| 221 | public function getSize() | ||
| 222 |     { | ||
| 223 | return $this->size; | ||
| 224 | } | ||
| 225 | |||
| 226 | /** | ||
| 227 | * @return int | ||
| 228 | */ | ||
| 229 | public function getEmptySelect() | ||
| 230 |     { | ||
| 231 | return $this->emptyselect; | ||
| 232 | } | ||
| 233 | |||
| 234 | /** | ||
| 235 | * @return int | ||
| 236 | */ | ||
| 237 | public function getType() | ||
| 238 |     { | ||
| 239 | return $this->type; | ||
| 240 | } | ||
| 241 | |||
| 242 | public function getPrefix() | ||
| 243 |     { | ||
| 244 | return $this->prefix; | ||
| 245 | } | ||
| 246 | |||
| 247 | public function getSuffix() | ||
| 250 | } | ||
| 251 | } | ||
| 252 |