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