From 9491d880cfadbb4ed5f1002d3c3c8a44b68e39d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Sun, 9 Nov 2025 01:19:31 +0100 Subject: [PATCH] clean code (#36170) * clean code * Update bom.class.php * Update commondocgenerator.class.php * fix * fix * fix * fix * fix * fix --------- Co-authored-by: Laurent Destailleur --- htdocs/bom/bom_card.php | 33 +++++++++++-------- htdocs/bom/class/api_boms.class.php | 18 ++++++---- htdocs/bom/class/bom.class.php | 18 ++++++---- htdocs/categories/viewcat.php | 4 +++ .../core/class/commondocgenerator.class.php | 2 +- htdocs/core/class/commonobject.class.php | 6 ++-- test/phpunit/CommonClassTest.class.php | 2 +- 7 files changed, 50 insertions(+), 33 deletions(-) diff --git a/htdocs/bom/bom_card.php b/htdocs/bom/bom_card.php index 92a70caca1a..29679d068c6 100644 --- a/htdocs/bom/bom_card.php +++ b/htdocs/bom/bom_card.php @@ -404,18 +404,23 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea // Confirmation of validation if ($action == 'validate') { + $error = 0; + $numref = ''; // We check that object has a temporary ref $ref = substr($object->ref, 1, 4); if ($ref == 'PROV') { - $object->fetch_product(); - $numref = $object->getNextNumRef($object->product); + $res = $object->fetch_product(); + if ($res > 0 && $object->product instanceof Product) { + $numref = $object->getNextNumRef($object->product); // @phan-suppress-current-line PhanTypeMismatchArgumentNullable + } else { + $error++; + } } else { $numref = (string) $object->ref; } $text = $langs->trans('ConfirmValidateBom', $numref); - /*if (isModEnabled('notification')) - { + /*if (isModEnabled('notification')) { require_once DOL_DOCUMENT_ROOT . '/core/class/notify.class.php'; $notify = new Notify($db); $text .= '
'; @@ -431,15 +436,18 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea // array('type' => 'checkbox', 'name' => 'update_prices', 'label' => $langs->trans("PuttingPricesUpToDate"), 'value' => 1), ); } - - $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('Validate'), $text, 'confirm_validate', $formquestion, 0, 1, 220); + if (!$error) { + $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('Validate'), $text, 'confirm_validate', $formquestion, 0, 1, 220); + } else { + setEventMessage($langs->trans('Error'), 'errors'); + $action = ''; + } } // Confirmation of closing if ($action == 'close') { $text = $langs->trans('ConfirmCloseBom', $object->ref); - /*if (isModEnabled('notification')) - { + /*if (isModEnabled('notification')) { require_once DOL_DOCUMENT_ROOT . '/core/class/notify.class.php'; $notify = new Notify($db); $text .= '
'; @@ -462,8 +470,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea // Confirmation of reopen if ($action == 'reopen') { $text = $langs->trans('ConfirmReopenBom', $object->ref); - /*if (isModEnabled('notification')) - { + /*if (isModEnabled('notification')) { require_once DOL_DOCUMENT_ROOT . '/core/class/notify.class.php'; $notify = new Notify($db); $text .= '
'; @@ -524,12 +531,10 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea // Thirdparty $morehtmlref.='
'.$langs->trans('ThirdParty') . ' : ' . $soc->getNomUrl(1); // Project - if (isModEnabled('project')) - { + if (isModEnabled('project')) { $langs->load("projects"); $morehtmlref.='
'.$langs->trans('Project') . ' '; - if ($permissiontoadd) - { + if ($permissiontoadd) { if ($action != 'classify') $morehtmlref.='' . img_edit($langs->transnoentitiesnoconv('SetProject')) . ' : '; if ($action == 'classify') { diff --git a/htdocs/bom/class/api_boms.class.php b/htdocs/bom/class/api_boms.class.php index 20ce25bd279..dab41464782 100644 --- a/htdocs/bom/class/api_boms.class.php +++ b/htdocs/bom/class/api_boms.class.php @@ -1,7 +1,7 @@ +/* Copyright (C) 2015 Jean-François Ferry * Copyright (C) 2019 Maxime Kohlhaas - * Copyright (C) 2020-2024 Frédéric France + * Copyright (C) 2020-2025 Frédéric France * Copyright (C) 2022 Christian Humpel * Copyright (C) 2025 MDW * @@ -620,17 +620,21 @@ class Boms extends DolibarrApi private function checkRefNumbering() { $ref = substr($this->bom->ref, 1, 4); - if ($this->bom->status > 0 && $ref == 'PROV') { + if ($this->bom->status > BOM::STATUS_DRAFT && $ref == 'PROV') { throw new RestException(400, "Wrong naming scheme '(PROV%)' is only allowed on 'DRAFT' status. For automatic increment use 'auto' on the 'ref' field."); } if (strtolower($this->bom->ref) == 'auto') { - if (empty($this->bom->id) && $this->bom->status == 0) { + if (empty($this->bom->id) && $this->bom->status == BOM::STATUS_DRAFT) { $this->bom->ref = ''; // 'ref' will auto incremented with '(PROV' + newID + ')' } else { - $this->bom->fetch_product(); - $numref = $this->bom->getNextNumRef($this->bom->product); - $this->bom->ref = $numref; + $res = $this->bom->fetch_product(); + if ($res > 0 && $this->bom->product instanceof Product) { + $numref = $this->bom->getNextNumRef($this->bom->product); // @phan-suppress-current-line PhanTypeMismatchArgumentNullable + $this->bom->ref = $numref; + } else { + throw new RestException(400, "Error when generating automatic increment on the 'ref' field."); + } } } } diff --git a/htdocs/bom/class/bom.class.php b/htdocs/bom/class/bom.class.php index 640df709400..17a0cc0e245 100644 --- a/htdocs/bom/class/bom.class.php +++ b/htdocs/bom/class/bom.class.php @@ -1,9 +1,9 @@ - * Copyright (C) 2023 Benjamin Falière - * Copyright (C) 2023 Charlene Benke - * Copyright (C) 2024-2025 Frédéric France - * Copyright (C) 2024-2025 MDW +/* Copyright (C) 2019 Laurent Destailleur + * Copyright (C) 2023 Benjamin Falière + * Copyright (C) 2023 Charlene Benke + * Copyright (C) 2024-2025 Frédéric France + * Copyright (C) 2024-2025 MDW * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -67,7 +67,7 @@ class BOM extends CommonObject public $picto = 'bom'; /** - * @var Product Object product of the BOM + * @var ?Product Object product of the BOM */ public $product; @@ -940,7 +940,11 @@ class BOM extends CommonObject // Define new ref if (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref)) { // empty should not happened, but when it occurs, the test save life - $this->fetch_product(); + $res = $this->fetch_product(); + if ($res < 0 || !is_object($this->product)) { + $this->db->rollback(); + return -1; + } $num = $this->getNextNumRef($this->product); } else { $num = (string) $this->ref; diff --git a/htdocs/categories/viewcat.php b/htdocs/categories/viewcat.php index 23b2b1549c6..91f2ff5dc79 100644 --- a/htdocs/categories/viewcat.php +++ b/htdocs/categories/viewcat.php @@ -1373,6 +1373,8 @@ if ($type == Categorie::TYPE_FICHINTER) { if ($fichinters < 0) { dol_print_error($db, $object->error, $object->errors); } else { + /** @var Fichinter[] $fichinters */ + '@phan-var-force Fichinter[] $fichinters'; // Form to add record into a category if ($showclassifyform) { require_once DOL_DOCUMENT_ROOT.'/core/class/html.formintervention.class.php'; @@ -1460,6 +1462,8 @@ if ($type == Categorie::TYPE_ORDER) { if ($objects < 0) { dol_print_error($db, $object->error, $object->errors); } else { + /** @var Commande[] $objects */ + '@phan-var-force Commande[] $objects'; // Form to add record into a category $showclassifyform = $user->hasRight('order', 'write'); if ($showclassifyform) { diff --git a/htdocs/core/class/commondocgenerator.class.php b/htdocs/core/class/commondocgenerator.class.php index c01a8844507..1084a3afb2f 100644 --- a/htdocs/core/class/commondocgenerator.class.php +++ b/htdocs/core/class/commondocgenerator.class.php @@ -476,7 +476,7 @@ abstract class CommonDocGenerator * Define array with couple substitution key => substitution value * For example {company_name}, {company_name_alias} * - * @param Societe $object Object + * @param ?Societe $object Object * @param Translate $outputlangs Language object for output * @param string $array_key Name of the key for return array * @return array Array of substitution key->code diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 1b306e41960..7da2b02687d 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -304,19 +304,19 @@ abstract class CommonObject public $contact_id; /** - * @var Societe|null A related thirdparty object + * @var ?Societe A related thirdparty object * @see fetch_thirdparty() */ public $thirdparty; /** - * @var User A related user + * @var ?User A related user * @see fetch_user() */ public $user; /** - * @var Product Populated by fetch_product() + * @var ?Product Populated by fetch_product() * @see fetch_product() */ public $product; diff --git a/test/phpunit/CommonClassTest.class.php b/test/phpunit/CommonClassTest.class.php index 3ea4940fa92..f249b1f5ffa 100644 --- a/test/phpunit/CommonClassTest.class.php +++ b/test/phpunit/CommonClassTest.class.php @@ -2,7 +2,7 @@ /* Copyright (C) 2018 Laurent Destailleur * Copyright (C) 2023 Alexandre Janniaux * Copyright (C) 2024-2025 MDW - * Copyright (C) 2024 Frédéric France + * Copyright (C) 2024-2025 Frédéric France * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by