From 088220b60952b2c9a926b43d32e823b3d404a87d Mon Sep 17 00:00:00 2001 From: Henning <leutz@pcsg.de> Date: Sun, 23 Feb 2025 10:24:18 +0100 Subject: [PATCH] fix(phpunit): improve method existence checks in product handling code Extended the method existence checks throughout src/QUI/ERP/Order/Basket/ files. Improved the robustness of the code by ensuring that methods such as 'getUuid', 'getProductSetParentUuid', 'getQuantity', 'toArticle', and 'getAttributesForUniqueField' exist before calling them, thus preventing potential undefined method errors. Removed unnecessary comments used for type hinting in favor of functional checks. Related: quiqqer/order#172 --- src/QUI/ERP/Order/Basket/Basket.php | 29 ++++++++++++++++++------ src/QUI/ERP/Order/Basket/BasketGuest.php | 10 ++++---- src/QUI/ERP/Order/Basket/BasketOrder.php | 18 +++++++++++---- src/QUI/ERP/Order/Basket/Product.php | 5 ++-- 4 files changed, 44 insertions(+), 18 deletions(-) diff --git a/src/QUI/ERP/Order/Basket/Basket.php b/src/QUI/ERP/Order/Basket/Basket.php index 28dc0193..94a4eda1 100644 --- a/src/QUI/ERP/Order/Basket/Basket.php +++ b/src/QUI/ERP/Order/Basket/Basket.php @@ -237,10 +237,17 @@ public function save(): void $products = $this->List->getProducts(); foreach ($products as $Product) { - /* @var $Product Product */ + if ( + !method_exists($Product, 'getUuid') + || !method_exists($Product, 'getProductSetParentUuid') + || !method_exists($Product, 'getQuantity') + || !method_exists($Product, 'toArticle') + ) { + continue; + } + $fields = $Product->getFields(); - /* @var $Field UniqueField */ foreach ($fields as $Field) { $Field->setChangeableStatus(false); } @@ -255,7 +262,6 @@ public function save(): void 'fields' => [] ]; - /* @var $Field UniqueField */ foreach ($fields as $Field) { if ($Field->isCustomField()) { $productData['fields'][] = $Field->getAttributes(); @@ -293,11 +299,17 @@ public function toArray(): array $products = $Products->getProducts(); $result = []; - /* @var $Product Product */ foreach ($products as $Product) { + if ( + !method_exists($Product, 'getUuid') + || !method_exists($Product, 'getProductSetParentUuid') + || !method_exists($Product, 'getQuantity') + ) { + continue; + } + $fields = []; - /* @var $Field UniqueField */ foreach ($Product->getFields() as $Field) { if (!$Field->isPublic() && !$Field->isCustomField()) { continue; @@ -456,8 +468,11 @@ public function toOrder(QUI\ERP\Order\AbstractOrder $Order): void $Order->clear(); foreach ($products as $Product) { + if (!method_exists($Product, 'toArticle')) { + continue; + } + try { - /* @var QUI\ERP\Order\Basket\Product $Product */ $Order->addArticle($Product->toArticle(null, false)); } catch (QUI\Users\Exception $Exception) { QUI\System\Log::writeDebugException($Exception); @@ -503,7 +518,7 @@ protected function createNewOrder(): QUI\ERP\Order\OrderInProcess try { // select the last order in processing return $Orders->getLastOrderInProcessFromUser($User); - } catch (QUI\Erp\Order\Exception) { + } catch (QUI\ERP\Order\Exception) { } return QUI\ERP\Order\Factory::getInstance()->createOrderInProcess(); diff --git a/src/QUI/ERP/Order/Basket/BasketGuest.php b/src/QUI/ERP/Order/Basket/BasketGuest.php index 57d6ed3a..a96a05dd 100644 --- a/src/QUI/ERP/Order/Basket/BasketGuest.php +++ b/src/QUI/ERP/Order/Basket/BasketGuest.php @@ -84,13 +84,13 @@ public function addProduct(Product $Product): void //endregion - /** + /** * Import the products to the basket * * @param array|null $products * @throws ExceptionStack */ - public function import(array|null $products = []): void + public function import(array | null $products = []): void { $this->clear(); @@ -164,11 +164,13 @@ public function toArray(): array $products = $Products->getProducts(); $result = []; - /* @var $Product Product */ foreach ($products as $Product) { + if (!method_exists($Product, 'getQuantity')) { + continue; + } + $fields = []; - /* @var $Field UniqueField */ foreach ($Product->getFields() as $Field) { if (!$Field->isPublic()) { continue; diff --git a/src/QUI/ERP/Order/Basket/BasketOrder.php b/src/QUI/ERP/Order/Basket/BasketOrder.php index 790d4aa0..af632800 100644 --- a/src/QUI/ERP/Order/Basket/BasketOrder.php +++ b/src/QUI/ERP/Order/Basket/BasketOrder.php @@ -212,6 +212,16 @@ public function addProduct(Product $Product): void $foundProduct = false; foreach ($Products as $P) { + if ( + !method_exists($Product, 'toArray') + || !method_exists($Product, 'getQuantity') + || !method_exists($P, 'toArray') + || !method_exists($P, 'getQuantity') + || !method_exists($P, 'setQuantity') + ) { + continue; + } + $p1 = OrderProductUtils::getCompareProductArray($Product->toArray()); $p2 = OrderProductUtils::getCompareProductArray($P->toArray()); @@ -352,7 +362,6 @@ public function toArray(): array $products = $Products->getProducts(); $result = []; - /* @var $Product Product */ foreach ($products as $Product) { $fields = []; @@ -371,7 +380,7 @@ public function toArray(): array $attributes = [ 'id' => $Product->getId(), - 'quantity' => $Product->getQuantity(), + 'quantity' => method_exists($Product, 'getQuantity') ? $Product->getQuantity() : 1, 'fields' => $fields ]; @@ -457,8 +466,9 @@ public function updateOrder(): void foreach ($products as $Product) { try { - /* @var QUI\ERP\Order\Basket\Product $Product */ - $this->Order->addArticle($Product->toArticle(null, false)); + if (method_exists($Product, 'toArticle')) { + $this->Order->addArticle($Product->toArticle(null, false)); + } } catch (QUI\Users\Exception $Exception) { QUI\System\Log::writeDebugException($Exception); } diff --git a/src/QUI/ERP/Order/Basket/Product.php b/src/QUI/ERP/Order/Basket/Product.php index 5ce00569..96e7e704 100644 --- a/src/QUI/ERP/Order/Basket/Product.php +++ b/src/QUI/ERP/Order/Basket/Product.php @@ -90,9 +90,8 @@ public function __construct(int $pid, array $attributes = []) // set missing fields $productFields = $Product->getFields(); - /* @var $Field QUI\ERP\Products\Field\Field */ foreach ($productFields as $Field) { - if (!isset($fieldList[$Field->getId()])) { + if (!isset($fieldList[$Field->getId()]) && method_exists($Field, 'getAttributesForUniqueField')) { $fieldList[$Field->getId()] = $Field->getAttributesForUniqueField(); } } @@ -108,7 +107,7 @@ public function __construct(int $pid, array $attributes = []) * @param $fieldValue * @return null|QUI\ERP\Products\Field\Field|UniqueField */ - protected function importFieldData($fieldId, $fieldValue): QUI\ERP\Products\Field\Field|UniqueField|null + protected function importFieldData($fieldId, $fieldValue): QUI\ERP\Products\Field\Field | UniqueField | null { try { if (is_array($fieldValue) && isset($fieldValue['identifier'])) { -- GitLab