| Conditions | 29 |
| Paths | 2428 |
| Total Lines | 136 |
| Code Lines | 89 |
| Lines | 15 |
| Ratio | 11.03 % |
| Changes | 0 | ||
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 |
||
| 123 | function getWidgetInfo($widget) |
||
| 124 | { |
||
| 125 | // Get a path of the requested module. Return if not exists. |
||
| 126 | $widget_path = $this->getWidgetPath($widget); |
||
| 127 | if(!$widget_path) return; |
||
| 128 | // Read the xml file for module skin information |
||
| 129 | $xml_file = sprintf("%sconf/info.xml", $widget_path); |
||
| 130 | if(!file_exists($xml_file)) return; |
||
| 131 | // If the problem by comparing the cache file and include the return variable $widget_info |
||
| 132 | $cache_file = sprintf(_XE_PATH_ . 'files/cache/widget/%s.%s.cache.php', $widget, Context::getLangType()); |
||
| 133 | |||
| 134 | View Code Duplication | if(file_exists($cache_file)&&filemtime($cache_file)>filemtime($xml_file)) |
|
| 135 | { |
||
| 136 | @include($cache_file); |
||
| 137 | return $widget_info; |
||
| 138 | } |
||
| 139 | // If no cache file exists, parse the xml and then return the variable. |
||
| 140 | $oXmlParser = new XmlParser(); |
||
| 141 | $tmp_xml_obj = $oXmlParser->loadXmlFile($xml_file); |
||
| 142 | $xml_obj = $tmp_xml_obj->widget; |
||
| 143 | if(!$xml_obj) return; |
||
| 144 | |||
| 145 | $buff = '$widget_info = new stdClass;'; |
||
| 146 | |||
| 147 | if($xml_obj->version && $xml_obj->attrs->version == '0.2') |
||
| 148 | { |
||
| 149 | // Title of the widget, version |
||
| 150 | $buff .= sprintf('$widget_info->widget = "%s";', $widget); |
||
| 151 | $buff .= sprintf('$widget_info->path = "%s";', $widget_path); |
||
| 152 | $buff .= sprintf('$widget_info->title = "%s";', $xml_obj->title->body); |
||
| 153 | $buff .= sprintf('$widget_info->description = "%s";', $xml_obj->description->body); |
||
| 154 | $buff .= sprintf('$widget_info->version = "%s";', $xml_obj->version->body); |
||
| 155 | sscanf($xml_obj->date->body, '%d-%d-%d', $date_obj->y, $date_obj->m, $date_obj->d); |
||
| 156 | $date = sprintf('%04d%02d%02d', $date_obj->y, $date_obj->m, $date_obj->d); |
||
| 157 | $buff .= sprintf('$widget_info->date = "%s";', $date); |
||
| 158 | $buff .= sprintf('$widget_info->homepage = "%s";', $xml_obj->link->body); |
||
| 159 | $buff .= sprintf('$widget_info->license = "%s";', $xml_obj->license->body); |
||
| 160 | $buff .= sprintf('$widget_info->license_link = "%s";', $xml_obj->license->attrs->link); |
||
| 161 | $buff .= sprintf('$widget_info->widget_srl = $widget_srl;'); |
||
| 162 | $buff .= sprintf('$widget_info->widget_title = $widget_title;'); |
||
| 163 | // Author information |
||
| 164 | View Code Duplication | if(!is_array($xml_obj->author)) $author_list[] = $xml_obj->author; |
|
| 165 | else $author_list = $xml_obj->author; |
||
| 166 | |||
| 167 | for($i=0; $i < count($author_list); $i++) |
||
| 168 | { |
||
| 169 | $buff .= '$widget_info->author['.$i.'] = new stdClass;'; |
||
| 170 | $buff .= sprintf('$widget_info->author['.$i.']->name = "%s";', $author_list[$i]->name->body); |
||
| 171 | $buff .= sprintf('$widget_info->author['.$i.']->email_address = "%s";', $author_list[$i]->attrs->email_address); |
||
| 172 | $buff .= sprintf('$widget_info->author['.$i.']->homepage = "%s";', $author_list[$i]->attrs->link); |
||
| 173 | } |
||
| 174 | } |
||
| 175 | else |
||
| 176 | { |
||
| 177 | // Title of the widget, version |
||
| 178 | $buff .= sprintf('$widget_info->widget = "%s";', $widget); |
||
| 179 | $buff .= sprintf('$widget_info->path = "%s";', $widget_path); |
||
| 180 | $buff .= sprintf('$widget_info->title = "%s";', $xml_obj->title->body); |
||
| 181 | $buff .= sprintf('$widget_info->description = "%s";', $xml_obj->author->description->body); |
||
| 182 | $buff .= sprintf('$widget_info->version = "%s";', $xml_obj->attrs->version); |
||
| 183 | sscanf($xml_obj->author->attrs->date, '%d. %d. %d', $date_obj->y, $date_obj->m, $date_obj->d); |
||
| 184 | $date = sprintf('%04d%02d%02d', $date_obj->y, $date_obj->m, $date_obj->d); |
||
| 185 | $buff .= sprintf('$widget_info->date = "%s";', $date); |
||
| 186 | $buff .= sprintf('$widget_info->widget_srl = $widget_srl;'); |
||
| 187 | $buff .= sprintf('$widget_info->widget_title = $widget_title;'); |
||
| 188 | // Author information |
||
| 189 | $buff .= '$widget_info->author[0] = new stdClass;'; |
||
| 190 | $buff .= sprintf('$widget_info->author[0]->name = "%s";', $xml_obj->author->name->body); |
||
| 191 | $buff .= sprintf('$widget_info->author[0]->email_address = "%s";', $xml_obj->author->attrs->email_address); |
||
| 192 | $buff .= sprintf('$widget_info->author[0]->homepage = "%s";', $xml_obj->author->attrs->link); |
||
| 193 | } |
||
| 194 | // Extra vars (user defined variables to use in a template) |
||
| 195 | $extra_var_groups = $xml_obj->extra_vars->group; |
||
| 196 | if(!$extra_var_groups) $extra_var_groups = $xml_obj->extra_vars; |
||
| 197 | if(!is_array($extra_var_groups)) $extra_var_groups = array($extra_var_groups); |
||
| 198 | foreach($extra_var_groups as $group) |
||
| 199 | { |
||
| 200 | $extra_vars = $group->var; |
||
| 201 | if(!is_array($group->var)) $extra_vars = array($group->var); |
||
| 202 | |||
| 203 | if($extra_vars[0]->attrs->id || $extra_vars[0]->attrs->name) |
||
| 204 | { |
||
| 205 | $extra_var_count = count($extra_vars); |
||
| 206 | |||
| 207 | $buff .= sprintf('$widget_info->extra_var_count = "%s";', $extra_var_count); |
||
| 208 | for($i=0;$i<$extra_var_count;$i++) |
||
| 209 | { |
||
| 210 | unset($var); |
||
| 211 | unset($options); |
||
| 212 | $var = $extra_vars[$i]; |
||
| 213 | |||
| 214 | $id = $var->attrs->id?$var->attrs->id:$var->attrs->name; |
||
| 215 | $name = $var->name->body?$var->name->body:$var->title->body; |
||
| 216 | $type = $var->attrs->type?$var->attrs->type:$var->type->body; |
||
| 217 | $buff .= sprintf('$widget_info->extra_var->%s = new stdClass;', $id); |
||
| 218 | if($type =='filebox') |
||
| 219 | { |
||
| 220 | $buff .= sprintf('$widget_info->extra_var->%s->filter = "%s";', $id, $var->type->attrs->filter); |
||
| 221 | $buff .= sprintf('$widget_info->extra_var->%s->allow_multiple = "%s";', $id, $var->type->attrs->allow_multiple); |
||
| 222 | } |
||
| 223 | |||
| 224 | $buff .= sprintf('$widget_info->extra_var->%s->group = "%s";', $id, $group->title->body); |
||
| 225 | $buff .= sprintf('$widget_info->extra_var->%s->name = "%s";', $id, $name); |
||
| 226 | $buff .= sprintf('$widget_info->extra_var->%s->type = "%s";', $id, $type); |
||
| 227 | $buff .= sprintf('$widget_info->extra_var->%s->value = $vars->%s;', $id, $id); |
||
| 228 | $buff .= sprintf('$widget_info->extra_var->%s->description = "%s";', $id, str_replace('"','\"',$var->description->body)); |
||
| 229 | |||
| 230 | $options = $var->options; |
||
| 231 | if(!$options) continue; |
||
| 232 | |||
| 233 | if(!is_array($options)) $options = array($options); |
||
| 234 | $options_count = count($options); |
||
| 235 | for($j=0;$j<$options_count;$j++) |
||
| 236 | { |
||
| 237 | $buff .= sprintf('$widget_info->extra_var->%s->options["%s"] = "%s";', $id, $options[$j]->value->body, $options[$j]->name->body); |
||
| 238 | |||
| 239 | View Code Duplication | if($options[$j]->attrs->default && $options[$j]->attrs->default=='true') |
|
| 240 | { |
||
| 241 | $buff .= sprintf('$widget_info->extra_var->%s->default_options["%s"] = true;', $id, $options[$j]->value->body); |
||
| 242 | } |
||
| 243 | |||
| 244 | View Code Duplication | if($options[$j]->attrs->init && $options[$j]->attrs->init=='true') |
|
| 245 | { |
||
| 246 | $buff .= sprintf('$widget_info->extra_var->%s->init_options["%s"] = true;', $id, $options[$j]->value->body); |
||
| 247 | } |
||
| 248 | } |
||
| 249 | } |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | $buff = '<?php if(!defined("__XE__")) exit(); '.$buff.' ?>'; |
||
| 254 | FileHandler::writeFile($cache_file, $buff); |
||
| 255 | |||
| 256 | if(file_exists($cache_file)) @include($cache_file); |
||
| 257 | return $widget_info; |
||
| 258 | } |
||
| 259 | |||
| 372 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.