| Conditions | 43 |
| Paths | 1113 |
| Total Lines | 175 |
| Code Lines | 106 |
| 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 |
||
| 161 | public function executeHooks($method, $parameters = array(), &$object = null, &$action = '') |
||
| 162 | { |
||
| 163 | if (!is_array($this->hooks) || empty($this->hooks)) { |
||
| 164 | return 0; // No hook available, do nothing. |
||
| 165 | } |
||
| 166 | if (!is_array($parameters)) { |
||
| 167 | dol_syslog('executeHooks was called with a non array $parameters. Surely a bug.', LOG_WARNING); |
||
| 168 | $parameters = array(); |
||
| 169 | } |
||
| 170 | |||
| 171 | $parameters['context'] = implode(':', $this->contextarray); |
||
| 172 | //dol_syslog(get_class($this).'::executeHooks method='.$method." action=".$action." context=".$parameters['context']); |
||
| 173 | |||
| 174 | // Define type of hook ('output' or 'addreplace'). |
||
| 175 | $hooktype = 'addreplace'; |
||
| 176 | // TODO Remove hooks with type 'output' (example createFrom). All hooks must be converted into 'addreplace' hooks. |
||
| 177 | if ( |
||
| 178 | in_array($method, array( |
||
| 179 | 'createFrom', |
||
| 180 | 'dashboardAccountancy', |
||
| 181 | 'dashboardActivities', |
||
| 182 | 'dashboardCommercials', |
||
| 183 | 'dashboardContracts', |
||
| 184 | 'dashboardDonation', |
||
| 185 | 'dashboardEmailings', |
||
| 186 | 'dashboardExpenseReport', |
||
| 187 | 'dashboardHRM', |
||
| 188 | 'dashboardInterventions', |
||
| 189 | 'dashboardMRP', |
||
| 190 | 'dashboardMembers', |
||
| 191 | 'dashboardOpensurvey', |
||
| 192 | 'dashboardOrders', |
||
| 193 | 'dashboardOrdersSuppliers', |
||
| 194 | 'dashboardProductServices', |
||
| 195 | 'dashboardProjects', |
||
| 196 | 'dashboardPropals', |
||
| 197 | 'dashboardSpecialBills', |
||
| 198 | 'dashboardSupplierProposal', |
||
| 199 | 'dashboardThirdparties', |
||
| 200 | 'dashboardTickets', |
||
| 201 | 'dashboardUsersGroups', |
||
| 202 | 'dashboardWarehouse', |
||
| 203 | 'dashboardWarehouseReceptions', |
||
| 204 | 'dashboardWarehouseSendings', |
||
| 205 | 'insertExtraHeader', |
||
| 206 | 'insertExtraFooter', |
||
| 207 | 'printLeftBlock', |
||
| 208 | 'formAddObjectLine', |
||
| 209 | 'formBuilddocOptions', |
||
| 210 | 'showSocinfoOnPrint' |
||
| 211 | )) |
||
| 212 | ) { |
||
| 213 | $hooktype = 'output'; |
||
| 214 | } |
||
| 215 | |||
| 216 | // Init return properties |
||
| 217 | $localResPrint = ''; |
||
| 218 | $localResArray = array(); |
||
| 219 | |||
| 220 | $this->resNbOfHooks = 0; |
||
| 221 | |||
| 222 | // Here, the value for $method and $hooktype are given. |
||
| 223 | // Loop on each hook to qualify modules that have declared context |
||
| 224 | $modulealreadyexecuted = array(); |
||
| 225 | $resaction = 0; |
||
| 226 | $error = 0; |
||
| 227 | foreach ($this->hooks as $context => $modules) { // $this->hooks is an array with context as key and value is an array of modules that handle this context |
||
| 228 | if (!empty($modules)) { |
||
| 229 | // Loop on each active hooks of module for this context |
||
| 230 | foreach ($modules as $module => $actionclassinstance) { |
||
| 231 | $module = preg_replace('/^\d+:/', '', $module); |
||
| 232 | //print "Before hook ".get_class($actionclassinstance)." method=".$method." module=".$module." hooktype=".$hooktype." results=".count($actionclassinstance->results)." resprints=".count($actionclassinstance->resprints)." resaction=".$resaction."<br>\n"; |
||
| 233 | |||
| 234 | // test to avoid running twice a hook, when a module implements several active contexts |
||
| 235 | if (in_array($module, $modulealreadyexecuted)) { |
||
| 236 | continue; |
||
| 237 | } |
||
| 238 | |||
| 239 | // jump to next module/class if method does not exist |
||
| 240 | if (!method_exists($actionclassinstance, $method)) { |
||
| 241 | continue; |
||
| 242 | } |
||
| 243 | |||
| 244 | $this->resNbOfHooks++; |
||
| 245 | |||
| 246 | $modulealreadyexecuted[$module] = $module; |
||
| 247 | |||
| 248 | // Clean class (an error may have been set from a previous call of another method for same module/hook) |
||
| 249 | $actionclassinstance->error = ''; |
||
| 250 | $actionclassinstance->errors = array(); |
||
| 251 | |||
| 252 | if (getDolGlobalInt('MAIN_HOOK_DEBUG')) { |
||
| 253 | // This his too much verbose, enabled if const enabled only |
||
| 254 | dol_syslog(get_class($this) . "::executeHooks Qualified hook found (hooktype=" . $hooktype . "). We call method " . get_class($actionclassinstance) . '->' . $method . ", context=" . $context . ", module=" . $module . ", action=" . $action . ((is_object($object) && property_exists($object, 'id')) ? ', object id=' . $object->id : '') . ((is_object($object) && property_exists($object, 'element')) ? ', object element=' . $object->element : ''), LOG_DEBUG); |
||
| 255 | } |
||
| 256 | |||
| 257 | // Add current context to avoid method execution in bad context, you can add this test in your method : eg if($currentcontext != 'formfile') return; |
||
| 258 | // Note: The hook can use the $currentcontext in its code to avoid to be ran twice or be ran for one given context only |
||
| 259 | $parameters['currentcontext'] = $context; |
||
| 260 | // Hooks that must return int (hooks with type 'addreplace') |
||
| 261 | if ($hooktype == 'addreplace') { |
||
| 262 | $resactiontmp = $actionclassinstance->$method($parameters, $object, $action, $this); // $object and $action can be changed by method ($object->id during creation for example or $action to go back to other action for example) |
||
| 263 | $resaction += $resactiontmp; |
||
| 264 | |||
| 265 | if ($resactiontmp < 0 || !empty($actionclassinstance->error) || (!empty($actionclassinstance->errors) && count($actionclassinstance->errors) > 0)) { |
||
| 266 | $error++; |
||
| 267 | $this->error = $actionclassinstance->error; |
||
| 268 | $this->errors = array_merge($this->errors, (array)$actionclassinstance->errors); |
||
| 269 | dol_syslog("Error on hook module=" . $module . ", method " . $method . ", class " . get_class($actionclassinstance) . ", hooktype=" . $hooktype . (empty($this->error) ? '' : " " . $this->error) . (empty($this->errors) ? '' : " " . implode(",", $this->errors)), LOG_ERR); |
||
| 270 | } |
||
| 271 | |||
| 272 | if (isset($actionclassinstance->results) && is_array($actionclassinstance->results)) { |
||
| 273 | if ($resactiontmp > 0) { |
||
| 274 | $localResArray = $actionclassinstance->results; |
||
| 275 | } else { |
||
| 276 | $localResArray = array_merge_recursive($localResArray, $actionclassinstance->results); |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | if (!empty($actionclassinstance->resprints)) { |
||
| 281 | if ($resactiontmp > 0) { |
||
| 282 | $localResPrint = $actionclassinstance->resprints; |
||
| 283 | } else { |
||
| 284 | $localResPrint .= $actionclassinstance->resprints; |
||
| 285 | } |
||
| 286 | } |
||
| 287 | } else { |
||
| 288 | // Generic hooks that return a string or array (printLeftBlock, formAddObjectLine, formBuilddocOptions, ...) |
||
| 289 | |||
| 290 | // TODO. this test should be done into the method of hook by returning nothing |
||
| 291 | if (is_array($parameters) && !empty($parameters['special_code']) && $parameters['special_code'] > 3 && $parameters['special_code'] != $actionclassinstance->module_number) { |
||
| 292 | continue; |
||
| 293 | } |
||
| 294 | |||
| 295 | if (getDolGlobalInt('MAIN_HOOK_DEBUG')) { |
||
| 296 | dol_syslog("Call method " . $method . " of class " . get_class($actionclassinstance) . ", module=" . $module . ", hooktype=" . $hooktype, LOG_DEBUG); |
||
| 297 | } |
||
| 298 | |||
| 299 | $resactiontmp = $actionclassinstance->$method($parameters, $object, $action, $this); // $object and $action can be changed by method ($object->id during creation for example or $action to go back to other action for example) |
||
| 300 | $resaction += $resactiontmp; |
||
| 301 | |||
| 302 | if (!empty($actionclassinstance->results) && is_array($actionclassinstance->results)) { |
||
| 303 | $localResArray = array_merge_recursive($localResArray, $actionclassinstance->results); |
||
| 304 | } |
||
| 305 | if (!empty($actionclassinstance->resprints)) { |
||
| 306 | $localResPrint .= $actionclassinstance->resprints; |
||
| 307 | } |
||
| 308 | if (is_numeric($resactiontmp) && $resactiontmp < 0) { |
||
| 309 | $error++; |
||
| 310 | $this->error = $actionclassinstance->error; |
||
| 311 | $this->errors = array_merge($this->errors, (array)$actionclassinstance->errors); |
||
| 312 | dol_syslog("Error on hook module=" . $module . ", method " . $method . ", class " . get_class($actionclassinstance) . ", hooktype=" . $hooktype . (empty($this->error) ? '' : " " . $this->error) . (empty($this->errors) ? '' : " " . implode(",", $this->errors)), LOG_ERR); |
||
| 313 | } |
||
| 314 | |||
| 315 | // TODO dead code to remove (do not disable this, but fix your hook instead): result must not be a string but an int. you must use $actionclassinstance->resprints to return a string |
||
| 316 | if (!is_array($resactiontmp) && !is_numeric($resactiontmp)) { |
||
| 317 | dol_syslog('Error: Bug into hook ' . $method . ' of module class ' . get_class($actionclassinstance) . '. Method must not return a string but an int (0=OK, 1=Replace, -1=KO) and set string into ->resprints', LOG_ERR); |
||
| 318 | if (empty($actionclassinstance->resprints)) { |
||
| 319 | $localResPrint .= $resactiontmp; |
||
| 320 | } |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | //print "After hook context=".$context." ".get_class($actionclassinstance)." method=".$method." hooktype=".$hooktype." results=".count($actionclassinstance->results)." resprints=".count($actionclassinstance->resprints)." resaction=".$resaction."<br>\n"; |
||
| 325 | |||
| 326 | unset($actionclassinstance->results); |
||
| 327 | unset($actionclassinstance->resprints); |
||
| 328 | } |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | $this->resPrint = $localResPrint; |
||
| 333 | $this->resArray = $localResArray; |
||
| 334 | |||
| 335 | return ($error ? -1 : $resaction); |
||
| 336 | } |
||
| 338 |