Skip to content
Code-Schnipsel Gruppen Projekte
Commit 088220b6 erstellt von Henning Leutz's avatar Henning Leutz :martial_arts_uniform:
Dateien durchsuchen

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: #172
Übergeordneter fdc7309a
No related branches found
No related tags found
2 Merge Requests!146fix: update `getCustomerFiles` method signature in `AbstractOrder`,!145Update 'next-3.x' with latest changes from 'main'
Pipeline #15531 fehlgeschlagen mit Phase
in 2 Minuten und 22 Sekunden
......@@ -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();
......
......@@ -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;
......
......@@ -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);
}
......
......@@ -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'])) {
......
0% oder .
You are about to add 0 people to the discussion. Proceed with caution.
Bearbeitung dieser Nachricht zuerst beenden!
Bitte registrieren oder zum Kommentieren