From 8773c99f1fcc60ace1dcd7b1e3803317eef9ebc3 Mon Sep 17 00:00:00 2001
From: Henning <leutz@pcsg.de>
Date: Mon, 10 Feb 2025 18:23:30 +0100
Subject: [PATCH] fix(phpstan): resolve method and class checks

Several changes have been made, primarily around checking the existence of methods and classes and
removing unnecessary comments.

- Method checks have been introduced to ensure the `getCachePath()` method exists before calling it.
- Removed invalid phpDocs, e.g., @var $Site Site and @var $Project Project
- Fixed typing to align return types and updated a factory method to check class existence.
- Added variable definition comments in the settings.css.php file for clarity.

Related: quiqqer/template-cologne#114
---
 phpstan-baseline.neon                         | 51 +------------------
 .../Controls/Menu/Categories.php              |  8 ++-
 src/QUI/TemplateCologne/Utils.php             | 15 +++---
 src/QUI/TemplateCologne/settings.css.php      |  9 +++-
 4 files changed, 21 insertions(+), 62 deletions(-)

diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon
index 0fa167e..1339890 100644
--- a/phpstan-baseline.neon
+++ b/phpstan-baseline.neon
@@ -1,51 +1,2 @@
 parameters:
-	ignoreErrors:
-		-
-			message: "#^Call to an undefined method QUI\\\\Interfaces\\\\Projects\\\\Site\\:\\:getCachePath\\(\\)\\.$#"
-			count: 1
-			path: src/QUI/TemplateCologne/Controls/Menu/Categories.php
-
-		-
-			message: "#^Access to constant PRODUCT_FIELD_SHIPPING_TIME on an unknown class QUI\\\\ERP\\\\Shipping\\\\Shipping\\.$#"
-			count: 1
-			path: src/QUI/TemplateCologne/Utils.php
-
-		-
-			message: "#^Access to constant PRODUCT_FIELD_STOCK on an unknown class QUI\\\\ERP\\\\StockManagement\\\\StockManager\\.$#"
-			count: 1
-			path: src/QUI/TemplateCologne/Utils.php
-
-		-
-			message: "#^Call to method setProduct\\(\\) on an unknown class QUI\\\\ERP\\\\StockManagement\\\\Products\\\\Fields\\\\StockView\\.$#"
-			count: 1
-			path: src/QUI/TemplateCologne/Utils.php
-
-		-
-			message: "#^Call to static method getShippingTimeFrontendViewByProduct\\(\\) on an unknown class QUI\\\\ERP\\\\StockManagement\\\\StockManager\\.$#"
-			count: 1
-			path: src/QUI/TemplateCologne/Utils.php
-
-		-
-			message: "#^Method QUI\\\\TemplateCologne\\\\Utils\\:\\:getStockFrontendView\\(\\) should return QUI\\\\ERP\\\\Products\\\\Field\\\\View\\|false but returns QUI\\\\ERP\\\\StockManagement\\\\Products\\\\Fields\\\\StockView\\.$#"
-			count: 1
-			path: src/QUI/TemplateCologne/Utils.php
-
-		-
-			message: "#^PHPDoc tag @var for variable \\$StockView contains unknown class QUI\\\\ERP\\\\StockManagement\\\\Products\\\\Fields\\\\StockView\\.$#"
-			count: 1
-			path: src/QUI/TemplateCologne/Utils.php
-
-		-
-			message: "#^PHPDoc tag @var has invalid value \\(\\$Site Site\\)\\: Unexpected token \"\\$Site\", expected type at offset 9$#"
-			count: 1
-			path: src/QUI/TemplateCologne/Utils.php
-
-		-
-			message: "#^Variable \\$Project might not be defined\\.$#"
-			count: 2
-			path: src/QUI/TemplateCologne/settings.css.php
-
-		-
-			message: "#^Variable \\$Site might not be defined\\.$#"
-			count: 1
-			path: src/QUI/TemplateCologne/settings.css.php
+	ignoreErrors:
\ No newline at end of file
diff --git a/src/QUI/TemplateCologne/Controls/Menu/Categories.php b/src/QUI/TemplateCologne/Controls/Menu/Categories.php
index eadc91c..ab3af91 100644
--- a/src/QUI/TemplateCologne/Controls/Menu/Categories.php
+++ b/src/QUI/TemplateCologne/Controls/Menu/Categories.php
@@ -72,10 +72,14 @@ public function getBody(): string
         }
 
         $cache = EventHandler::menuCacheName() . '/megaMenu/';
