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