From 6100708fe13f429aa7a3b1ec0f1e82aabb815b50 Mon Sep 17 00:00:00 2001
From: Henning <leutz@pcsg.de>
Date: Fri, 14 Feb 2025 09:27:59 +0100
Subject: [PATCH] fix(phpstan): improved method validity checks and reduced
 error handling

This commit fixes several issues in different PHP files.
- Added `method_exists` checks for `isActive` method in `activate.php`, `deactivate.php` and
`toggle.php` to prevent calls to an undefined method.
- Similar check added for `getTitle` method in `get.php`.
- Fixed sorting functionality in `search.php` by using `strcmp` instead of greater than comparison.
- Removed a large number of ignored errors in `phpstan-baseline.neon`, indicating reduced error
handling.
- Corrected the `implode` parameters order in `Discount.php` and typecasted variable to string
before using `explode` in `PriceFactor.php`.
- Updated `getTitle` & `toPriceFactor` method parameters in `Discount.php` to use union types for
compatibility with different data types.

Related: quiqqer/discount#18
---
 ajax/activate.php                    |  2 +-
 ajax/deactivate.php                  |  2 +-
 ajax/get.php                         |  2 +-
 ajax/search.php                      |  2 +-
 ajax/toggle.php                      |  4 +++
 phpstan-baseline.neon                | 46 +---------------------------
 src/QUI/ERP/Discount/Discount.php    |  6 ++--
 src/QUI/ERP/Discount/PriceFactor.php |  2 +-
 8 files changed, 13 insertions(+), 53 deletions(-)

diff --git a/ajax/activate.php b/ajax/activate.php
index b84ce10..138f7ab 100644
--- a/ajax/activate.php
+++ b/ajax/activate.php
@@ -22,7 +22,7 @@ function ($discountId) {
         $Discount->setAttribute('active', 1);
         $Discount->update();
 
-        return $Discount->isActive();
+        return method_exists($Discount, 'isActive') ? $Discount->isActive() : false;
     },
     ['discountId'],
     'Permission::checkAdminUser'
diff --git a/ajax/deactivate.php b/ajax/deactivate.php
index d82213b..2ecbc73 100644
--- a/ajax/deactivate.php
+++ b/ajax/deactivate.php
@@ -22,7 +22,7 @@ function ($discountId) {
         $Discount->setAttribute('active', 0);
         $Discount->update();
 
-        return $Discount->isActive();
+        return method_exists($Discount, 'isActive') ? $Discount->isActive() : false;
     },
     ['discountId'],
     'Permission::checkAdminUser'
diff --git a/ajax/get.php b/ajax/get.php
index 7a941df..c1aa502 100644
--- a/ajax/get.php
+++ b/ajax/get.php
@@ -22,7 +22,7 @@ function ($id) {
         $attributes = $Discount->getAttributes();
 
         /* @var $Discount Discount */
-        $attributes['title'] = $Discount->getTitle();
+        $attributes['title'] = method_exists($Discount, 'getTitle') ? $Discount->getTitle() : '';
 
         return $attributes;
     },
diff --git a/ajax/search.php b/ajax/search.php
index 0dfdbc1..f1a1706 100644
--- a/ajax/search.php
+++ b/ajax/search.php
@@ -67,7 +67,7 @@ function ($fields, $params) {
         }
 
         usort($result, function ($a, $b) {
-            return $a['text'] > $b['text'];
+            return strcmp($a['text'], $b['text']);
         });
 
         return $result;
