| Conditions | 35 |
| Paths | 125 |
| Total Lines | 197 |
| Code Lines | 151 |
| Lines | 112 |
| Ratio | 56.85 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 34 | function createConfigform($config) |
||
| 35 | { |
||
| 36 | $xoops = Xoops::getInstance(); |
||
| 37 | $config_handler = $xoops->getHandlerConfig(); |
||
| 38 | //$xoops->config = $config_handler->getConfigsByCat(XOOPS_CONF); |
||
| 39 | //$config =& $xoops->config; |
||
| 40 | |||
| 41 | $ret = array(); |
||
| 42 | $confcount = count($config); |
||
| 43 | |||
| 44 | for ($i = 0; $i < $confcount; ++$i) { |
||
| 45 | $conf_catid = $config[$i]->getVar('conf_catid'); |
||
| 46 | if (!isset($ret[$conf_catid])) { |
||
| 47 | $ret[$conf_catid] = new Xoops\Form\ThemeForm('', 'configs', 'index.php', 'post'); |
||
| 48 | } |
||
| 49 | |||
| 50 | $title = \Xoops\Locale::translate($config[$i]->getVar('conf_title'), 'system'); |
||
| 51 | |||
| 52 | switch ($config[$i]->getVar('conf_formtype')) { |
||
| 53 | |||
| 54 | case 'textarea': |
||
| 55 | if ($config[$i]->getVar('conf_valuetype') === 'array') { |
||
| 56 | // this is exceptional.. only when value type is array need a smarter way for this |
||
| 57 | $ele = ($config[$i]->getVar('conf_value') != '') |
||
| 58 | ? new Xoops\Form\TextArea($title, $config[$i]->getVar('conf_name'), installHtmlSpecialCharacters(implode('|', $config[$i]->getConfValueForOutput())), 5, 50) |
||
| 59 | : new Xoops\Form\TextArea($title, $config[$i]->getVar('conf_name'), '', 5, 50); |
||
| 60 | } else { |
||
| 61 | $ele = new Xoops\Form\TextArea($title, $config[$i]->getVar('conf_name'), installHtmlSpecialCharacters($config[$i]->getConfValueForOutput()), 5, 100); |
||
| 62 | } |
||
| 63 | break; |
||
| 64 | |||
| 65 | View Code Duplication | case 'select': |
|
| 66 | $ele = new Xoops\Form\Select($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput()); |
||
| 67 | $options =& $config_handler->getConfigOptions(new Criteria('conf_id', $config[$i]->getVar('conf_id'))); |
||
| 68 | $opcount = count($options); |
||
| 69 | for ($j = 0; $j < $opcount; ++$j) { |
||
| 70 | $optval = \Xoops\Locale::translate($options[$j]->getVar('confop_value'), 'system'); |
||
| 71 | $optkey = \Xoops\Locale::translate($options[$j]->getVar('confop_name'), 'system'); |
||
| 72 | $ele->addOption($optval, $optkey); |
||
| 73 | } |
||
| 74 | break; |
||
| 75 | |||
| 76 | View Code Duplication | case 'select_multi': |
|
| 77 | $ele = new Xoops\Form\Select($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput(), 5, true); |
||
| 78 | $options =& $config_handler->getConfigOptions(new Criteria('conf_id', $config[$i]->getVar('conf_id'))); |
||
| 79 | $opcount = count($options); |
||
| 80 | for ($j = 0; $j < $opcount; ++$j) { |
||
| 81 | $optval = \Xoops\Locale::translate($options[$j]->getVar('confop_value'), 'system'); |
||
| 82 | $optkey = \Xoops\Locale::translate($options[$j]->getVar('confop_name'), 'system'); |
||
| 83 | $ele->addOption($optval, $optkey); |
||
| 84 | } |
||
| 85 | break; |
||
| 86 | |||
| 87 | View Code Duplication | case 'yesno': |
|
| 88 | $ele = new Xoops\Form\RadioYesNo($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput(), XoopsLocale::YES, XoopsLocale::NO); |
||
| 89 | break; |
||
| 90 | |||
| 91 | case 'theme': |
||
| 92 | View Code Duplication | case 'theme_multi': |
|
| 93 | $ele = ($config[$i]->getVar('conf_formtype') !== 'theme_multi') |
||
| 94 | ? new Xoops\Form\Select($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput()) |
||
| 95 | : new Xoops\Form\Select($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput(), 5, true); |
||
| 96 | $dirlist = XoopsLists::getThemesList(); |
||
| 97 | if (!empty($dirlist)) { |
||
| 98 | asort($dirlist); |
||
| 99 | $ele->addOptionArray($dirlist); |
||
| 100 | } |
||
| 101 | break; |
||
| 102 | |||
| 103 | View Code Duplication | case 'tplset': |
|
| 104 | $ele = new Xoops\Form\Select($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput()); |
||
| 105 | $tplset_handler = $xoops->getHandlerTplSet(); |
||
| 106 | $tplsetlist = $tplset_handler->getNameList(); |
||
| 107 | asort($tplsetlist); |
||
| 108 | foreach ($tplsetlist as $key => $name) { |
||
| 109 | $ele->addOption($key, $name); |
||
| 110 | } |
||
| 111 | break; |
||
| 112 | |||
| 113 | View Code Duplication | case 'timezone': |
|
| 114 | $ele = new Xoops\Form\SelectTimeZone($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput()); |
||
| 115 | break; |
||
| 116 | |||
| 117 | View Code Duplication | case 'language': |
|
| 118 | $ele = new Xoops\Form\SelectLanguage($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput()); |
||
| 119 | break; |
||
| 120 | |||
| 121 | View Code Duplication | case 'locale': |
|
| 122 | $ele = new Xoops\Form\SelectLocale($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput()); |
||
| 123 | break; |
||
| 124 | |||
| 125 | View Code Duplication | case 'startpage': |
|
| 126 | $ele = new Xoops\Form\Select($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput()); |
||
| 127 | $module_handler = $xoops->getHandlerModule(); |
||
| 128 | $criteria = new CriteriaCompo(new Criteria('hasmain', 1)); |
||
| 129 | $criteria->add(new Criteria('isactive', 1)); |
||
| 130 | $moduleslist =& $module_handler->getNameList($criteria, true); |
||
| 131 | $moduleslist['--'] = XoopsLocale::NONE; |
||
| 132 | $ele->addOptionArray($moduleslist); |
||
| 133 | break; |
||
| 134 | |||
| 135 | case 'group': |
||
| 136 | $ele = new Xoops\Form\SelectGroup($title, $config[$i]->getVar('conf_name'), false, $config[$i]->getConfValueForOutput(), 1, false); |
||
| 137 | break; |
||
| 138 | |||
| 139 | case 'group_multi': |
||
| 140 | $ele = new Xoops\Form\SelectGroup($title, $config[$i]->getVar('conf_name'), false, $config[$i]->getConfValueForOutput(), 5, true); |
||
| 141 | break; |
||
| 142 | |||
| 143 | // RMV-NOTIFY - added 'user' and 'user_multi' |
||
| 144 | View Code Duplication | case 'user': |
|
| 145 | $ele = new Xoops\Form\SelectUser($title, $config[$i]->getVar('conf_name'), false, $config[$i]->getConfValueForOutput(), 1, false); |
||
| 146 | break; |
||
| 147 | |||
| 148 | View Code Duplication | case 'user_multi': |
|
| 149 | $ele = new Xoops\Form\SelectUser($title, $config[$i]->getVar('conf_name'), false, $config[$i]->getConfValueForOutput(), 5, true); |
||
| 150 | break; |
||
| 151 | |||
| 152 | View Code Duplication | case 'module_cache': |
|
| 153 | $module_handler = $xoops->getHandlerModule(); |
||
| 154 | $modules = $module_handler->getObjectsArray(new Criteria('hasmain', 1), true); |
||
| 155 | $currrent_val = $config[$i]->getConfValueForOutput(); |
||
| 156 | $cache_options = array( |
||
| 157 | '0' => XoopsLocale::NO_CACHE, |
||
| 158 | '30' => sprintf(XoopsLocale::F_SECONDS, 30), |
||
| 159 | '60' => XoopsLocale::ONE_MINUTE, |
||
| 160 | '300' => sprintf(XoopsLocale::F_MINUTES, 5), |
||
| 161 | '1800' => sprintf(XoopsLocale::F_MINUTES, 30), |
||
| 162 | '3600' => XoopsLocale::ONE_HOUR, |
||
| 163 | '18000' => sprintf(XoopsLocale::F_HOURS, 5), |
||
| 164 | '86400' => XoopsLocale::ONE_DAY, |
||
| 165 | '259200' => sprintf(XoopsLocale::F_DAYS, 3), |
||
| 166 | '604800' => XoopsLocale::ONE_WEEK, |
||
| 167 | '2592000' => XoopsLocale::ONE_MONTH |
||
| 168 | ); |
||
| 169 | if (count($modules) > 0) { |
||
| 170 | $ele = new Xoops\Form\ElementTray($title, '<br />'); |
||
| 171 | foreach (array_keys($modules) as $mid) { |
||
| 172 | $c_val = isset($currrent_val[$mid]) ? (int)($currrent_val[$mid]) : null; |
||
| 173 | $selform = new Xoops\Form\Select($modules[$mid]->getVar('name'), $config[$i]->getVar('conf_name') . "[$mid]", $c_val); |
||
| 174 | $selform->addOptionArray($cache_options); |
||
| 175 | $ele->addElement($selform); |
||
| 176 | unset($selform); |
||
| 177 | } |
||
| 178 | } else { |
||
| 179 | $ele = new Xoops\Form\Label($title, SystemLocale::NO_MODULE_TO_CACHE); |
||
| 180 | } |
||
| 181 | break; |
||
| 182 | |||
| 183 | View Code Duplication | case 'site_cache': |
|
| 184 | $ele = new Xoops\Form\Select($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput()); |
||
| 185 | $ele->addOptionArray(array( |
||
| 186 | '0' => XoopsLocale::NO_CACHE, |
||
| 187 | '30' => sprintf(XoopsLocale::F_SECONDS, 30), |
||
| 188 | '60' => XoopsLocale::ONE_MINUTE, |
||
| 189 | '300' => sprintf(XoopsLocale::F_MINUTES, 5), |
||
| 190 | '1800' => sprintf(XoopsLocale::F_MINUTES, 30), |
||
| 191 | '3600' => XoopsLocale::ONE_HOUR, |
||
| 192 | '18000' => sprintf(XoopsLocale::F_HOURS, 5), |
||
| 193 | '86400' => XoopsLocale::ONE_DAY, |
||
| 194 | '259200' => sprintf(XoopsLocale::F_DAYS, 3), |
||
| 195 | '604800' => XoopsLocale::ONE_WEEK, |
||
| 196 | '2592000' => XoopsLocale::ONE_MONTH |
||
| 197 | )); |
||
| 198 | break; |
||
| 199 | |||
| 200 | case 'password': |
||
| 201 | $ele = new Xoops\Form\Password($title, $config[$i]->getVar('conf_name'), 50, 255, installHtmlSpecialCharacters($config[$i]->getConfValueForOutput())); |
||
| 202 | break; |
||
| 203 | |||
| 204 | case 'color': |
||
| 205 | $ele = new Xoops\Form\ColorPicker($title, $config[$i]->getVar('conf_name'), installHtmlSpecialCharacters($config[$i]->getConfValueForOutput())); |
||
| 206 | break; |
||
| 207 | |||
| 208 | case 'hidden': |
||
| 209 | $ele = new Xoops\Form\Hidden($config[$i]->getVar('conf_name'), installHtmlSpecialCharacters($config[$i]->getConfValueForOutput())); |
||
| 210 | break; |
||
| 211 | |||
| 212 | case 'textbox': |
||
| 213 | default: |
||
| 214 | $ele = new Xoops\Form\Text($title, $config[$i]->getVar('conf_name'), 50, 255, installHtmlSpecialCharacters($config[$i]->getConfValueForOutput())); |
||
| 215 | break; |
||
| 216 | } |
||
| 217 | |||
| 218 | if ($config[$i]->getVar('conf_desc') != '') { |
||
| 219 | $ele->setDescription(\Xoops\Locale::translate($config[$i]->getVar('conf_desc'), 'system')); |
||
| 220 | } |
||
| 221 | $ret[$conf_catid]->addElement($ele); |
||
| 222 | |||
| 223 | $hidden = new Xoops\Form\Hidden('conf_ids[]', $config[$i]->getVar('conf_id')); |
||
| 224 | $ret[$conf_catid]->addElement($hidden); |
||
| 225 | |||
| 226 | unset($ele); |
||
| 227 | unset($hidden); |
||
| 228 | } |
||
| 229 | return $ret; |
||
| 230 | } |
||
| 231 | |||
| 300 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: