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