| Conditions | 26 |
| Paths | 6460 |
| Total Lines | 130 |
| Code Lines | 78 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 232 | function getTriggersList($forcedirtriggers=null) |
||
| 233 | { |
||
| 234 | global $conf, $langs; |
||
| 235 | |||
| 236 | $files = array(); |
||
| 237 | $fullpath = array(); |
||
| 238 | $relpath = array(); |
||
| 239 | $iscoreorexternal = array(); |
||
| 240 | $modules = array(); |
||
| 241 | $orders = array(); |
||
| 242 | $i = 0; |
||
| 243 | |||
| 244 | $dirtriggers=array_merge(array('/core/triggers/'),$conf->modules_parts['triggers']); |
||
| 245 | if (is_array($forcedirtriggers)) |
||
| 246 | { |
||
| 247 | $dirtriggers=$forcedirtriggers; |
||
| 248 | } |
||
| 249 | |||
| 250 | foreach($dirtriggers as $reldir) |
||
| 251 | { |
||
| 252 | $dir=dol_buildpath($reldir,0); |
||
| 253 | $newdir=dol_osencode($dir); |
||
| 254 | |||
| 255 | // Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php at each call) |
||
| 256 | if (! is_dir($newdir)) continue; |
||
| 257 | |||
| 258 | $handle=opendir($newdir); |
||
| 259 | if (is_resource($handle)) |
||
| 260 | { |
||
| 261 | while (($file = readdir($handle))!==false) |
||
| 262 | { |
||
| 263 | if (is_readable($newdir.'/'.$file) && preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/',$file,$reg)) |
||
| 264 | { |
||
| 265 | if (preg_match('/\.back$/',$file)) continue; |
||
| 266 | |||
| 267 | $part1=$reg[1]; |
||
| 268 | $part2=$reg[2]; |
||
| 269 | $part3=$reg[3]; |
||
| 270 | |||
| 271 | $modName = 'Interface'.ucfirst($reg[3]); |
||
| 272 | //print "file=$file"; print "modName=$modName"; exit; |
||
| 273 | if (in_array($modName,$modules)) |
||
| 274 | { |
||
| 275 | $langs->load("errors"); |
||
| 276 | print '<div class="error">'.$langs->trans("Error").' : '.$langs->trans("ErrorDuplicateTrigger",$modName,"/htdocs/core/triggers/").'</div>'; |
||
| 277 | } |
||
| 278 | else |
||
| 279 | { |
||
| 280 | include_once $newdir.'/'.$file; |
||
| 281 | } |
||
| 282 | |||
| 283 | $files[$i] = $file; |
||
| 284 | $fullpath[$i] = $dir.'/'.$file; |
||
| 285 | $relpath[$i] = preg_replace('/^\//','',$reldir).'/'.$file; |
||
| 286 | $iscoreorexternal[$i] = ($reldir == '/core/triggers/'?'internal':'external'); |
||
| 287 | $modules[$i] = $modName; |
||
| 288 | $orders[$i] = $part1.'_'.$part2.'_'.$part3; // Set sort criteria value |
||
| 289 | |||
| 290 | $i++; |
||
| 291 | } |
||
| 292 | } |
||
| 293 | closedir($handle); |
||
| 294 | } |
||
| 295 | } |
||
| 296 | |||
| 297 | asort($orders); |
||
| 298 | |||
| 299 | $triggers = array(); |
||
| 300 | $j = 0; |
||
| 301 | |||
| 302 | // Loop on each trigger |
||
| 303 | foreach ($orders as $key => $value) |
||
| 304 | { |
||
| 305 | $modName = $modules[$key]; |
||
| 306 | if (empty($modName)) continue; |
||
| 307 | |||
| 308 | if (! class_exists($modName)) |
||
| 309 | { |
||
| 310 | print 'Error: A trigger file was found but its class "'.$modName.'" was not found.'."<br>\n"; |
||
| 311 | continue; |
||
| 312 | } |
||
| 313 | |||
| 314 | $objMod = new $modName($this->db); |
||
| 315 | |||
| 316 | // Define disabledbyname and disabledbymodule |
||
| 317 | $disabledbyname=0; |
||
| 318 | $disabledbymodule=1; |
||
| 319 | $module=''; |
||
| 320 | |||
| 321 | // Check if trigger file is disabled by name |
||
| 322 | if (preg_match('/NORUN$/i',$files[$key])) $disabledbyname=1; |
||
| 323 | // Check if trigger file is for a particular module |
||
| 324 | if (preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/i',$files[$key],$reg)) |
||
| 325 | { |
||
| 326 | $module=preg_replace('/^mod/i','',$reg[2]); |
||
| 327 | $constparam='MAIN_MODULE_'.strtoupper($module); |
||
| 328 | if (strtolower($reg[2]) == 'all') $disabledbymodule=0; |
||
| 329 | else if (empty($conf->global->$constparam)) $disabledbymodule=2; |
||
| 330 | } |
||
| 331 | |||
| 332 | // We set info of modules |
||
| 333 | $triggers[$j]['picto'] = $objMod->picto?img_object('',$objMod->picto):img_object('','generic'); |
||
| 334 | $triggers[$j]['file'] = $files[$key]; |
||
| 335 | $triggers[$j]['fullpath'] = $fullpath[$key]; |
||
| 336 | $triggers[$j]['relpath'] = $relpath[$key]; |
||
| 337 | $triggers[$j]['iscoreorexternal'] = $iscoreorexternal[$key]; |
||
| 338 | $triggers[$j]['version'] = $objMod->getVersion(); |
||
| 339 | $triggers[$j]['status'] = img_picto($langs->trans("Active"),'tick'); |
||
| 340 | if ($disabledbyname > 0 || $disabledbymodule > 1) $triggers[$j]['status'] = ''; |
||
| 341 | |||
| 342 | $text ='<b>'.$langs->trans("Description").':</b><br>'; |
||
| 343 | $text.=$objMod->getDesc().'<br>'; |
||
| 344 | $text.='<br><b>'.$langs->trans("Status").':</b><br>'; |
||
| 345 | if ($disabledbyname == 1) |
||
| 346 | { |
||
| 347 | $text.=$langs->trans("TriggerDisabledByName").'<br>'; |
||
| 348 | if ($disabledbymodule == 2) $text.=$langs->trans("TriggerDisabledAsModuleDisabled",$module).'<br>'; |
||
| 349 | } |
||
| 350 | else |
||
| 351 | { |
||
| 352 | if ($disabledbymodule == 0) $text.=$langs->trans("TriggerAlwaysActive").'<br>'; |
||
| 353 | if ($disabledbymodule == 1) $text.=$langs->trans("TriggerActiveAsModuleActive",$module).'<br>'; |
||
| 354 | if ($disabledbymodule == 2) $text.=$langs->trans("TriggerDisabledAsModuleDisabled",$module).'<br>'; |
||
| 355 | } |
||
| 356 | |||
| 357 | $triggers[$j]['info'] = $text; |
||
| 358 | $j++; |
||
| 359 | } |
||
| 360 | return $triggers; |
||
| 361 | } |
||
| 362 | |||
| 364 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: