| Conditions | 47 |
| Paths | 4452 |
| Total Lines | 200 |
| Code Lines | 122 |
| 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 |
||
| 204 | public function executeHooks($method, $parameters = array(), &$object = null, &$action = '') |
||
| 205 | { |
||
| 206 | //global $debugbar; |
||
| 207 | //if (is_object($debugbar) && get_class($debugbar) === 'DolibarrDebugBar') { |
||
| 208 | if (isModEnabled('debugbar') && function_exists('debug_backtrace')) { |
||
| 209 | $trace = debug_backtrace(); |
||
| 210 | if (isset($trace[0])) { |
||
| 211 | $hookInformations = [ |
||
| 212 | 'name' => $method, |
||
| 213 | 'contexts' => $this->contextarray, |
||
| 214 | 'file' => $trace[0]['file'], |
||
| 215 | 'line' => $trace[0]['line'], |
||
| 216 | 'count' => 0, |
||
| 217 | ]; |
||
| 218 | $hash = md5(json_encode($hookInformations)); |
||
| 219 | if (!empty($this->hooksHistory[$hash])) { |
||
| 220 | $this->hooksHistory[$hash]['count']++; |
||
| 221 | } else { |
||
| 222 | $hookInformations['count'] = 1; |
||
| 223 | $this->hooksHistory[$hash] = $hookInformations; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | if (!is_array($this->hooks) || empty($this->hooks)) { |
||
| 229 | return 0; // No hook available, do nothing. |
||
| 230 | } |
||
| 231 | if (!is_array($parameters)) { |
||
| 232 | dol_syslog('executeHooks was called with a non array $parameters. Surely a bug.', LOG_WARNING); |
||
| 233 | $parameters = array(); |
||
| 234 | } |
||
| 235 | |||
| 236 | $parameters['context'] = implode(':', $this->contextarray); |
||
| 237 | //dol_syslog(get_class($this).'::executeHooks method='.$method." action=".$action." context=".$parameters['context']); |
||
| 238 | |||
| 239 | // Define type of hook ('output' or 'addreplace'). |
||
| 240 | $hooktype = 'addreplace'; |
||
| 241 | // TODO Remove hooks with type 'output' (example createFrom). All hooks must be converted into 'addreplace' hooks. |
||
| 242 | if ( |
||
| 243 | in_array($method, array( |
||
| 244 | 'createFrom', |
||
| 245 | 'dashboardAccountancy', |
||
| 246 | 'dashboardActivities', |
||
| 247 | 'dashboardCommercials', |
||
| 248 | 'dashboardContracts', |
||
| 249 | 'dashboardDonation', |
||
| 250 | 'dashboardEmailings', |
||
| 251 | 'dashboardExpenseReport', |
||
| 252 | 'dashboardHRM', |
||
| 253 | 'dashboardInterventions', |
||
| 254 | 'dashboardMRP', |
||
| 255 | 'dashboardMembers', |
||
| 256 | 'dashboardOpensurvey', |
||
| 257 | 'dashboardOrders', |
||
| 258 | 'dashboardOrdersSuppliers', |
||
| 259 | 'dashboardProductServices', |
||
| 260 | 'dashboardProjects', |
||
| 261 | 'dashboardPropals', |
||
| 262 | 'dashboardSpecialBills', |
||
| 263 | 'dashboardSupplierProposal', |
||
| 264 | 'dashboardThirdparties', |
||
| 265 | 'dashboardTickets', |
||
| 266 | 'dashboardUsersGroups', |
||
| 267 | 'dashboardWarehouse', |
||
| 268 | 'dashboardWarehouseReceptions', |
||
| 269 | 'dashboardWarehouseSendings', |
||
| 270 | 'insertExtraHeader', |
||
| 271 | 'insertExtraFooter', |
||
| 272 | 'printLeftBlock', |
||
| 273 | 'formAddObjectLine', |
||
| 274 | 'formBuilddocOptions', |
||
| 275 | 'showSocinfoOnPrint' |
||
| 276 | )) |
||
| 277 | ) { |
||
| 278 | $hooktype = 'output'; |
||
| 279 | } |
||
| 280 | |||
| 281 | // Init return properties |
||
| 282 | $localResPrint = ''; |
||
| 283 | $localResArray = array(); |
||
| 284 | |||
| 285 | $this->resNbOfHooks = 0; |
||
| 286 | |||
| 287 | // Here, the value for $method and $hooktype are given. |
||
| 288 | // Loop on each hook to qualify modules that have declared context |
||
| 289 | $modulealreadyexecuted = array(); |
||
| 290 | $resaction = 0; |
||
| 291 | $error = 0; |
||
| 292 | foreach ($this->hooksSorted as $context => $modules) { // $this->hooks is an array with the context as key and the value is an array of modules that handle this context |
||
| 293 | if (!empty($modules)) { |
||
| 294 | '@phan-var-force array<string,CommonHookActions> $modules'; |
||
| 295 | // Loop on each active hooks of module for this context |
||
| 296 | foreach ($modules as $module => $actionclassinstance) { |
||
| 297 | $module = preg_replace('/^\d+:/', '', $module); // $module string is 'priority:module' |
||
| 298 | //print "Before hook ".get_class($actionclassinstance)." method=".$method." module=".$module." hooktype=".$hooktype." results=".count($actionclassinstance->results)." resprints=".count($actionclassinstance->resprints)." resaction=".$resaction."<br>\n"; |
||
| 299 | |||
| 300 | // test to avoid running twice a hook, when a module implements several active contexts |
||
| 301 | if (in_array($module, $modulealreadyexecuted)) { |
||
| 302 | continue; |
||
| 303 | } |
||
| 304 | |||
| 305 | // jump to next module/class if method does not exist |
||
| 306 | if (!method_exists($actionclassinstance, $method)) { |
||
| 307 | continue; |
||
| 308 | } |
||
| 309 | |||
| 310 | $this->resNbOfHooks++; |
||
| 311 | |||
| 312 | $modulealreadyexecuted[$module] = $module; |
||
| 313 | |||
| 314 | // Clean class (an error may have been set from a previous call of another method for same module/hook) |
||
| 315 | $actionclassinstance->error = ''; |
||
| 316 | $actionclassinstance->errors = array(); |
||
| 317 | |||
| 318 | if (getDolGlobalInt('MAIN_HOOK_DEBUG')) { |
||
| 319 | // This his too much verbose, enabled if const enabled only |
||
| 320 | 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); |
||
| 321 | } |
||
| 322 | |||
| 323 | // Add current context to avoid method execution in bad context, you can add this test in your method : eg if($currentcontext != 'formfile') return; |
||
| 324 | // Note: The hook can use the $currentcontext in its code to avoid to be ran twice or be ran for one given context only |
||
| 325 | $parameters['currentcontext'] = $context; |
||
| 326 | // Hooks that must return int (hooks with type 'addreplace') |
||
| 327 | if ($hooktype == 'addreplace') { |
||
| 328 | // @phan-suppress-next-line PhanUndeclaredMethod The method's existence is tested above. |
||
| 329 | $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) |
||
| 330 | $resaction += $resactiontmp; |
||
| 331 | |||
| 332 | if ($resactiontmp < 0 || !empty($actionclassinstance->error) || (!empty($actionclassinstance->errors) && count($actionclassinstance->errors) > 0)) { |
||
| 333 | $error++; |
||
| 334 | $this->error = $actionclassinstance->error; |
||
| 335 | $this->errors = array_merge($this->errors, (array) $actionclassinstance->errors); |
||
| 336 | 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); |
||
| 337 | } |
||
| 338 | |||
| 339 | if (isset($actionclassinstance->results) && is_array($actionclassinstance->results)) { |
||
| 340 | if ($resactiontmp > 0) { |
||
| 341 | $localResArray = $actionclassinstance->results; |
||
| 342 | } else { |
||
| 343 | $localResArray = array_merge_recursive($localResArray, $actionclassinstance->results); |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | if (!empty($actionclassinstance->resprints)) { |
||
| 348 | if ($resactiontmp > 0) { |
||
| 349 | $localResPrint = (string) $actionclassinstance->resprints; |
||
| 350 | } else { |
||
| 351 | $localResPrint .= (string) $actionclassinstance->resprints; |
||
| 352 | } |
||
| 353 | } |
||
| 354 | } else { |
||
| 355 | // Generic hooks that return a string or array (printLeftBlock, formAddObjectLine, formBuilddocOptions, ...) |
||
| 356 | |||
| 357 | // TODO. this test should be done into the method of hook by returning nothing |
||
| 358 | if (is_array($parameters) && !empty($parameters['special_code']) && $parameters['special_code'] > 3 && $parameters['special_code'] != $actionclassinstance->module_number) { |
||
| 359 | continue; |
||
| 360 | } |
||
| 361 | |||
| 362 | if (getDolGlobalInt('MAIN_HOOK_DEBUG')) { |
||
| 363 | dol_syslog("Call method " . $method . " of class " . get_class($actionclassinstance) . ", module=" . $module . ", hooktype=" . $hooktype, LOG_DEBUG); |
||
| 364 | } |
||
| 365 | |||
| 366 | // @phan-suppress-next-line PhanUndeclaredMethod The method's existence is tested above. |
||
| 367 | $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) |
||
| 368 | $resaction += $resactiontmp; |
||
| 369 | |||
| 370 | if (!empty($actionclassinstance->results) && is_array($actionclassinstance->results)) { |
||
| 371 | $localResArray = array_merge_recursive($localResArray, $actionclassinstance->results); |
||
| 372 | } |
||
| 373 | if (!empty($actionclassinstance->resprints)) { |
||
| 374 | $localResPrint .= (string) $actionclassinstance->resprints; |
||
| 375 | } |
||
| 376 | if (is_numeric($resactiontmp) && $resactiontmp < 0) { |
||
| 377 | $error++; |
||
| 378 | $this->error = $actionclassinstance->error; |
||
| 379 | $this->errors = array_merge($this->errors, (array) $actionclassinstance->errors); |
||
| 380 | 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); |
||
| 381 | } |
||
| 382 | |||
| 383 | // 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 |
||
| 384 | if (!is_array($resactiontmp) && !is_numeric($resactiontmp)) { |
||
| 385 | 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); |
||
| 386 | if (empty($actionclassinstance->resprints)) { |
||
| 387 | $localResPrint .= $resactiontmp; |
||
| 388 | } |
||
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | //print "After hook context=".$context." ".get_class($actionclassinstance)." method=".$method." hooktype=".$hooktype." results=".count($actionclassinstance->results)." resprints=".count($actionclassinstance->resprints)." resaction=".$resaction."<br>\n"; |
||
| 393 | |||
| 394 | $actionclassinstance->results = array(); |
||
| 395 | $actionclassinstance->resprints = null; |
||
| 396 | } |
||
| 397 | } |
||
| 398 | } |
||
| 399 | |||
| 400 | $this->resPrint = $localResPrint; |
||
| 401 | $this->resArray = $localResArray; |
||
| 402 | |||
| 403 | return ($error ? -1 : $resaction); |
||
| 404 | } |
||
| 406 |