| Conditions | 24 |
| Paths | 15 |
| Total Lines | 54 |
| Code Lines | 26 |
| 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 |
||
| 76 | public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf) |
||
| 77 | { |
||
| 78 | if ( |
||
| 79 | $action === 'PROPAL_CREATE' || $action === 'ORDER_CREATE' || $action === 'BILL_CREATE' |
||
| 80 | || $action === 'ORDER_SUPPLIER_CREATE' || $action === 'BILL_SUPPLIER_CREATE' || $action === 'PROPOSAL_SUPPLIER_CREATE' |
||
| 81 | || $action === 'CONTRACT_CREATE' || $action === 'FICHINTER_CREATE' || $action === 'PROJECT_CREATE' || $action === 'TICKET_CREATE' |
||
| 82 | ) { |
||
| 83 | dol_syslog("Trigger '" . $this->name . "' for action '" . $action . "' launched by " . __FILE__ . ". id=" . $object->id); |
||
| 84 | |||
| 85 | $socid = (property_exists($object, 'socid') ? $object->socid : $object->fk_soc); |
||
| 86 | |||
| 87 | if (!empty($socid) && $socid > 0) { |
||
| 88 | $contactdefault = new Contact($this->db); |
||
| 89 | $contactdefault->socid = $socid; |
||
| 90 | |||
| 91 | $TContact = array(); |
||
| 92 | if (method_exists($contactdefault, 'getContactRoles')) { // For backward compatibility |
||
| 93 | $TContact = $contactdefault->getContactRoles($object->element); |
||
| 94 | } |
||
| 95 | |||
| 96 | if (is_array($TContact) && !empty($TContact)) { |
||
| 97 | $TContactAlreadyLinked = array(); |
||
| 98 | |||
| 99 | if ($object->id > 0) { |
||
| 100 | $TContactAlreadyLinked = array_merge($object->liste_contact(-1, 'external'), $object->liste_contact(-1, 'internal')); |
||
| 101 | } |
||
| 102 | |||
| 103 | foreach ($TContact as $i => $infos) { |
||
| 104 | foreach ($TContactAlreadyLinked as $contactData) { |
||
| 105 | if ($contactData['id'] == $infos['fk_socpeople'] && $contactData['fk_c_type_contact'] == $infos['type_contact']) { |
||
| 106 | unset($TContact[$i]); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | $nb = 0; |
||
| 112 | foreach ($TContact as $infos) { |
||
| 113 | $res = $object->add_contact($infos['fk_socpeople'], $infos['type_contact']); |
||
| 114 | if ($res > 0) { |
||
| 115 | $nb++; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | // We disable this message, it shows the message in api, public page or batch actions when it should not. |
||
| 120 | // Message setting must be done by the calling GUI page and not set inside the trigger. |
||
| 121 | /* |
||
| 122 | if ($nb > 0) { |
||
| 123 | setEventMessages($langs->trans('ContactAddedAutomatically', $nb), null, 'mesgs'); |
||
| 124 | } |
||
| 125 | */ |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | return 0; |
||
| 130 | } |
||
| 132 |