+        $cachePah = '';
+
+        if (method_exists($this->getSite(), 'getCachePath')) {
+            $cachePah = $this->getSite()->getCachePath();
+        }
 
         $cache .= md5(
-            $this->getSite()->getCachePath() .
-            serialize($this->getAttributes())
+            $cachePah . serialize($this->getAttributes())
         );
 
         try {
diff --git a/src/QUI/TemplateCologne/Utils.php b/src/QUI/TemplateCologne/Utils.php
index 098a86c..9f88806 100644
--- a/src/QUI/TemplateCologne/Utils.php
+++ b/src/QUI/TemplateCologne/Utils.php
@@ -11,7 +11,6 @@
 use QUI\ERP\Shipping\Shipping;
 use QUI\ERP\StockManagement\StockManager;
 use QUI\Projects\Project;
-use QUI\Projects\Site;
 use QUI\TemplateCologne\Controls\Payments;
 
 use function class_exists;
@@ -75,13 +74,8 @@ public static function getAvatar(mixed $User): QUI\Projects\Media\Image|bool
      */
     public static function getConfig(array $params): object|array|bool|string
     {
-        /** @var $Site Site */
         $Site = $params['Site'];
-
-        /* @var $Project Project */
         $Project = $params['Project'];
-
-        /* @var $Project QUI\Template */
         $Template = $params['Template'];
 
         $cacheName = md5($Site->getId() . $Project->getName() . $Project->getLang());
@@ -599,7 +593,7 @@ private static function getPredefinedFooter(Project $Project): array
      *
      * @return false|QUI\ERP\Products\Field\View
      */
-    public static function getShippingTimeFrontendView(int $productId)
+    public static function getShippingTimeFrontendView(int $productId): bool | QUI\ERP\Products\Field\View
     {
         try {
             $Product = QUI\ERP\Products\Handler\Products::getProduct($productId);
@@ -609,7 +603,7 @@ public static function getShippingTimeFrontendView(int $productId)
             return false;
         }
 
-        if (QUI::getPackageManager()->isInstalled('quiqqer/stock-management')) {
+        if (class_exists('QUI\ERP\StockManagement\StockManager')) {
             return StockManager::getShippingTimeFrontendViewByProduct($Product);
         }
 
@@ -656,6 +650,10 @@ public static function getStockFrontendView(int $productId)
             return false;
         }
 
+        if (!class_exists('QUI\ERP\StockManagement\StockManager')) {
+            return false;
+        }
+
         try {
             $Product = QUI\ERP\Products\Handler\Products::getProduct($productId);
             $StockField = $Product->getField(StockManager::PRODUCT_FIELD_STOCK);
@@ -665,7 +663,6 @@ public static function getStockFrontendView(int $productId)
             return false;
         }
 
-        /** @var QUI\ERP\StockManagement\Products\Fields\StockView $StockView */
         $StockView = $StockField->getFrontendView();
 
         if (method_exists($StockView, 'setProduct')) {
diff --git a/src/QUI/TemplateCologne/settings.css.php b/src/QUI/TemplateCologne/settings.css.php
index 9953e25..965553d 100644
--- a/src/QUI/TemplateCologne/settings.css.php
+++ b/src/QUI/TemplateCologne/settings.css.php
@@ -1,4 +1,12 @@
 <?php
+/**
+ * cologne template file
+ *
+ * @var QUI\Projects\Project $Project
+ * @var QUI\Projects\Site $Site
+ * @var QUI\Interfaces\Template\EngineInterface $Engine
+ * @var QUI\Template $Template
+ **/
 
 $largeSpacing = $Site->getAttribute('templateCologne.largeSpacing');
 $headerImagePosition = $Project->getConfig('templateCologne.settings.headerImagePosition');
@@ -7,7 +15,6 @@
 ob_start();
 
 ?>
-
 <?php if ($largeSpacing) { ?>
 /* more spacing */
 .page-content-header,
-- 
GitLab