From 0e79fa36ee101e0b3ea9fb35d134017754b403b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Thu, 11 Sep 2025 12:14:53 +0200 Subject: [PATCH] clean code contact class (#35260) * clean code contact class * clean code contact class * clean code * clean code * clean code * clean code * clean code * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix --- dev/build/phpstan/phpstan-baseline.neon | 56 +--------------------- htdocs/contact/class/contact.class.php | 63 +++++++++++-------------- htdocs/societe/class/societe.class.php | 2 +- 3 files changed, 30 insertions(+), 91 deletions(-) diff --git a/dev/build/phpstan/phpstan-baseline.neon b/dev/build/phpstan/phpstan-baseline.neon index e4f13da55c9..b1ad19d6bc2 100644 --- a/dev/build/phpstan/phpstan-baseline.neon +++ b/dev/build/phpstan/phpstan-baseline.neon @@ -5034,60 +5034,6 @@ parameters: count: 1 path: ../../../htdocs/contact/card.php - - - message: '#^Call to function method_exists\(\) with \$this\(Contact\) and ''getLibStatut'' will always evaluate to true\.$#' - identifier: function.alreadyNarrowedType - count: 1 - path: ../../../htdocs/contact/class/contact.class.php - - - - message: '#^Call to function method_exists\(\) with \$this\(Contact\) and ''getNomUrl'' will always evaluate to true\.$#' - identifier: function.alreadyNarrowedType - count: 1 - path: ../../../htdocs/contact/class/contact.class.php - - - - message: '#^Call to function property_exists\(\) with \$this\(Contact\) and ''photo'' will always evaluate to true\.$#' - identifier: function.alreadyNarrowedType - count: 1 - path: ../../../htdocs/contact/class/contact.class.php - - - - message: '#^Call to function property_exists\(\) with \$this\(Contact\) and ''thirdparty'' will always evaluate to true\.$#' - identifier: function.alreadyNarrowedType - count: 1 - path: ../../../htdocs/contact/class/contact.class.php - - - - message: '#^Left side of && is always true\.$#' - identifier: booleanAnd.leftAlwaysTrue - count: 1 - path: ../../../htdocs/contact/class/contact.class.php - - - - message: '#^Negated boolean expression is always true\.$#' - identifier: booleanNot.alwaysTrue - count: 3 - path: ../../../htdocs/contact/class/contact.class.php - - - - message: '#^Property Contact\:\:\$roles \(array\\>\) in isset\(\) is not nullable\.$#' - identifier: isset.property - count: 1 - path: ../../../htdocs/contact/class/contact.class.php - - - - message: '#^Property Contact\:\:\$stcomm_id \(int\) in isset\(\) is not nullable\.$#' - identifier: isset.property - count: 1 - path: ../../../htdocs/contact/class/contact.class.php - - - - message: '#^Variable \$url in empty\(\) always exists and is not falsy\.$#' - identifier: empty.variable - count: 2 - path: ../../../htdocs/contact/class/contact.class.php - - message: '#^Loose comparison using \=\= between 0 and 1 will always evaluate to false\.$#' identifier: equal.alwaysFalse @@ -18571,7 +18517,7 @@ parameters: path: ../../../htdocs/societe/class/societe.class.php - - message: '#^Property Contact\:\:\$roles \(array\\) does not accept non\-empty\-list\\.$#' + message: '#^Property Contact\:\:\$roles \(array\|null\) does not accept non\-empty\-list\\.$#' identifier: assign.propertyType count: 1 path: ../../../htdocs/societe/class/societe.class.php diff --git a/htdocs/contact/class/contact.class.php b/htdocs/contact/class/contact.class.php index 9a17e80a3f1..ff4cacd08bb 100644 --- a/htdocs/contact/class/contact.class.php +++ b/htdocs/contact/class/contact.class.php @@ -346,7 +346,7 @@ class Contact extends CommonObject // END MODULEBUILDER PROPERTIES /** - * @var array roles + * @var null|array roles, null until fetched or set */ public $roles; @@ -361,7 +361,7 @@ class Contact extends CommonObject public $fk_prospectlevel; /** - * @var int + * @var null|int Is null until fetched or set */ public $stcomm_id; @@ -558,12 +558,10 @@ class Contact extends CommonObject if ($resql) { $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."socpeople"); - if (!$error) { - $result = $this->update($this->id, $user, 1, 'add'); // This include updateRoles(), ... - if ($result < 0) { - $error++; - $this->error = $this->db->lasterror(); - } + $result = $this->update($this->id, $user, 1, 'add'); // This include updateRoles(), ... + if ($result < 0) { + $error++; + $this->error = $this->db->lasterror(); } if (!$error) { @@ -702,11 +700,9 @@ class Contact extends CommonObject $action = 'update'; // Actions on extra fields - if (!$error) { - $result = $this->insertExtraFields(); - if ($result < 0) { - $error++; - } + $result = $this->insertExtraFields(); + if ($result < 0) { + $error++; } if (!$error) { @@ -1078,13 +1074,13 @@ class Contact extends CommonObject } elseif ($num) { // $num = 1 $obj = $this->db->fetch_object($resql); - $this->id = $obj->rowid; - $this->entity = $obj->entity; - $this->ref = $obj->rowid; - $this->ref_ext = $obj->ref_ext; + $this->id = $obj->rowid; + $this->entity = $obj->entity; + $this->ref = $obj->rowid; + $this->ref_ext = $obj->ref_ext; - $this->civility_code = $obj->civility_code; - $this->civility = $obj->civility_code ? ($langs->trans("Civility".$obj->civility_code) != "Civility".$obj->civility_code ? $langs->trans("Civility".$obj->civility_code) : $obj->civility_code) : ''; + $this->civility_code = $obj->civility_code; + $this->civility = $obj->civility_code ? ($langs->trans("Civility".$obj->civility_code) != "Civility".$obj->civility_code ? $langs->trans("Civility".$obj->civility_code) : $obj->civility_code) : ''; $this->name_alias = $obj->name_alias; $this->lastname = $obj->lastname; @@ -1093,7 +1089,7 @@ class Contact extends CommonObject $this->zip = $obj->zip; $this->town = $obj->town; - $this->date_creation = $this->db->jdate($obj->date_creation); + $this->date_creation = $this->db->jdate($obj->date_creation); $this->date_modification = $this->db->jdate($obj->date_modification); $this->user_creation_id = $obj->fk_user_creat; $this->user_modification_id = $obj->fk_user_modif; @@ -1121,10 +1117,10 @@ class Contact extends CommonObject $this->statut_commercial = $libelle; // libelle statut commercial $this->stcomm_picto = $obj->stcomm_picto; // Picto statut commercial - $this->phone_pro = trim($obj->phone); - $this->fax = trim($obj->fax); - $this->phone_perso = trim($obj->phone_perso); - $this->phone_mobile = trim($obj->phone_mobile); + $this->phone_pro = trim($obj->phone); + $this->fax = trim($obj->fax); + $this->phone_perso = trim($obj->phone_perso); + $this->phone_mobile = trim($obj->phone_mobile); $this->email = $obj->email; $this->socialnetworks = ($obj->socialnetworks ? (array) json_decode($obj->socialnetworks, true) : array()); @@ -1317,7 +1313,7 @@ class Contact extends CommonObject $this->db->begin(); - if (!$error && !$notrigger) { + if (!$notrigger) { // Call trigger $result = $this->call_trigger('CONTACT_DELETE', $user); if ($result < 0) { @@ -1576,7 +1572,7 @@ class Contact extends CommonObject if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) { $add_save_lastsearch_values = 1; } - if ($url && $add_save_lastsearch_values) { + if ($add_save_lastsearch_values) { $url .= '&save_lastsearch_values=1'; } } @@ -1595,13 +1591,13 @@ class Contact extends CommonObject $linkclose = ($morecss ? ' class="'.$morecss.'"' : ''); } - if ($option == 'nolink' || empty($url)) { + if ($option == 'nolink') { $linkstart = ''; - if ($option == 'nolink' || empty($url)) { + if ($option == 'nolink') { $linkend = ''; } else { $linkend = ''; @@ -2271,19 +2267,18 @@ class Contact extends CommonObject $return = '
'; $return .= '
'; $return .= ''; - //var_dump($this->photo);exit; - if (property_exists($this, 'photo') && !is_null($this->photo)) { + if (!is_null($this->photo)) { $return .= Form::showphoto('contact', $this, 0, 60, 0, 'photokanban photoref photowithmargin photologintooltip', 'small', 0, 1); } else { $return .= img_picto('', $this->picto); } $return .= ''; $return .= '
'; - $return .= '
'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(0) : $this->ref).'
'; + $return .= '
' . $this->getNomUrl(0) . '
'; if ($selected >= 0) { $return .= ''; } - if (property_exists($this, 'thirdparty') && is_object($this->thirdparty)) { + if (is_object($this->thirdparty)) { $return .= '
'.$this->thirdparty->getNomUrl(1).'
'; } /*if (property_exists($this, 'phone_pro') && !empty($this->phone_pro)) { @@ -2294,9 +2289,7 @@ class Contact extends CommonObject $return .= '
'.$langs->trans("Visibility").''; $return .= ' : '.$this->LibPubPriv($this->priv).''; }*/ - if (method_exists($this, 'getLibStatut')) { - $return .= '
'.$this->getLibStatut(3).'
'; - } + $return .= '
'.$this->getLibStatut(3).'
'; $return .= '
'; $return .= '
'; $return .= '
'; diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index bc42d50d81e..94f322af753 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -1215,7 +1215,7 @@ class Societe extends CommonObject $contact->zip = $this->zip; $contact->town = $this->town; $this->setUpperOrLowerCase(); - $contact->phone_pro = $this->phone; + $contact->phone_pro = $this->phone; if (getDolGlobalString('CONTACTS_DEFAULT_ROLES')) { $contact->roles = explode(',', getDolGlobalString('CONTACTS_DEFAULT_ROLES')); }