Clean code

This commit is contained in:
Laurent Destailleur 2021-09-27 15:50:19 +02:00
parent d4cc60640b
commit 80165811e0
5 changed files with 36 additions and 38 deletions

View File

@ -1696,21 +1696,21 @@ class Account extends CommonObject
/** /**
* Function used to replace a thirdparty id with another one. * Function used to replace a thirdparty id with another one.
* *
* @param DoliDB $db Database handler * @param DoliDB $dbs Database handler
* @param int $origin_id Old thirdparty id * @param int $origin_id Old thirdparty id
* @param int $dest_id New thirdparty id * @param int $dest_id New thirdparty id
* @return bool * @return bool True=SQL success, False=SQL error
*/ */
public static function replaceThirdparty($db, $origin_id, $dest_id) public static function replaceThirdparty($dbs, $origin_id, $dest_id)
{ {
$sql = "UPDATE ".MAIN_DB_PREFIX."bank_url SET url_id = ".((int) $dest_id)." WHERE url_id = ".((int) $origin_id)." AND type='company'"; $sql = "UPDATE ".MAIN_DB_PREFIX."bank_url SET url_id = ".((int) $dest_id)." WHERE url_id = ".((int) $origin_id)." AND type='company'";
if (!$db->query($sql)) { if ($dbs->query($sql)) {
//if ($ignoreerrors) return true; // TODO Not enough. If there is A-B on kept thirdarty and B-C on old one, we must get A-B-C after merge. Not A-B.
//$this->errors = $db->lasterror();
return false;
} else {
return true; return true;
} else {
//if ($ignoreerrors) return true; // TODO Not enough. If there is A-B on kept thirdarty and B-C on old one, we must get A-B-C after merge. Not A-B.
//$this->errors = $dbs->lasterror();
return false;
} }
} }
} }

View File

@ -83,7 +83,7 @@ class InfoBox
/** /**
* Return array of boxes qualified for area and user * Return array of boxes qualified for area and user
* *
* @param DoliDB $db Database handler * @param DoliDB $dbs Database handler
* @param string $mode 'available' or 'activated' * @param string $mode 'available' or 'activated'
* @param int $zone Name or area (-1 for all, 0 for Homepage, 1 for Accountancy, 2 for xxx, ...) * @param int $zone Name or area (-1 for all, 0 for Homepage, 1 for Accountancy, 2 for xxx, ...)
* @param User|null $user Object user to filter * @param User|null $user Object user to filter
@ -91,7 +91,7 @@ class InfoBox
* @param int $includehidden Include also hidden boxes * @param int $includehidden Include also hidden boxes
* @return array Array of boxes * @return array Array of boxes
*/ */
public static function listBoxes($db, $mode, $zone, $user = null, $excludelist = array(), $includehidden = 1) public static function listBoxes($dbs, $mode, $zone, $user = null, $excludelist = array(), $includehidden = 1)
{ {
global $conf; global $conf;
@ -119,12 +119,12 @@ class InfoBox
} }
dol_syslog(get_class()."::listBoxes get default box list for mode=".$mode." userid=".(is_object($user) ? $user->id : '')."", LOG_DEBUG); dol_syslog(get_class()."::listBoxes get default box list for mode=".$mode." userid=".(is_object($user) ? $user->id : '')."", LOG_DEBUG);
$resql = $db->query($sql); $resql = $dbs->query($sql);
if ($resql) { if ($resql) {
$num = $db->num_rows($resql); $num = $dbs->num_rows($resql);
$j = 0; $j = 0;
while ($j < $num) { while ($j < $num) {
$obj = $db->fetch_object($resql); $obj = $dbs->fetch_object($resql);
if (!in_array($obj->box_id, $excludelist)) { if (!in_array($obj->box_id, $excludelist)) {
$regs = array(); $regs = array();
@ -144,7 +144,7 @@ class InfoBox
// Goal is to avoid making a "new" done for each boxes returned by select. // Goal is to avoid making a "new" done for each boxes returned by select.
dol_include_once($relsourcefile); dol_include_once($relsourcefile);
if (class_exists($boxname)) { if (class_exists($boxname)) {
$box = new $boxname($db, $obj->note); // Constructor may set properties like box->enabled. obj->note is note into box def, not user params. $box = new $boxname($dbs, $obj->note); // Constructor may set properties like box->enabled. obj->note is note into box def, not user params.
//$box=new stdClass(); //$box=new stdClass();
// box properties // box properties
@ -204,8 +204,8 @@ class InfoBox
$j++; $j++;
} }
} else { } else {
dol_syslog($db->lasterror(), LOG_ERR); dol_syslog($dbs->lasterror(), LOG_ERR);
return array('error'=>$db->lasterror()); return array('error'=>$dbs->lasterror());
} }
return $boxes; return $boxes;
@ -215,13 +215,13 @@ class InfoBox
/** /**
* Save order of boxes for area and user * Save order of boxes for area and user
* *
* @param DoliDB $db Database handler * @param DoliDB $dbs Database handler
* @param int $zone Name of area (0 for Homepage, ...) * @param int $zone Name of area (0 for Homepage, ...)
* @param string $boxorder List of boxes with correct order 'A:123,456,...-B:789,321...' * @param string $boxorder List of boxes with correct order 'A:123,456,...-B:789,321...'
* @param int $userid Id of user * @param int $userid Id of user
* @return int <0 if KO, 0=Nothing done, > 0 if OK * @return int <0 if KO, 0=Nothing done, > 0 if OK
*/ */
public static function saveboxorder($db, $zone, $boxorder, $userid = 0) public static function saveboxorder($dbs, $zone, $boxorder, $userid = 0)
{ {
global $conf; global $conf;
@ -235,18 +235,18 @@ class InfoBox
return 0; return 0;
} }
$user = new User($db); $user = new User($dbs);
$user->id = $userid; $user->id = $userid;
$db->begin(); $dbs->begin();
// Save parameters to say user has a dedicated setup // Save parameters to say user has a dedicated setup
$tab = array(); $tab = array();
$confuserzone = 'MAIN_BOXES_'.$zone; $confuserzone = 'MAIN_BOXES_'.$zone;
$tab[$confuserzone] = 1; $tab[$confuserzone] = 1;
if (dol_set_user_param($db, $conf, $user, $tab) < 0) { if (dol_set_user_param($dbs, $conf, $user, $tab) < 0) {
$error = $db->lasterror(); $error = $dbs->lasterror();
$db->rollback(); $dbs->rollback();
return -3; return -3;
} }
@ -257,7 +257,7 @@ class InfoBox
$sql .= " AND position = ".((int) $zone); $sql .= " AND position = ".((int) $zone);
dol_syslog(get_class()."::saveboxorder", LOG_DEBUG); dol_syslog(get_class()."::saveboxorder", LOG_DEBUG);
$result = $db->query($sql); $result = $dbs->query($sql);
if ($result) { if ($result) {
$colonnes = explode('-', $boxorder); $colonnes = explode('-', $boxorder);
foreach ($colonnes as $collist) { foreach ($colonnes as $collist) {
@ -279,12 +279,12 @@ class InfoBox
$sql .= " values ("; $sql .= " values (";
$sql .= " ".((int) $id).","; $sql .= " ".((int) $id).",";
$sql .= " ".((int) $zone).","; $sql .= " ".((int) $zone).",";
$sql .= " '".$db->escape($colonne.$ii)."',"; $sql .= " '".$dbs->escape($colonne.$ii)."',";
$sql .= " ".((int) $userid).","; $sql .= " ".((int) $userid).",";
$sql .= " ".((int) $conf->entity); $sql .= " ".((int) $conf->entity);
$sql .= ")"; $sql .= ")";
$result = $db->query($sql); $result = $dbs->query($sql);
if ($result < 0) { if ($result < 0) {
$error++; $error++;
break; break;
@ -297,10 +297,10 @@ class InfoBox
} }
if ($error) { if ($error) {
$db->rollback(); $dbs->rollback();
return -2; return -2;
} else { } else {
$db->commit(); $dbs->commit();
return 1; return 1;
} }
} }

View File

@ -274,24 +274,24 @@ class Link extends CommonObject
/** /**
* Return nb of links * Return nb of links
* *
* @param DoliDb $db Database handler * @param DoliDb $dbs Database handler
* @param string $objecttype Type of the associated object in dolibarr * @param string $objecttype Type of the associated object in dolibarr
* @param int $objectid Id of the associated object in dolibarr * @param int $objectid Id of the associated object in dolibarr
* @return int Nb of links, -1 if error * @return int Nb of links, -1 if error
**/ **/
public static function count($db, $objecttype, $objectid) public static function count($dbs, $objecttype, $objectid)
{ {
global $conf; global $conf;
$sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."links"; $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."links";
$sql .= " WHERE objecttype = '".$db->escape($objecttype)."' AND objectid = ".((int) $objectid); $sql .= " WHERE objecttype = '".$dbs->escape($objecttype)."' AND objectid = ".((int) $objectid);
if ($conf->entity != 0) { if ($conf->entity != 0) {
$sql .= " AND entity = ".$conf->entity; $sql .= " AND entity = ".$conf->entity;
} }
$resql = $db->query($sql); $resql = $dbs->query($sql);
if ($resql) { if ($resql) {
$obj = $db->fetch_object($resql); $obj = $dbs->fetch_object($resql);
if ($obj) { if ($obj) {
return $obj->nb; return $obj->nb;
} }

View File

@ -437,6 +437,7 @@ class Productbatch extends CommonObject
public static function findAll($db, $fk_product_stock, $with_qty = 0, $fk_product = 0) public static function findAll($db, $fk_product_stock, $with_qty = 0, $fk_product = 0)
{ {
global $langs, $conf; global $langs, $conf;
$ret = array(); $ret = array();
$sql = "SELECT"; $sql = "SELECT";

View File

@ -205,8 +205,6 @@ class CodingPhpTest extends PHPUnit\Framework\TestCase
'conf.class.php', 'conf.class.php',
'html.form.class.php', 'html.form.class.php',
'html.formmail.class.php', 'html.formmail.class.php',
'infobox.class.php',
'link.class.php',
'translate.class.php', 'translate.class.php',
'utils.class.php', 'utils.class.php',
'modules_product.class.php', 'modules_product.class.php',
@ -215,8 +213,7 @@ class CodingPhpTest extends PHPUnit\Framework\TestCase
'multicurrency.class.php', 'multicurrency.class.php',
'productbatch.class.php', 'productbatch.class.php',
'reception.class.php', 'reception.class.php',
'societe.class.php' , 'societe.class.php'
'account.class.php'
))) { ))) {
// Must not found $db-> // Must not found $db->
$ok=true; $ok=true;
@ -337,7 +334,7 @@ class CodingPhpTest extends PHPUnit\Framework\TestCase
// Check string sql|set...'".$yyy->xxx with xxx that is not 'escape', 'idate', .... It means we forget a db->escape when forging sql request. // Check string sql|set...'".$yyy->xxx with xxx that is not 'escape', 'idate', .... It means we forget a db->escape when forging sql request.
preg_match_all('/(sql|SET|WHERE|INSERT|VALUES).+\s*\'"\s*\.\s*\$(.........)/', $filecontent, $matches, PREG_SET_ORDER); preg_match_all('/(sql|SET|WHERE|INSERT|VALUES).+\s*\'"\s*\.\s*\$(.........)/', $filecontent, $matches, PREG_SET_ORDER);
foreach ($matches as $key => $val) { foreach ($matches as $key => $val) {
if (! in_array($val[2], array('this->db-', 'this->esc', 'db->escap', 'mydb->esc', 'dbsession', 'db->idate', 'escapedli', 'excludeGr', 'includeGr'))) { if (! in_array($val[2], array('this->db-', 'this->esc', 'db->escap', 'dbs->esca', 'mydb->esc', 'dbsession', 'db->idate', 'escapedli', 'excludeGr', 'includeGr'))) {
$ok=false; $ok=false;
break; break;
} }