From 19046b7bfc806ab7ead59536f5bde98e03616e62 Mon Sep 17 00:00:00 2001
From: Henning <leutz@pcsg.de>
Date: Sun, 23 Feb 2025 10:36:09 +0100
Subject: [PATCH] fix(phpstan): ajax - improve null checking and function
 existence verification

Changes include:

1) In `removePos.php`, removed the usage of null safe method call, as it was not needed.
2) In `getOrderControl.php`, added a null guard before trying to get view from the OrderProcess and
use it. If the method does not exist, null will be assigned to `$View`.
3) Removed unnecessary fallback to get the first step in `getStep.php`. The current step is enough.
4) In `savePayment.php` added a check for the existence of 'savePayment' method before invoking it
to avoid possible errors.
5) The fallback to get the first step was also removed in `reload.php`.
6) Removed an unnecessary if condition in `send.php` and directly assigned current step's name to
`$current`.
7) In `setQuantity.php`, added a verification to check the existence of 'setQuantity' method before
calling it.

Related: quiqqer/order#172
---
 ajax/frontend/basket/removePos.php             | 2 +-
 ajax/frontend/order/getOrderControl.php        | 7 ++++++-
 ajax/frontend/order/getStep.php                | 5 -----
 ajax/frontend/order/processing/savePayment.php | 7 ++++---
 ajax/frontend/order/reload.php                 | 4 ----
 ajax/frontend/order/send.php                   | 6 +-----
 ajax/frontend/order/setQuantity.php            | 2 +-
 7 files changed, 13 insertions(+), 20 deletions(-)

diff --git a/ajax/frontend/basket/removePos.php b/ajax/frontend/basket/removePos.php
index 4320d813..43c1a414 100644
--- a/ajax/frontend/basket/removePos.php
+++ b/ajax/frontend/basket/removePos.php
@@ -15,7 +15,7 @@
     function ($basketId, $pos) {
         $User = QUI::getUserBySession();
         $Basket = new QUI\ERP\Order\Basket\Basket($basketId, $User);
-        $Basket->getProducts()?->removePos($pos);
+        $Basket->getProducts()->removePos($pos);
 
         QUI::getEvents()->fireEvent(
             'quiqqerOrderBasketRemovePos',
diff --git a/ajax/frontend/order/getOrderControl.php b/ajax/frontend/order/getOrderControl.php
index 0c8b7884..1cfc1d74 100644
--- a/ajax/frontend/order/getOrderControl.php
+++ b/ajax/frontend/order/getOrderControl.php
@@ -20,10 +20,15 @@ function ($orderHash) {
         $Output = new QUI\Output();
         $result = $OrderProcess->create();
         $css = QUI\Control\Manager::getCSS();
+        $View = null;
+
+        if (method_exists($OrderProcess->getOrder(), 'getView')) {
+            $View = $OrderProcess->getOrder()->getView();
+        }
 
         return [
             'html' => $Output->parse($css . $result),
-            'data' => $OrderProcess->getOrder()->getView()->toArray()
+            'data' => $View?->toArray()
         ];
     },
     ['orderHash']
diff --git a/ajax/frontend/order/getStep.php b/ajax/frontend/order/getStep.php
index b7cd5c68..88b40917 100644
--- a/ajax/frontend/order/getStep.php
+++ b/ajax/frontend/order/getStep.php
@@ -28,11 +28,6 @@ function ($orderId, $step, $orderHash, $basketEditable) {
             ]);
 
             $Current = $OrderProcess->getCurrentStep();
-
-            if (!$Current) {
-                $Current = $OrderProcess->getFirstStep();
-            }
-
             $OrderProcess->setAttribute('step', $Current->getName());
 
             $html = $OrderProcess->create();
diff --git a/ajax/frontend/order/processing/savePayment.php b/ajax/frontend/order/processing/savePayment.php
index 05059593..f44b1511 100644
--- a/ajax/frontend/order/processing/savePayment.php
+++ b/ajax/frontend/order/processing/savePayment.php
@@ -22,12 +22,13 @@ function ($orderHash, $payment) {
             'step' => $Processing->getName()
         ]);
 
-        /* @var $Processing Processing */
         $Processing = $OrderProcess->getCurrentStep();
         $Order = $OrderProcess->getOrder();
-
         $Processing->setAttribute('Order', $Order);
-        $Processing->savePayment($payment);
+
+        if (method_exists($Processing, 'savePayment')) {
+            $Processing->savePayment($payment);
+        }
     },
     ['orderHash', 'payment']
 );
diff --git a/ajax/frontend/order/reload.php b/ajax/frontend/order/reload.php
index 16b8c12b..80021bd3 100644
--- a/ajax/frontend/order/reload.php
+++ b/ajax/frontend/order/reload.php
@@ -25,10 +25,6 @@ function ($orderId, $step, $orderHash, $basketEditable) {
         $Order = $OrderProcess->getOrder();
         $Current = $OrderProcess->getCurrentStep();
 
-        if (!$Current) {
-            $Current = $OrderProcess->getFirstStep();
-        }
-
         $OrderProcess->setAttribute('step', $Current->getName());
         $OrderProcess->setAttribute('orderHash', $Order->getUUID());
 
diff --git a/ajax/frontend/order/send.php b/ajax/frontend/order/send.php
index 9779b71f..edbd4142 100644
--- a/ajax/frontend/order/send.php
+++ b/ajax/frontend/order/send.php
@@ -32,11 +32,7 @@ function ($current, $orderHash, $formData) {
             ]);
 
             $result = $OrderProcess->create();
-            $current = false;
-
-            if ($OrderProcess->getCurrentStep()) {
-                $current = $OrderProcess->getCurrentStep()->getName();
-            }
+            $current = $OrderProcess->getCurrentStep()->getName();
 
             return [
                 'html' => $result,
diff --git a/ajax/frontend/order/setQuantity.php b/ajax/frontend/order/setQuantity.php
index 7ef1f249..885045dd 100644
--- a/ajax/frontend/order/setQuantity.php
+++ b/ajax/frontend/order/setQuantity.php
@@ -30,7 +30,7 @@ function ($orderHash, $pos, $quantity) {
         $Products = $Basket->getProducts();
         $products = $Products->getProducts(); // get as array
 
-        if (isset($products[$pos])) {
+        if (isset($products[$pos]) && method_exists($products[$pos], 'setQuantity')) {
             $products[$pos]->setQuantity($quantity);
         }
 
-- 
GitLab