| Conditions | 28 |
| Paths | 7345 |
| Total Lines | 165 |
| Code Lines | 85 |
| 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 |
||
| 59 | function run_triggers($action,$object,$user,$langs,$conf) |
||
| 60 | { |
||
| 61 | // Check parameters |
||
| 62 | if (! is_object($object) || ! is_object($conf)) // Error |
||
| 63 | { |
||
| 64 | $this->error='function run_triggers called with wrong parameters action='.$action.' object='.is_object($object).' user='.is_object($user).' langs='.is_object($langs).' conf='.is_object($conf); |
||
| 65 | dol_syslog(get_class($this).'::run_triggers '.$this->error, LOG_ERR); |
||
| 66 | $this->errors[]=$this->error; |
||
| 67 | return -1; |
||
| 68 | } |
||
| 69 | if (! is_object($langs)) // Warning |
||
| 70 | { |
||
| 71 | dol_syslog(get_class($this).'::run_triggers was called with wrong parameters action='.$action.' object='.is_object($object).' user='.is_object($user).' langs='.is_object($langs).' conf='.is_object($conf), LOG_WARNING); |
||
| 72 | } |
||
| 73 | if (! is_object($user)) // Warning |
||
| 74 | { |
||
| 75 | dol_syslog(get_class($this).'::run_triggers was called with wrong parameters action='.$action.' object='.is_object($object).' user='.is_object($user).' langs='.is_object($langs).' conf='.is_object($conf), LOG_WARNING); |
||
| 76 | global $db; |
||
| 77 | $user = new User($db); |
||
| 78 | } |
||
| 79 | |||
| 80 | $nbfile = $nbtotal = $nbok = $nbko = 0; |
||
| 81 | |||
| 82 | $files = array(); |
||
| 83 | $modules = array(); |
||
| 84 | $orders = array(); |
||
| 85 | $i=0; |
||
| 86 | |||
| 87 | $dirtriggers=array_merge(array('/core/triggers'),$conf->modules_parts['triggers']); |
||
| 88 | foreach($dirtriggers as $reldir) |
||
| 89 | { |
||
| 90 | $dir=dol_buildpath($reldir,0); |
||
| 91 | $newdir=dol_osencode($dir); |
||
| 92 | //print "xx".$dir;exit; |
||
| 93 | |||
| 94 | // Check if directory exists (we do not use dol_is_dir to avoir loading files.lib.php at each call) |
||
| 95 | if (! is_dir($newdir)) continue; |
||
| 96 | |||
| 97 | $handle=opendir($newdir); |
||
| 98 | if (is_resource($handle)) |
||
| 99 | { |
||
| 100 | while (($file = readdir($handle))!==false) |
||
| 101 | { |
||
| 102 | if (is_readable($newdir."/".$file) && preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php$/i',$file,$reg)) |
||
| 103 | { |
||
| 104 | $part1=$reg[1]; |
||
| 105 | $part2=$reg[2]; |
||
| 106 | $part3=$reg[3]; |
||
| 107 | |||
| 108 | $nbfile++; |
||
| 109 | |||
| 110 | // Check if trigger file is disabled by name |
||
| 111 | if (preg_match('/NORUN$/i',$file)) continue; |
||
| 112 | // Check if trigger file is for a particular module |
||
| 113 | $qualified=true; |
||
| 114 | if (strtolower($reg[2]) != 'all') |
||
| 115 | { |
||
| 116 | $module=preg_replace('/^mod/i','',$reg[2]); |
||
| 117 | $constparam='MAIN_MODULE_'.strtoupper($module); |
||
| 118 | if (empty($conf->global->$constparam)) $qualified=false; |
||
| 119 | } |
||
| 120 | |||
| 121 | if (! $qualified) |
||
| 122 | { |
||
| 123 | dol_syslog(get_class($this)."::run_triggers action=".$action." Triggers for file '".$file."' need module to be enabled", LOG_DEBUG); |
||
| 124 | continue; |
||
| 125 | } |
||
| 126 | |||
| 127 | $modName = "Interface".ucfirst($reg[3]); |
||
| 128 | //print "file=$file - modName=$modName\n"; |
||
| 129 | if (in_array($modName,$modules)) // $modules = list of modName already loaded |
||
| 130 | { |
||
| 131 | $langs->load("errors"); |
||
| 132 | dol_syslog(get_class($this)."::run_triggers action=".$action." ".$langs->trans("ErrorDuplicateTrigger", $newdir."/".$file, $fullpathfiles[$modName]), LOG_WARNING); |
||
|
|
|||
| 133 | continue; |
||
| 134 | } |
||
| 135 | |||
| 136 | try { |
||
| 137 | //print 'Todo for '.$modName." : ".$newdir.'/'.$file."\n"; |
||
| 138 | include_once $newdir.'/'.$file; |
||
| 139 | //print 'Done for '.$modName."\n"; |
||
| 140 | } |
||
| 141 | catch(Exception $e) |
||
| 142 | { |
||
| 143 | dol_syslog('ko for '.$modName." ".$e->getMessage()."\n", LOG_ERR); |
||
| 144 | } |
||
| 145 | |||
| 146 | $modules[$i] = $modName; |
||
| 147 | $files[$i] = $file; |
||
| 148 | $fullpathfiles[$modName] = $newdir.'/'.$file; |
||
| 149 | $orders[$i] = $part1.'_'.$part2.'_'.$part3; // Set sort criteria value |
||
| 150 | |||
| 151 | $i++; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | asort($orders); |
||
| 158 | |||
| 159 | // Loop on each trigger |
||
| 160 | foreach ($orders as $key => $value) |
||
| 161 | { |
||
| 162 | $modName = $modules[$key]; |
||
| 163 | if (empty($modName)) continue; |
||
| 164 | |||
| 165 | $objMod = new $modName($this->db); |
||
| 166 | if ($objMod) |
||
| 167 | { |
||
| 168 | $result=0; |
||
| 169 | |||
| 170 | if (method_exists($objMod, 'runTrigger')) // New method to implement |
||
| 171 | { |
||
| 172 | dol_syslog(get_class($this)."::run_triggers action=".$action." Launch runTrigger for file '".$files[$key]."'", LOG_INFO); |
||
| 173 | $result=$objMod->runTrigger($action,$object,$user,$langs,$conf); |
||
| 174 | } |
||
| 175 | elseif (method_exists($objMod, 'run_trigger')) // Deprecated method |
||
| 176 | { |
||
| 177 | dol_syslog(get_class($this)."::run_triggers action=".$action." Launch run_trigger for file '".$files[$key]."'", LOG_INFO); |
||
| 178 | $result=$objMod->run_trigger($action,$object,$user,$langs,$conf); |
||
| 179 | } |
||
| 180 | else |
||
| 181 | { |
||
| 182 | dol_syslog(get_class($this)."::run_triggers action=".$action." A trigger was declared for class ".get_class($objMod)." but method runTrigger was not found", LOG_ERR); |
||
| 183 | } |
||
| 184 | |||
| 185 | if ($result > 0) |
||
| 186 | { |
||
| 187 | // Action OK |
||
| 188 | $nbtotal++; |
||
| 189 | $nbok++; |
||
| 190 | } |
||
| 191 | if ($result == 0) |
||
| 192 | { |
||
| 193 | // Aucune action faite |
||
| 194 | $nbtotal++; |
||
| 195 | } |
||
| 196 | if ($result < 0) |
||
| 197 | { |
||
| 198 | // Action KO |
||
| 199 | //dol_syslog("Error in trigger ".$action." - Nb of error string returned = ".count($objMod->errors), LOG_ERR); |
||
| 200 | $nbtotal++; |
||
| 201 | $nbko++; |
||
| 202 | if (! empty($objMod->errors)) $this->errors=array_merge($this->errors,$objMod->errors); |
||
| 203 | else if (! empty($objMod->error)) $this->errors[]=$objMod->error; |
||
| 204 | //dol_syslog("Error in trigger ".$action." - Nb of error string returned = ".count($this->errors), LOG_ERR); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | else |
||
| 208 | { |
||
| 209 | dol_syslog(get_class($this)."::run_triggers action=".$action." Failed to instantiate trigger for file '".$files[$key]."'", LOG_ERR); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | if ($nbko) |
||
| 214 | { |
||
| 215 | dol_syslog(get_class($this)."::run_triggers action=".$action." Files found: ".$nbfile.", Files launched: ".$nbtotal.", Done: ".$nbok.", Failed: ".$nbko." - Nb of error string returned in this->errors = ".count($this->errors), LOG_ERR); |
||
| 216 | return -$nbko; |
||
| 217 | } |
||
| 218 | else |
||
| 219 | { |
||
| 220 | //dol_syslog(get_class($this)."::run_triggers Files found: ".$nbfile.", Files launched: ".$nbtotal.", Done: ".$nbok.", Failed: ".$nbko, LOG_DEBUG); |
||
| 221 | return $nbok; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 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: