2
0
forked from Wavyzz/dolibarr

Clean code

This commit is contained in:
Laurent Destailleur
2024-08-31 15:08:17 +02:00
parent d1158716c7
commit ee6779dbcf
3 changed files with 30 additions and 9 deletions

View File

@@ -178,7 +178,7 @@ if (empty($reshook)) {
// Confirmation deactivation // Confirmation deactivation
if ($action == 'disable' && !empty($permissiontoadd)) { if ($action == 'disable' && $permissiontoadd) {
$object->fetch($id); $object->fetch($id);
if ($object->setstatus(0) < 0) { if ($object->setstatus(0) < 0) {
setEventMessages($object->error, $object->errors, 'errors'); setEventMessages($object->error, $object->errors, 'errors');
@@ -189,7 +189,7 @@ if (empty($reshook)) {
} }
// Confirmation activation // Confirmation activation
if ($action == 'enable' && !empty($permissiontoadd)) { if ($action == 'enable' && $permissiontoadd) {
$object->fetch($id); $object->fetch($id);
if ($object->setstatus(1) < 0) { if ($object->setstatus(1) < 0) {
setEventMessages($object->error, $object->errors, 'errors'); setEventMessages($object->error, $object->errors, 'errors');
@@ -200,7 +200,7 @@ if (empty($reshook)) {
} }
// Add contact // Add contact
if ($action == 'add' && !empty($permissiontoadd)) { if ($action == 'add' && $permissiontoadd) {
$db->begin(); $db->begin();
if ($canvas) { if ($canvas) {
@@ -337,7 +337,7 @@ if (empty($reshook)) {
} }
} }
if ($action == 'update' && empty($cancel) && !empty($permissiontoadd)) { if ($action == 'update' && empty($cancel) && $permissiontoadd) {
if (!GETPOST("lastname", 'alpha')) { if (!GETPOST("lastname", 'alpha')) {
$error++; $error++;
$errors = array($langs->trans("ErrorFieldRequired", $langs->transnoentities("Name").' / '.$langs->transnoentities("Label"))); $errors = array($langs->trans("ErrorFieldRequired", $langs->transnoentities("Name").' / '.$langs->transnoentities("Label")));
@@ -482,7 +482,7 @@ if (empty($reshook)) {
} }
} }
if ($action == 'setprospectcontactlevel' && !empty($permissiontoadd)) { if ($action == 'setprospectcontactlevel' && $permissiontoadd) {
$object->fetch($id); $object->fetch($id);
$object->fk_prospectlevel = GETPOST('prospect_contact_level_id', 'alpha'); $object->fk_prospectlevel = GETPOST('prospect_contact_level_id', 'alpha');
$result = $object->update($object->id, $user); $result = $object->update($object->id, $user);
@@ -492,7 +492,7 @@ if (empty($reshook)) {
} }
// set communication status // set communication status
if ($action == 'setstcomm' && !empty($permissiontoadd)) { if ($action == 'setstcomm' && $permissiontoadd) {
$object->fetch($id); $object->fetch($id);
$object->stcomm_id = dol_getIdFromCode($db, GETPOST('stcomm', 'alpha'), 'c_stcommcontact'); $object->stcomm_id = dol_getIdFromCode($db, GETPOST('stcomm', 'alpha'), 'c_stcommcontact');
$result = $object->update($object->id, $user); $result = $object->update($object->id, $user);
@@ -502,7 +502,7 @@ if (empty($reshook)) {
} }
// Update extrafields // Update extrafields
if ($action == "update_extras" && !empty($permissiontoadd)) { if ($action == "update_extras" && $permissiontoadd) {
$object->fetch(GETPOSTINT('id')); $object->fetch(GETPOSTINT('id'));
$attributekey = GETPOST('attribute', 'alpha'); $attributekey = GETPOST('attribute', 'alpha');

View File

@@ -241,7 +241,7 @@ class Workstation extends CommonObject
$id = $this->createCommon($user, $notrigger); $id = $this->createCommon($user, $notrigger);
// Usergroups // Usergroups
$groups = GETPOST('groups', 'array:int'); $groups = GETPOST('groups', 'array:int'); // FIXME We should not GETPOST but receive array as parameter
if (empty($groups)) { if (empty($groups)) {
$groups = $this->usergroups; // createFromClone $groups = $this->usergroups; // createFromClone
} }
@@ -256,7 +256,7 @@ class Workstation extends CommonObject
} }
// Resources // Resources
$resources = GETPOST('resources', 'array:int'); $resources = GETPOST('resources', 'array:int'); // FIXME We should not GETPOST but receive array as parameter
if (empty($resources)) { if (empty($resources)) {
$resources = $this->resources; // createFromClone $resources = $this->resources; // createFromClone
} }

View File

@@ -628,6 +628,27 @@ class CodingPhpTest extends CommonClassTest
break; break;
} }
$this->assertTrue($ok, 'Found a CURDATE\(\) in code. Do not use this SQL method in file '.$file['relativename'].'. You must use the PHP function dol_now() instead.'); $this->assertTrue($ok, 'Found a CURDATE\(\) in code. Do not use this SQL method in file '.$file['relativename'].'. You must use the PHP function dol_now() instead.');
// Test we don't have if ($action == 'xxx'... without test on permission
// We do not test on file into admin, protection is done on page on user->admin
if (!preg_match('/admin\//', $filecontent)
&& !preg_match('/\.tpl\.php/', $filecontent)
&& !preg_match('/\.lib\.php/', $filecontent)
&& !preg_match('/\.inc\.php/', $filecontent)
&& !preg_match('/\.class\.php/', $filecontent)) {
$ok = true;
$matches = array();
preg_match_all('/if\s*\(\s*\$action\s*==\s*[\'"][a-z]+[\'"].*/', $filecontent, $matches, PREG_SET_ORDER);
foreach ($matches as $key => $val) {
if (!preg_match('/\$user->hasR/', $val[0])) {
$ok = false;
print "Line: ".$val[0]."\n";
break;
}
}
$this->assertTrue($ok, 'Found a test on action without check on permission and without comment to say this is expected, in file '.$file['relativename'].'.');
}
} }