diff --git a/ajax/toggle.php b/ajax/toggle.php
index 2ff6bc5..8e318cb 100644
--- a/ajax/toggle.php
+++ b/ajax/toggle.php
@@ -19,6 +19,10 @@ function ($discountId) {
         $Discount = $Handler->getChild($discountId);
 
         /* @var $Discount Discount */
+        if (!method_exists($Discount, 'isActive')) {
+            return false;
+        }
+
         if ($Discount->isActive()) {
             $Discount->setAttribute('active', 0);
         } else {
diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon
index 74b2d37..1339890 100644
--- a/phpstan-baseline.neon
+++ b/phpstan-baseline.neon
@@ -1,46 +1,2 @@
 parameters:
-	ignoreErrors:
-		-
-			message: "#^Call to an undefined method QUI\\\\CRUD\\\\Child\\:\\:isActive\\(\\)\\.$#"
-			count: 1
-			path: ajax/activate.php
-
-		-
-			message: "#^Call to an undefined method QUI\\\\CRUD\\\\Child\\:\\:isActive\\(\\)\\.$#"
-			count: 1
-			path: ajax/deactivate.php
-
-		-
-			message: "#^Call to an undefined method QUI\\\\CRUD\\\\Child\\:\\:getTitle\\(\\)\\.$#"
-			count: 1
-			path: ajax/get.php
-
-		-
-			message: "#^Parameter \\#2 \\$callback of function usort expects callable\\(mixed, mixed\\)\\: int, Closure\\(mixed, mixed\\)\\: bool given\\.$#"
-			count: 1
-			path: ajax/search.php
-
-		-
-			message: "#^Call to an undefined method QUI\\\\CRUD\\\\Child\\:\\:isActive\\(\\)\\.$#"
-			count: 2
-			path: ajax/toggle.php
-
-		-
-			message: "#^Call to method getArticles\\(\\) on an unknown class QUI\\\\ERP\\\\Order\\\\OrderInterface\\.$#"
-			count: 1
-			path: src/QUI/ERP/Discount/Discount.php
-
-		-
-			message: "#^Parameter \\#2 \\$array of function implode expects array\\|null, string given\\.$#"
-			count: 1
-			path: src/QUI/ERP/Discount/Discount.php
-
-		-
-			message: "#^Parameter \\$Order of method QUI\\\\ERP\\\\Discount\\\\Discount\\:\\:canUsedInOrder\\(\\) has invalid type QUI\\\\ERP\\\\Order\\\\OrderInterface\\.$#"
-			count: 2
-			path: src/QUI/ERP/Discount/Discount.php
-
-		-
-			message: "#^Parameter \\#2 \\$string of function explode expects string, float\\|int\\<min, \\-1\\>\\|int\\<1, max\\>\\|true given\\.$#"
-			count: 1
-			path: src/QUI/ERP/Discount/PriceFactor.php
+	ignoreErrors:
\ No newline at end of file
diff --git a/src/QUI/ERP/Discount/Discount.php b/src/QUI/ERP/Discount/Discount.php
index 399ae32..a28e43e 100644
--- a/src/QUI/ERP/Discount/Discount.php
+++ b/src/QUI/ERP/Discount/Discount.php
@@ -248,7 +248,7 @@ public function setAttribute(string $name, mixed $value): void
      * @param null|QUI\Locale $Locale - optional, locale object
      * @return string
      */
-    public function getTitle(QUI\Locale $Locale = null): string
+    public function getTitle(null | QUI\Locale $Locale = null): string
     {
         if (!$Locale) {
             $Locale = QUI::getLocale();
@@ -284,7 +284,7 @@ public function canCombinedWith(Discount $Discount): bool
             return false;
         }
 
-        $combine = implode($combine, ',');
+        $combine = implode(',', $combine);
 
         if (in_array($Discount->getId(), (array)$combine)) {
             return true;
@@ -521,7 +521,7 @@ public function verifyUser(User $User): void
     public function toPriceFactor(
         $Locale = null,
         $Customer = null
-    ): QUI\ERP\Products\Interfaces\PriceFactorInterface|QUI\ERP\Products\Interfaces\PriceFactorWithVatInterface|QUI\ERP\Products\Utils\PriceFactor {
+    ): QUI\ERP\Products\Interfaces\PriceFactorInterface | QUI\ERP\Products\Interfaces\PriceFactorWithVatInterface | QUI\ERP\Products\Utils\PriceFactor {
         switch ($this->getAttribute('discount_type')) {
             case QUI\ERP\Accounting\Calc::CALCULATION_PERCENTAGE:
                 $calculation = QUI\ERP\Accounting\Calc::CALCULATION_PERCENTAGE;
diff --git a/src/QUI/ERP/Discount/PriceFactor.php b/src/QUI/ERP/Discount/PriceFactor.php
index c18cdb2..12d57d8 100644
--- a/src/QUI/ERP/Discount/PriceFactor.php
+++ b/src/QUI/ERP/Discount/PriceFactor.php
@@ -47,7 +47,7 @@ public function getVatType(): QUI\ERP\Tax\TaxType
             return QUI\ERP\Tax\Utils::getShopTaxType();
         }
 
-        $standardTax = explode(':', $this->vat);
+        $standardTax = explode(':', (string)$this->vat);
 
         if (!isset($standardTax[1])) {
             return QUI\ERP\Tax\Utils::getShopTaxType();
-- 
GitLab