| Conditions | 31 |
| Paths | > 20000 |
| Total Lines | 169 |
| Code Lines | 100 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 76 | public function run_triggers($action, $object, $user, $langs, $conf) |
||
| 77 | { |
||
| 78 | // phpcs:enable |
||
| 79 | |||
| 80 | if (getDolGlobalInt('MAIN_TRIGGER_DEBUG')) { |
||
| 81 | // This his too much verbose, enabled if const enabled only |
||
| 82 | dol_syslog(get_class($this) . "::run_triggers action=" . $action . " Launch run_triggers", LOG_DEBUG); |
||
| 83 | } |
||
| 84 | |||
| 85 | // Check parameters |
||
| 86 | if (!is_object($object) || !is_object($conf)) { // Error |
||
| 87 | $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); |
||
| 88 | dol_syslog(get_class($this) . '::run_triggers ' . $error, LOG_ERR); |
||
| 89 | $this->errors[] = $error; |
||
| 90 | return -1; |
||
| 91 | } |
||
| 92 | if (!is_object($langs)) { // Warning |
||
| 93 | 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); |
||
| 94 | } |
||
| 95 | if (!is_object($user)) { // Warning |
||
| 96 | 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); |
||
| 97 | $user = new User($this->db); |
||
| 98 | } |
||
| 99 | |||
| 100 | $nbfile = $nbtotal = $nbok = $nbko = 0; |
||
| 101 | $this->lastmoduleerror = ''; |
||
| 102 | |||
| 103 | $files = []; |
||
| 104 | $modules = []; |
||
| 105 | $orders = []; |
||
| 106 | $i = 0; |
||
| 107 | |||
| 108 | $dirtriggers = array_merge(['/core/triggers'], $conf->modules_parts['triggers']); |
||
| 109 | foreach ($dirtriggers as $reldir) { |
||
| 110 | $dir = dol_buildpath($reldir, 0); |
||
| 111 | $newdir = dol_osencode($dir); |
||
| 112 | //print "xx".$dir;exit; |
||
| 113 | |||
| 114 | // Check if directory exists (we do not use dol_is_dir to avoir loading files.lib.php at each call) |
||
| 115 | if (!is_dir($newdir)) { |
||
| 116 | continue; |
||
| 117 | } |
||
| 118 | |||
| 119 | $handle = opendir($newdir); |
||
| 120 | if (is_resource($handle)) { |
||
| 121 | $fullpathfiles = []; |
||
| 122 | while (($file = readdir($handle)) !== false) { |
||
| 123 | $reg = []; |
||
| 124 | if (is_readable($newdir . "/" . $file) && preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php$/i', $file, $reg)) { |
||
| 125 | $part1 = $reg[1]; |
||
| 126 | $part2 = $reg[2]; |
||
| 127 | $part3 = $reg[3]; |
||
| 128 | |||
| 129 | $nbfile++; |
||
| 130 | |||
| 131 | // Check if trigger file is disabled by name |
||
| 132 | if (preg_match('/NORUN$/i', $file)) { |
||
| 133 | continue; |
||
| 134 | } |
||
| 135 | // Check if trigger file is for a particular module |
||
| 136 | $qualified = true; |
||
| 137 | if (strtolower($reg[2]) != 'all') { |
||
| 138 | $module = preg_replace('/^mod/i', '', $reg[2]); |
||
| 139 | if (!isModEnabled(strtolower($module))) { |
||
| 140 | $qualified = false; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | if (!$qualified) { |
||
| 145 | //dol_syslog(get_class($this)."::run_triggers action=".$action." Triggers for file '".$file."' need module to be enabled", LOG_DEBUG); |
||
| 146 | continue; |
||
| 147 | } |
||
| 148 | |||
| 149 | $modName = "Interface" . ucfirst($reg[3]); |
||
| 150 | //print "file=$file - modName=$modName\n"; |
||
| 151 | if (in_array($modName, $modules)) { // $modules = list of modName already loaded |
||
| 152 | $langs->load("errors"); |
||
| 153 | dol_syslog(get_class($this) . "::run_triggers action=" . $action . " " . $langs->trans("ErrorDuplicateTrigger", $newdir . "/" . $file, $fullpathfiles[$modName]), LOG_WARNING); |
||
| 154 | continue; |
||
| 155 | } |
||
| 156 | |||
| 157 | try { |
||
| 158 | //print 'Todo for '.$modName." : ".$newdir.'/'.$file."\n"; |
||
| 159 | include_once $newdir . '/' . $file; |
||
| 160 | //print 'Done for '.$modName."\n"; |
||
| 161 | } catch (Exception $e) { |
||
| 162 | dol_syslog('ko for ' . $modName . " " . $e->getMessage() . "\n", LOG_ERR); |
||
| 163 | } |
||
| 164 | |||
| 165 | $modules[$i] = $modName; |
||
| 166 | $files[$i] = $file; |
||
| 167 | $fullpathfiles[$modName] = $newdir . '/' . $file; |
||
| 168 | $orders[$i] = $part1 . '_' . $part2 . '_' . $part3; // Set sort criteria value |
||
| 169 | |||
| 170 | $i++; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | closedir($handle); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | asort($orders, SORT_NATURAL); |
||
| 179 | |||
| 180 | // Loop on each trigger |
||
| 181 | foreach ($orders as $key => $value) { |
||
| 182 | $modName = $modules[$key]; |
||
| 183 | if (empty($modName)) { |
||
| 184 | continue; |
||
| 185 | } |
||
| 186 | |||
| 187 | $objMod = new $modName($this->db); |
||
| 188 | if ($objMod) { |
||
| 189 | $dblevelbefore = $this->db->transaction_opened; |
||
| 190 | |||
| 191 | $result = 0; |
||
| 192 | |||
| 193 | if (method_exists($objMod, 'runTrigger')) { // New method to implement |
||
| 194 | //dol_syslog(get_class($this)."::run_triggers action=".$action." Launch runTrigger for file '".$files[$key]."'", LOG_DEBUG); |
||
| 195 | $result = $objMod->runTrigger($action, $object, $user, $langs, $conf); |
||
| 196 | } elseif (method_exists($objMod, 'run_trigger')) { // Deprecated method |
||
| 197 | dol_syslog(get_class($this) . "::run_triggers action=" . $action . " Launch old method run_trigger (rename your trigger into runTrigger) for file '" . $files[$key] . "'", LOG_WARNING); |
||
| 198 | $result = $objMod->run_trigger($action, $object, $user, $langs, $conf); |
||
| 199 | } else { |
||
| 200 | 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); |
||
| 201 | } |
||
| 202 | |||
| 203 | $dblevelafter = $this->db->transaction_opened; |
||
| 204 | |||
| 205 | if ($dblevelbefore != $dblevelafter) { |
||
| 206 | $errormessage = "Error, the balance begin/close of db transactions has been broken into trigger " . $modName . " with action=" . $action . " before=" . $dblevelbefore . " after=" . $dblevelafter; |
||
| 207 | $this->errors[] = $errormessage; |
||
| 208 | dol_syslog($errormessage, LOG_ERR); |
||
| 209 | $result = -1; |
||
| 210 | } |
||
| 211 | |||
| 212 | if ($result > 0) { |
||
| 213 | // Action OK |
||
| 214 | $nbtotal++; |
||
| 215 | $nbok++; |
||
| 216 | } |
||
| 217 | if ($result == 0) { |
||
| 218 | // Aucune action faite |
||
| 219 | $nbtotal++; |
||
| 220 | } |
||
| 221 | if ($result < 0) { |
||
| 222 | // Action KO |
||
| 223 | //dol_syslog("Error in trigger ".$action." - result = ".$result." - Nb of error string returned = ".count($objMod->errors), LOG_ERR); |
||
| 224 | $nbtotal++; |
||
| 225 | $nbko++; |
||
| 226 | $this->lastmoduleerror = $modName; |
||
| 227 | if (!empty($objMod->errors)) { |
||
| 228 | $this->errors = array_merge($this->errors, $objMod->errors); |
||
| 229 | } elseif (!empty($objMod->error)) { |
||
| 230 | $this->errors[] = $objMod->error; |
||
| 231 | } |
||
| 232 | //dol_syslog("Error in trigger ".$action." - Nb of error string returned = ".count($this->errors), LOG_ERR); |
||
| 233 | } |
||
| 234 | } else { |
||
| 235 | dol_syslog(get_class($this) . "::run_triggers action=" . $action . " Failed to instantiate trigger for file '" . $files[$key] . "'", LOG_ERR); |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | if ($nbko) { |
||
| 240 | dol_syslog(get_class($this) . "::run_triggers action=" . $action . " Files found: " . $nbfile . ", Files launched: " . $nbtotal . ", Done: " . $nbok . ", Failed: " . $nbko . ($this->lastmoduleerror ? " - Last module in error: " . $this->lastmoduleerror : "") . " - Nb of error string returned in this->errors = " . count($this->errors), LOG_ERR); |
||
| 241 | return -$nbko; |
||
| 242 | } else { |
||
| 243 | //dol_syslog(get_class($this)."::run_triggers Files found: ".$nbfile.", Files launched: ".$nbtotal.", Done: ".$nbok.", Failed: ".$nbko, LOG_DEBUG); |
||
| 244 | return $nbok; |
||
| 245 | } |
||
| 415 |