From e8a56bf244b851fef34ef04db61a039aabd6ad37 Mon Sep 17 00:00:00 2001
From: Henning <leutz@pcsg.de>
Date: Sun, 23 Feb 2025 09:06:42 +0100
Subject: [PATCH] fix(phpstan): better error handling and class usage

This commit fixes several areas:

1. It changes class type in BasketOrder from QUI\\Users\\User to QUI\\Interfaces\\Users\\User.

2. It refactors Order to better handle if statements and moves verification of class existence up
to the beginning. Fix involves assuming that function getInvoiceAddress and getDeliveryAddress
always return a non-null value. It also updates on how we check class exists for Shipping data.

3. In OrderInProcess, now we check if method 'toArticle' exists on the Product object before
calling it and applies similar class checking like in Order for Shipping data.

4. Corrects and standardizes the casing for exception catching in OrderProcess.

In summary, these changes lead to a safer and cleaner code base by addressing potential Null
Pointer Exceptions and ensuring the existence of classes before they are being called.

Related: quiqqer/order#172
---
 src/QUI/ERP/Order/Basket/BasketOrder.php |  2 +-
 src/QUI/ERP/Order/Order.php              | 26 ++++++++++--------------
 src/QUI/ERP/Order/OrderInProcess.php     | 19 +++++++++--------
 src/QUI/ERP/Order/OrderProcess.php       |  5 +++--
 4 files changed, 26 insertions(+), 26 deletions(-)

diff --git a/src/QUI/ERP/Order/Basket/BasketOrder.php b/src/QUI/ERP/Order/Basket/BasketOrder.php
index 469f1f63..790d4aa0 100644
--- a/src/QUI/ERP/Order/Basket/BasketOrder.php
+++ b/src/QUI/ERP/Order/Basket/BasketOrder.php
@@ -61,7 +61,7 @@ class BasketOrder
      * Basket constructor.
      *
      * @param string $orderHash - ID of the order
-     * @param ?QUI\Users\User $User
+     * @param ?QUI\Interfaces\Users\User $User
      *
      * @throws Exception
      * @throws QUI\Exception
