From ff7a684a0a2b82b945aeaebf795f123c4bc740d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Sat, 1 Nov 2025 17:48:18 +0100 Subject: [PATCH] clean code (#36054) * clean code * clean code * avoid obj to be false * clean code * clean code * clean code * clean code * clean code * clean code * clean code --- dev/build/phpstan/phpstan-baseline.neon | 78 ------------------- htdocs/product/class/product.class.php | 6 +- htdocs/product/class/productbatch.class.php | 7 +- .../stock/class/mouvementstock.class.php | 31 ++++---- .../product/stock/class/productlot.class.php | 18 ++--- .../class/productstockentrepot.class.php | 4 +- 6 files changed, 31 insertions(+), 113 deletions(-) diff --git a/dev/build/phpstan/phpstan-baseline.neon b/dev/build/phpstan/phpstan-baseline.neon index 8b57e485aad..2fcbb8c00b4 100644 --- a/dev/build/phpstan/phpstan-baseline.neon +++ b/dev/build/phpstan/phpstan-baseline.neon @@ -11880,30 +11880,12 @@ parameters: count: 1 path: ../../../htdocs/product/class/productbatch.class.php - - - message: '#^Property Productbatch\:\:\$batch \(string\) in isset\(\) is not nullable\.$#' - identifier: isset.property - count: 3 - path: ../../../htdocs/product/class/productbatch.class.php - - - - message: '#^Property Productbatch\:\:\$eatby \(int\|string\) in isset\(\) is not nullable\.$#' - identifier: isset.property - count: 1 - path: ../../../htdocs/product/class/productbatch.class.php - - message: '#^Property Productbatch\:\:\$qty \(float\) in isset\(\) is not nullable\.$#' identifier: isset.property count: 3 path: ../../../htdocs/product/class/productbatch.class.php - - - message: '#^Property Productbatch\:\:\$sellby \(int\|string\) in isset\(\) is not nullable\.$#' - identifier: isset.property - count: 1 - path: ../../../htdocs/product/class/productbatch.class.php - - message: '#^Loose comparison using \=\= between '''' and 0 will always evaluate to false\.$#' identifier: equal.alwaysFalse @@ -12492,66 +12474,12 @@ parameters: count: 1 path: ../../../htdocs/product/stock/class/productlot.class.php - - - message: '#^Property Productlot\:\:\$batch \(string\) in isset\(\) is not nullable\.$#' - identifier: isset.property - count: 4 - path: ../../../htdocs/product/stock/class/productlot.class.php - - - - message: '#^Property Productlot\:\:\$eatby \(int\|string\) in isset\(\) is not nullable\.$#' - identifier: isset.property - count: 2 - path: ../../../htdocs/product/stock/class/productlot.class.php - - - - message: '#^Property Productlot\:\:\$eol_date \(int\|string\) in isset\(\) is not nullable\.$#' - identifier: isset.property - count: 2 - path: ../../../htdocs/product/stock/class/productlot.class.php - - message: '#^Property Productlot\:\:\$fk_product \(int\) in isset\(\) is not nullable\.$#' identifier: isset.property count: 4 path: ../../../htdocs/product/stock/class/productlot.class.php - - - message: '#^Property Productlot\:\:\$fk_user_creat \(int\) in isset\(\) is not nullable\.$#' - identifier: isset.property - count: 4 - path: ../../../htdocs/product/stock/class/productlot.class.php - - - - message: '#^Property Productlot\:\:\$fk_user_modif \(int\) in isset\(\) is not nullable\.$#' - identifier: isset.property - count: 4 - path: ../../../htdocs/product/stock/class/productlot.class.php - - - - message: '#^Property Productlot\:\:\$import_key \(string\) in isset\(\) is not nullable\.$#' - identifier: isset.property - count: 4 - path: ../../../htdocs/product/stock/class/productlot.class.php - - - - message: '#^Property Productlot\:\:\$manufacturing_date \(int\|string\) in isset\(\) is not nullable\.$#' - identifier: isset.property - count: 2 - path: ../../../htdocs/product/stock/class/productlot.class.php - - - - message: '#^Property Productlot\:\:\$scrapping_date \(int\|string\) in isset\(\) is not nullable\.$#' - identifier: isset.property - count: 2 - path: ../../../htdocs/product/stock/class/productlot.class.php - - - - message: '#^Property Productlot\:\:\$sellby \(int\|string\) in isset\(\) is not nullable\.$#' - identifier: isset.property - count: 2 - path: ../../../htdocs/product/stock/class/productlot.class.php - - message: '#^Negated boolean expression is always true\.$#' identifier: booleanNot.alwaysTrue @@ -12570,12 +12498,6 @@ parameters: count: 4 path: ../../../htdocs/product/stock/class/productstockentrepot.class.php - - - message: '#^Property ProductStockEntrepot\:\:\$import_key \(string\) in isset\(\) is not nullable\.$#' - identifier: isset.property - count: 4 - path: ../../../htdocs/product/stock/class/productstockentrepot.class.php - - message: '#^Variable \$action might not be defined\.$#' identifier: variable.undefined diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index 8f6dcfa752a..e01246c1dfc 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -6218,9 +6218,9 @@ class Product extends CommonObject * @param int<0,1> $movement 0 = add, 1 = remove * @param string $label Label of stock movement * @param int|float $price Price to use for stock eval - * @param int|string $dlc eat-by date - * @param int|string $dluo sell-by date - * @param string $lot Lot number + * @param int|''|null $dlc eat-by date + * @param int|''|null $dluo sell-by date + * @param string|null $lot Lot number * @param string $inventorycode Inventory code * @param string $origin_element Origin element type * @param ?int $origin_id Origin id of element diff --git a/htdocs/product/class/productbatch.class.php b/htdocs/product/class/productbatch.class.php index 8f3ef7185f6..20e117907eb 100644 --- a/htdocs/product/class/productbatch.class.php +++ b/htdocs/product/class/productbatch.class.php @@ -55,7 +55,7 @@ class Productbatch extends CommonObject public $fk_product_stock; /** - * @var string batch number + * @var null|string batch number */ public $batch = ''; @@ -81,12 +81,13 @@ class Productbatch extends CommonObject public $lotid; /** - * @var int|'' + * @var int|''|null * @deprecated */ public $sellby = ''; // dlc + /** - * @var int|'' + * @var int|''|null * @deprecated */ public $eatby = ''; // dmd/dluo diff --git a/htdocs/product/stock/class/mouvementstock.class.php b/htdocs/product/stock/class/mouvementstock.class.php index 33041df8645..4572ab7380f 100644 --- a/htdocs/product/stock/class/mouvementstock.class.php +++ b/htdocs/product/stock/class/mouvementstock.class.php @@ -211,7 +211,7 @@ class MouvementStock extends CommonObject * @param int|string $datem Force date of movement * @param int|'' $eatby eat-by date. Will be used if lot does not exists yet and will be created. * @param int|'' $sellby sell-by date. Will be used if lot does not exists yet and will be created. - * @param string $batch batch number + * @param string|null $batch batch number * @param bool $skip_batch If set to true, stock movement is done without impacting batch record * @param int $id_product_batch Id product_batch (when skip_batch is false and we already know which record of product_batch table to use) * @param int<0,1> $disablestockchangeforsubproduct Disable stock change for sub-products of kit (useful only if product is a kit) @@ -362,8 +362,7 @@ class MouvementStock extends CommonObject $num = $this->db->num_rows($resql); $i = 0; if ($num > 0) { - while ($i < $num) { - $obj = $this->db->fetch_object($resql); + while ($i < $num && $obj = $this->db->fetch_object($resql)) { if ($obj->eatby) { if ($eatby) { $tmparray = dol_getdate((int) $eatby, true); @@ -386,8 +385,7 @@ class MouvementStock extends CommonObject $productlot->eatby = $eatby; $result = $productlot->update($user); if ($result <= 0) { - $this->error = $productlot->error; - $this->errors = $productlot->errors; + $this->setErrorsFromObject($productlot); $this->db->rollback(); return -5; } @@ -414,8 +412,7 @@ class MouvementStock extends CommonObject $productlot->sellby = $sellby; $result = $productlot->update($user); if ($result <= 0) { - $this->error = $productlot->error; - $this->errors = $productlot->errors; + $this->setErrorsFromObject($productlot); $this->db->rollback(); return -5; } @@ -436,8 +433,7 @@ class MouvementStock extends CommonObject $productlot->sellby = $sellby; $result = $productlot->create($user); if ($result <= 0) { - $this->error = $productlot->error; - $this->errors = $productlot->errors; + $this->setErrorsFromObject($productlot); $this->db->rollback(); return -4; } @@ -475,8 +471,8 @@ class MouvementStock extends CommonObject $tmpwarehouse = new Entrepot($this->db); $tmpwarehouse->fetch($entrepot_id); - $this->error = $langs->trans('qtyToTranferLotIsNotEnough', $product->ref, $batch, $qtyisnotenough, $tmpwarehouse->ref); - $this->errors[] = $langs->trans('qtyToTranferLotIsNotEnough', $product->ref, $batch, $qtyisnotenough, $tmpwarehouse->ref); + $this->error = $langs->trans('qtyToTranferLotIsNotEnough', $product->ref, (string) $batch, $qtyisnotenough, $tmpwarehouse->ref); + $this->errors[] = $langs->trans('qtyToTranferLotIsNotEnough', $product->ref, (string) $batch, $qtyisnotenough, $tmpwarehouse->ref); $this->db->rollback(); return -8; } @@ -513,7 +509,7 @@ class MouvementStock extends CommonObject $sql .= " fk_entrepot, value, type_mouvement, fk_user_author, label, inventorycode, price, fk_origin, origintype, fk_projet"; $sql .= ")"; $sql .= " VALUES ('".$this->db->idate($this->datem)."', ".((int) $this->product_id).", "; - $sql .= " ".($batch ? "'".$this->db->escape($batch)."'" : "null").", "; + $sql .= " ".(isset($batch) ? "'".$this->db->escape($batch)."'" : "null").", "; $sql .= " ".($eatby ? "'".$this->db->idate($eatby)."'" : "null").", "; $sql .= " ".($sellby ? "'".$this->db->idate($sellby)."'" : "null").", "; $sql .= " ".((int) $this->entrepot_id).", ".((float) $this->qty).", ".((int) $this->type).","; @@ -621,11 +617,11 @@ class MouvementStock extends CommonObject if ($id_product_batch > 0) { $result = $this->createBatch($id_product_batch, $qty); if ($result == -2 && $fk_product_stock > 0) { // The entry for this product batch does not exists anymore, bu we already have a llx_product_stock, so we recreate the batch entry in product_batch - $param_batch = array('fk_product_stock' => $fk_product_stock, 'batchnumber' => $batch); + $param_batch = array('fk_product_stock' => $fk_product_stock, 'batchnumber' => (string) $batch); $result = $this->createBatch($param_batch, $qty); } } else { - $param_batch = array('fk_product_stock' => $fk_product_stock, 'batchnumber' => $batch); + $param_batch = array('fk_product_stock' => $fk_product_stock, 'batchnumber' => (string) $batch); $result = $this->createBatch($param_batch, $qty); } if ($result < 0) { @@ -678,9 +674,9 @@ class MouvementStock extends CommonObject // Check unicity for serial numbered equipment once all movement were done. if (!$error && isModEnabled('productbatch') && $product->hasbatch() && !$skip_batch) { if ($product->status_batch == 2 && $qty > 0) { // We check only if we increased qty - if ($this->getBatchCount($fk_product, $batch) > 1) { + if ($this->getBatchCount($fk_product, (string) $batch) > 1) { $error++; - $this->errors[] = $langs->trans("TooManyQtyForSerialNumber", $product->ref, $batch); + $this->errors[] = $langs->trans("TooManyQtyForSerialNumber", $product->ref, (string) $batch); } } } @@ -984,8 +980,7 @@ class MouvementStock extends CommonObject $result = $pdluo->create($user, 1); if ($result < 0) { - $this->error = $pdluo->error; - $this->errors = $pdluo->errors; + $this->setErrorsFromObject($pdluo); } } } diff --git a/htdocs/product/stock/class/productlot.class.php b/htdocs/product/stock/class/productlot.class.php index 2f4d9d30626..f51eec1c97d 100644 --- a/htdocs/product/stock/class/productlot.class.php +++ b/htdocs/product/stock/class/productlot.class.php @@ -174,32 +174,32 @@ class Productlot extends CommonObject public $fk_product; /** - * @var string batch ref + * @var ?string batch ref */ public $batch; /** - * @var int|string eatby + * @var int|''|null eatby */ public $eatby = ''; /** - * @var int|string sellby + * @var int|''|null sellby */ public $sellby = ''; /** - * @var int|'' eol_date + * @var int|''|null eol_date */ public $eol_date = ''; /** - * @var int|'' manufacturing_date + * @var int|''|null manufacturing_date */ public $manufacturing_date = ''; /** - * @var int|'' scrapping_date + * @var int|''|null scrapping_date */ public $scrapping_date = ''; //public $commissionning_date = ''; @@ -217,17 +217,17 @@ class Productlot extends CommonObject public $datec = ''; /** - * @var int user ID + * @var int|null user ID */ public $fk_user_creat; /** - * @var int user ID + * @var int|null user ID */ public $fk_user_modif; /** - * @var string import key + * @var null|string import key */ public $import_key; diff --git a/htdocs/product/stock/class/productstockentrepot.class.php b/htdocs/product/stock/class/productstockentrepot.class.php index df9df57846a..dec70ffccb7 100644 --- a/htdocs/product/stock/class/productstockentrepot.class.php +++ b/htdocs/product/stock/class/productstockentrepot.class.php @@ -3,7 +3,7 @@ * Copyright (C) 2014-2016 Juanjo Menent * Copyright (C) 2015 Florian Henry * Copyright (C) 2015 Raphaël Doursenaud - * Copyright (C) 2018-2024 Frédéric France + * Copyright (C) 2018-2025 Frédéric France * Copyright (C) 2024 MDW * * This program is free software; you can redistribute it and/or modify @@ -68,7 +68,7 @@ class ProductStockEntrepot extends CommonObject */ public $desiredstock; /** - * @var string + * @var null|string */ public $import_key;