diff --git a/src/QUI/ERP/Order/Order.php b/src/QUI/ERP/Order/Order.php
index f0fb2e48..18f736a4 100644
--- a/src/QUI/ERP/Order/Order.php
+++ b/src/QUI/ERP/Order/Order.php
@@ -216,10 +216,6 @@ public function createInvoice(
 
         // set the data to the temporary invoice
         $payment = '';
-
-        $invoiceAddress = '';
-        $invoiceAddressId = '';
-
         $deliveryAddress = '';
         $deliveryAddressId = '';
 
@@ -227,16 +223,14 @@ public function createInvoice(
             $payment = $this->getPayment()->getId();
         }
 
-        if ($this->getInvoiceAddress()) {
-            $invoiceAddress = $this->getInvoiceAddress()->toJSON();
-            $invoiceAddressId = $this->getInvoiceAddress()->getUUID();
-        }
+        $invoiceAddress = $this->getInvoiceAddress()->toJSON();
+        $invoiceAddressId = $this->getInvoiceAddress()->getUUID();
 
         if (empty($invoiceAddressId)) {
             $invoiceAddressId = $this->getCustomer()->getStandardAddress()->getUUID();
         }
 
-        if ($this->getDeliveryAddress()) {
+        if ($this->getDeliveryAddress()->getUUID()) {
             $deliveryAddress = $this->getDeliveryAddress()->toJSON();
             $deliveryAddressId = $this->getDeliveryAddress()->getUUID();
 
@@ -579,16 +573,18 @@ class_exists('QUI\ERP\Accounting\Invoice\Invoice')
         $shippingData = '';
         $shippingStatus = null;
 
-        $Shipping = $this->getShipping();
+        if (class_exists('QUI\ERP\Shipping\Types\ShippingEntry')) {
+            $Shipping = $this->getShipping();
 
-        if ($Shipping) {
-            $shippingId = $Shipping->getId();
-            $shippingData = $Shipping->toJSON();
+            if ($Shipping) {
+                $shippingId = $Shipping->getId();
+                $shippingData = $Shipping->toJSON();
+            }
         }
 
-        if (QUI::getPackageManager()->isInstalled('quiqqer/shipping')) {
+        if (class_exists('QUI\ERP\Shipping\ShippingStatus\Status')) {
             $ShippingStatus = $this->getShippingStatus();
-            $shippingStatus = $ShippingStatus ? $ShippingStatus->getId() : null;
+            $shippingStatus = $ShippingStatus?->getId();
         }
 
         // project name
diff --git a/src/QUI/ERP/Order/OrderInProcess.php b/src/QUI/ERP/Order/OrderInProcess.php
index f654ccdd..3a20cc52 100644
--- a/src/QUI/ERP/Order/OrderInProcess.php
+++ b/src/QUI/ERP/Order/OrderInProcess.php
@@ -246,8 +246,9 @@ public function addPriceFactors(array $priceFactors = []): void
 
         foreach ($products as $Product) {
             try {
-                /* @var QUI\ERP\Order\Basket\Product $Product */
-                $ArticleList->addArticle($Product->toArticle(null, false));
+                if (method_exists($Product, 'toArticle')) {
+                    $ArticleList->addArticle($Product->toArticle(null, false));
+                }
             } catch (Exception $Exception) {
                 QUI\System\Log::writeDebugException($Exception);
             }
@@ -640,16 +641,18 @@ protected function getDataForSaving(): array
         $shippingData = '';
         $shippingStatus = null;
 
-        $Shipping = $this->getShipping();
+        if (class_exists('QUI\ERP\Shipping\Types\ShippingEntry')) {
+            $Shipping = $this->getShipping();
 
-        if ($Shipping) {
-            $shippingId = $Shipping->getId();
-            $shippingData = $Shipping->toArray();
+            if ($Shipping) {
+                $shippingId = $Shipping->getId();
+                $shippingData = $Shipping->toArray();
+            }
         }
 
-        if (QUI::getPackageManager()->isInstalled('quiqqer/shipping')) {
+        if (class_exists('QUI\ERP\Shipping\ShippingStatus\Status')) {
             $ShippingStatus = $this->getShippingStatus();
-            $shippingStatus = $ShippingStatus ? $ShippingStatus->getId() : null;
+            $shippingStatus = $ShippingStatus?->getId();
         }
 
         return [
diff --git a/src/QUI/ERP/Order/OrderProcess.php b/src/QUI/ERP/Order/OrderProcess.php
index a4eaf65b..d3e413cb 100644
--- a/src/QUI/ERP/Order/OrderProcess.php
+++ b/src/QUI/ERP/Order/OrderProcess.php
@@ -824,6 +824,7 @@ protected function checkProcessing(): bool | string
         $paymentIsSuccessful = false;
         $Payment = $Order->getPayment();
 
+        // @phpstan-ignore-next-line
         if ($Payment && $Payment->isSuccessful($Order->getUUID())) {
             $paymentIsSuccessful = true;
         }
@@ -1316,7 +1317,7 @@ public function getOrder(): ?AbstractOrder
                     return $this->Order;
                 }
             }
-        } catch (QUI\Erp\Order\Exception) {
+        } catch (QUI\ERP\Order\Exception) {
         }
 
 
@@ -1327,7 +1328,7 @@ public function getOrder(): ?AbstractOrder
             if (!$OrderInProcess->getOrderId()) {
                 $this->Order = $OrderInProcess;
             }
-        } catch (QUI\Erp\Order\Exception) {
+        } catch (QUI\ERP\Order\Exception) {
         }
 
         if ($this->Order === null) {
-- 
GitLab