From 99c778a198642b9e4b6025d9dfe6ce7baf159b6f Mon Sep 17 00:00:00 2001 From: Sven Reichel Date: Mon, 2 Sep 2024 07:47:58 +0200 Subject: [PATCH] PhpStan fixes (#4146) * Rector: CQ - UnusedForeachValueToArrayKeysRector (#1) * Rector: CQ - UnusedForeachValueToArrayKeysRector See Rector\CodeQuality\Rector\Foreach_\UnusedForeachValueToArrayKeysRector * fixes + phpstan See fix at rector: https://github.com/rectorphp/rector-src/pull/6164 * Fixes some phpstan errors * Revert "Rector: CQ - UnusedForeachValueToArrayKeysRector (#1)" This reverts commit 3d7eaf63f2211a9e1a8defe8c29e8f6da889ab2e. * Update app/code/core/Mage/Tag/Model/Tag.php Co-authored-by: Ng Kiat Siong * Updated docblock - have to keep array from parent method --------- Co-authored-by: Ng Kiat Siong --- .../Mage/Cms/Model/Wysiwyg/Images/Storage.php | 2 +- app/code/core/Mage/Core/Block/Abstract.php | 4 +- app/code/core/Mage/Core/Helper/String.php | 2 +- app/code/core/Mage/Core/Model/Config.php | 6 +- app/code/core/Mage/Core/Model/Store.php | 4 +- app/code/core/Mage/Core/functions.php | 6 +- .../core/Mage/Tag/Block/Customer/Tags.php | 2 +- app/code/core/Mage/Tag/Block/Product/List.php | 2 +- .../core/Mage/Tag/Block/Product/Result.php | 4 +- app/code/core/Mage/Tag/Model/Api.php | 1 + app/code/core/Mage/Tag/Model/Api/V2.php | 6 +- .../Tag/Model/Entity/Customer/Collection.php | 1 + .../Model/Resource/Customer/Collection.php | 1 + app/code/core/Mage/Tag/Model/Tag.php | 1 + lib/Varien/Simplexml/Element.php | 2 +- phpstan.dist.baseline.neon | 127 +----------------- 16 files changed, 27 insertions(+), 144 deletions(-) diff --git a/app/code/core/Mage/Cms/Model/Wysiwyg/Images/Storage.php b/app/code/core/Mage/Cms/Model/Wysiwyg/Images/Storage.php index 94e143a9073..8aa9f1998c3 100644 --- a/app/code/core/Mage/Cms/Model/Wysiwyg/Images/Storage.php +++ b/app/code/core/Mage/Cms/Model/Wysiwyg/Images/Storage.php @@ -28,7 +28,7 @@ class Mage_Cms_Model_Wysiwyg_Images_Storage extends Varien_Object /** * Config object * - * @var Mage_Core_Model_Config_Element|Varien_Simplexml_Element|false + * @var Mage_Core_Model_Config_Element */ protected $_config; diff --git a/app/code/core/Mage/Core/Block/Abstract.php b/app/code/core/Mage/Core/Block/Abstract.php index f33b14a031e..b052542446c 100644 --- a/app/code/core/Mage/Core/Block/Abstract.php +++ b/app/code/core/Mage/Core/Block/Abstract.php @@ -993,7 +993,9 @@ protected function _getUrlModelClass() */ protected function _getUrlModel() { - return Mage::getModel($this->_getUrlModelClass()); + /** @var Mage_Core_Model_Url $model */ + $model = Mage::getModel($this->_getUrlModelClass()); + return $model; } /** diff --git a/app/code/core/Mage/Core/Helper/String.php b/app/code/core/Mage/Core/Helper/String.php index 49544a73856..02ab17ead36 100644 --- a/app/code/core/Mage/Core/Helper/String.php +++ b/app/code/core/Mage/Core/Helper/String.php @@ -466,7 +466,7 @@ protected function _getLastSubkey($key, $withBrackets = true) /** * Set array helper * - * @param Mage_Core_Helper_Abstract|Mage_Core_Helper_Array $helper + * @param Mage_Core_Helper_Array $helper * @return $this */ public function setArrayHelper(Mage_Core_Helper_Abstract $helper) diff --git a/app/code/core/Mage/Core/Model/Config.php b/app/code/core/Mage/Core/Model/Config.php index b921c35fe42..546aa7bbef4 100644 --- a/app/code/core/Mage/Core/Model/Config.php +++ b/app/code/core/Mage/Core/Model/Config.php @@ -724,7 +724,7 @@ protected function _getSectionConfig($path) * Get node value from cached section data * * @param array $path - * @return false|Varien_Simplexml_Element + * @return false|Mage_Core_Model_Config_Element */ public function getSectionNode($path) { @@ -741,7 +741,7 @@ public function getSectionNode($path) * Returns node found by the $path and scope info * * @inheritDoc - * @return Mage_Core_Model_Config_Element|Varien_Simplexml_Element|false + * @return Mage_Core_Model_Config_Element */ public function getNode($path = null, $scope = '', $scopeCode = null) { @@ -1150,7 +1150,7 @@ public function substDistroServerVars($data) * Get module config node * * @param string $moduleName - * @return Mage_Core_Model_Config_Element|SimpleXMLElement + * @return Mage_Core_Model_Config_Element */ public function getModuleConfig($moduleName = '') { diff --git a/app/code/core/Mage/Core/Model/Store.php b/app/code/core/Mage/Core/Model/Store.php index 286db5850d0..981e8d1fe66 100644 --- a/app/code/core/Mage/Core/Model/Store.php +++ b/app/code/core/Mage/Core/Model/Store.php @@ -967,9 +967,9 @@ public function roundPrice($price) /** * Format price with currency filter (taking rate into consideration) * - * @param double $price + * @param float $price * @param bool $includeContainer - * @return string + * @return string|float */ public function formatPrice($price, $includeContainer = true) { diff --git a/app/code/core/Mage/Core/functions.php b/app/code/core/Mage/Core/functions.php index de853bee9c5..1e46c5d5814 100644 --- a/app/code/core/Mage/Core/functions.php +++ b/app/code/core/Mage/Core/functions.php @@ -103,7 +103,7 @@ function mageFindClassFile($class) * @param string $errstr * @param string $errfile * @param int $errline - * @return bool|void + * @return bool|null */ function mageCoreErrorHandler($errno, $errstr, $errfile, $errline) { @@ -184,6 +184,7 @@ function mageCoreErrorHandler($errno, $errstr, $errfile, $errline) throw new Exception($errorMessage); } else { Mage::log($errorMessage, Zend_Log::ERR); + return null; } } @@ -191,7 +192,7 @@ function mageCoreErrorHandler($errno, $errstr, $errfile, $errline) * @param bool $return * @param bool $html * @param bool $showFirst - * @return string|void + * @return string|null * * @SuppressWarnings(PHPMD.ErrorControlOperator) */ @@ -216,6 +217,7 @@ function mageDebugBacktrace($return = false, $html = true, $showFirst = false) return $out; } else { echo $out; + return null; } } diff --git a/app/code/core/Mage/Tag/Block/Customer/Tags.php b/app/code/core/Mage/Tag/Block/Customer/Tags.php index 075c3960090..01ce223e417 100644 --- a/app/code/core/Mage/Tag/Block/Customer/Tags.php +++ b/app/code/core/Mage/Tag/Block/Customer/Tags.php @@ -31,7 +31,7 @@ protected function _loadTags() $this->_tags = []; $tags = Mage::getResourceModel('tag/tag_collection') - ->addPopularity(null, Mage::app()->getStore()->getId()) + ->addPopularity() ->setOrder('popularity', 'DESC') ->addCustomerFilter(Mage::getSingleton('customer/session')->getCustomerId()) ->setActiveFilter() diff --git a/app/code/core/Mage/Tag/Block/Product/List.php b/app/code/core/Mage/Tag/Block/Product/List.php index fd71223d3c8..d69ee562fa1 100644 --- a/app/code/core/Mage/Tag/Block/Product/List.php +++ b/app/code/core/Mage/Tag/Block/Product/List.php @@ -83,7 +83,7 @@ protected function _getCollection() protected function _beforeToHtml() { if (!$this->getProductId()) { - return false; + return $this; } return parent::_beforeToHtml(); diff --git a/app/code/core/Mage/Tag/Block/Product/Result.php b/app/code/core/Mage/Tag/Block/Product/Result.php index aeb8638ff49..bd5da0fb927 100644 --- a/app/code/core/Mage/Tag/Block/Product/Result.php +++ b/app/code/core/Mage/Tag/Block/Product/Result.php @@ -24,7 +24,7 @@ class Mage_Tag_Block_Product_Result extends Mage_Catalog_Block_Product_Abstract { /** - * @var Mage_Tag_Model_Resource_Tag_Collection|null + * @var Mage_Tag_Model_Resource_Product_Collection|null */ protected $_productCollection; @@ -81,7 +81,7 @@ public function getProductListHtml() } /** - * @return Mage_Tag_Model_Resource_Tag_Collection + * @return Mage_Tag_Model_Resource_Product_Collection * @throws Mage_Core_Model_Store_Exception */ protected function _getProductCollection() diff --git a/app/code/core/Mage/Tag/Model/Api.php b/app/code/core/Mage/Tag/Model/Api.php index bb04b28c842..bb8debcee0e 100644 --- a/app/code/core/Mage/Tag/Model/Api.php +++ b/app/code/core/Mage/Tag/Model/Api.php @@ -95,6 +95,7 @@ public function info($tagId, $store) */ public function add($data) { + $result = []; $data = $this->_prepareDataForAdd($data); /** @var Mage_Catalog_Model_Product $product */ $product = Mage::getModel('catalog/product')->load($data['product_id']); diff --git a/app/code/core/Mage/Tag/Model/Api/V2.php b/app/code/core/Mage/Tag/Model/Api/V2.php index 92fa9b27c8e..0684d77772a 100644 --- a/app/code/core/Mage/Tag/Model/Api/V2.php +++ b/app/code/core/Mage/Tag/Model/Api/V2.php @@ -59,7 +59,7 @@ public function add($data) * * @param int $tagId * @param string|int $store - * @return object + * @return object|array */ public function info($tagId, $store) { @@ -74,7 +74,7 @@ public function info($tagId, $store) /** * Convert data from object to array before add * - * @param object $data + * @param array|object $data * @return array */ protected function _prepareDataForAdd($data) @@ -86,7 +86,7 @@ protected function _prepareDataForAdd($data) /** * Convert data from object to array before update * - * @param object $data + * @param array|object $data * @return array */ protected function _prepareDataForUpdate($data) diff --git a/app/code/core/Mage/Tag/Model/Entity/Customer/Collection.php b/app/code/core/Mage/Tag/Model/Entity/Customer/Collection.php index f6fe3c7b8b6..d358c941611 100644 --- a/app/code/core/Mage/Tag/Model/Entity/Customer/Collection.php +++ b/app/code/core/Mage/Tag/Model/Entity/Customer/Collection.php @@ -22,6 +22,7 @@ class Mage_Tag_Model_Entity_Customer_Collection extends Mage_Customer_Model_Entity_Customer_Collection { protected $_tagTable; + protected $_tagRelTable; public function __construct() diff --git a/app/code/core/Mage/Tag/Model/Resource/Customer/Collection.php b/app/code/core/Mage/Tag/Model/Resource/Customer/Collection.php index d9b4dbf5770..167552ad0f2 100644 --- a/app/code/core/Mage/Tag/Model/Resource/Customer/Collection.php +++ b/app/code/core/Mage/Tag/Model/Resource/Customer/Collection.php @@ -268,6 +268,7 @@ public function getSelectCountSql() public function addProductName() { $productsId = []; + $productsSku = []; $productsData = []; foreach ($this->getItems() as $item) { diff --git a/app/code/core/Mage/Tag/Model/Tag.php b/app/code/core/Mage/Tag/Model/Tag.php index 0f20394cd4c..1dc6ee88c3a 100644 --- a/app/code/core/Mage/Tag/Model/Tag.php +++ b/app/code/core/Mage/Tag/Model/Tag.php @@ -366,6 +366,7 @@ public function saveRelation($productId, $customerId, $storeId) ->setActive(Mage_Tag_Model_Tag_Relation::STATUS_ACTIVE) ->setCreatedAt($relationModel->getResource()->formatDate(time())); + $result = ''; $relationModelSaveNeed = false; switch ($this->getStatus()) { case $this->getApprovedStatus(): diff --git a/lib/Varien/Simplexml/Element.php b/lib/Varien/Simplexml/Element.php index ee22aa78864..e07d39e6b77 100644 --- a/lib/Varien/Simplexml/Element.php +++ b/lib/Varien/Simplexml/Element.php @@ -128,7 +128,7 @@ public function addChild($name, $value=null, $namespace=null) * @todo Do we need to make it xpath look-a-like? * @todo Check if we still need all this and revert to plain XPath if this makes any sense * @todo param string $path Subset of xpath. Example: "child/grand[@attrName='attrValue']/subGrand" - * @param string $path Example: "child/grand@attrName=attrValue/subGrand" (to make it faster without regex) + * @param array|string $path Example: "child/grand@attrName=attrValue/subGrand" (to make it faster without regex) * @return Varien_Simplexml_Element|false */ public function descend($path) diff --git a/phpstan.dist.baseline.neon b/phpstan.dist.baseline.neon index 58d3dfd945d..aff5acc1f79 100644 --- a/phpstan.dist.baseline.neon +++ b/phpstan.dist.baseline.neon @@ -480,16 +480,6 @@ parameters: count: 1 path: app/code/core/Mage/Adminhtml/Model/Config/Data.php - - - message: "#^Method Mage_Adminhtml_Model_Config_Data\\:\\:getConfigRoot\\(\\) should return Mage_Core_Model_Config_Element but returns Varien_Simplexml_Element\\|false\\.$#" - count: 1 - path: app/code/core/Mage/Adminhtml/Model/Config/Data.php - - - - message: "#^Property Mage_Adminhtml_Model_Config_Data\\:\\:\\$_configRoot \\(Mage_Core_Model_Config_Element\\|null\\) does not accept Varien_Simplexml_Element\\|false\\.$#" - count: 1 - path: app/code/core/Mage/Adminhtml/Model/Config/Data.php - - message: "#^Method Mage_Eav_Model_Entity_Abstract\\:\\:load\\(\\) invoked with 1 parameter, 2\\-3 required\\.$#" count: 1 @@ -565,11 +555,6 @@ parameters: count: 1 path: app/code/core/Mage/Adminhtml/controllers/Customer/Wishlist/Product/Composite/WishlistController.php - - - message: "#^Call to an undefined method Varien_Simplexml_Element\\:\\:getClassName\\(\\)\\.$#" - count: 1 - path: app/code/core/Mage/Adminhtml/controllers/IndexController.php - - message: "#^Variable \\$id in isset\\(\\) always exists and is not nullable\\.$#" count: 1 @@ -2170,11 +2155,6 @@ parameters: count: 1 path: app/code/core/Mage/Checkout/controllers/OnepageController.php - - - message: "#^Method Mage_Cms_Model_Wysiwyg_Images_Storage\\:\\:getConfig\\(\\) should return Mage_Core_Model_Config_Element but returns Varien_Simplexml_Element\\|false\\.$#" - count: 1 - path: app/code/core/Mage/Cms/Model/Wysiwyg/Images/Storage.php - - message: "#^Method Mage_Eav_Model_Entity_Attribute_Source_Interface\\:\\:getAllOptions\\(\\) invoked with 2 parameters, 0 required\\.$#" count: 1 @@ -2185,11 +2165,6 @@ parameters: count: 2 path: app/code/core/Mage/ConfigurableSwatches/Model/Resource/Catalog/Product/Type/Configurable/Product/Collection.php - - - message: "#^Method Mage_Core_Block_Abstract\\:\\:_getUrlModel\\(\\) should return Mage_Core_Model_Url but returns Mage_Core_Model_Abstract\\|false\\.$#" - count: 1 - path: app/code/core/Mage/Core/Block/Abstract.php - - message: "#^Method Mage_Core_Block_Abstract\\:\\:getHelper\\(\\) should return \\$this\\(Mage_Core_Block_Abstract\\) but returns Mage_Core_Block_Abstract\\.$#" count: 1 @@ -2210,11 +2185,6 @@ parameters: count: 1 path: app/code/core/Mage/Core/Controller/Front/Router.php - - - message: "#^Call to an undefined method Varien_Simplexml_Element\\:\\:is\\(\\)\\.$#" - count: 1 - path: app/code/core/Mage/Core/Controller/Varien/Action.php - - message: "#^Constructor of class Mage_Core_Controller_Varien_Action has an unused parameter \\$invokeArgs\\.$#" count: 1 @@ -2285,11 +2255,6 @@ parameters: count: 1 path: app/code/core/Mage/Core/Helper/Abstract.php - - - message: "#^Property Mage_Core_Helper_String\\:\\:\\$_arrayHelper \\(Mage_Core_Helper_Array\\) does not accept Mage_Core_Helper_Abstract\\.$#" - count: 1 - path: app/code/core/Mage/Core/Helper/String.php - - message: "#^Property Mage_Core_Model_Abstract\\:\\:\\$_cacheTag \\(string\\|true\\) does not accept default value of type false\\.$#" count: 1 @@ -2330,28 +2295,13 @@ parameters: count: 1 path: app/code/core/Mage/Core/Model/Cache.php - - - message: "#^Call to an undefined method SimpleXMLElement\\:\\:getClassName\\(\\)\\.$#" - count: 1 - path: app/code/core/Mage/Core/Model/Config.php - - - - message: "#^Call to an undefined method Varien_Simplexml_Element\\:\\:getClassName\\(\\)\\.$#" - count: 2 - path: app/code/core/Mage/Core/Model/Config.php - - - - message: "#^Call to an undefined method Varien_Simplexml_Element\\:\\:is\\(\\)\\.$#" - count: 1 - path: app/code/core/Mage/Core/Model/Config.php - - message: "#^Method Mage_Core_Model_Config\\:\\:cleanCache\\(\\) should return \\$this\\(Mage_Core_Model_Config\\) but returns Mage_Core_Model_Config\\.$#" count: 1 path: app/code/core/Mage/Core/Model/Config.php - - message: "#^Method Mage_Core_Model_Config\\:\\:getNodeClassInstance\\(\\) should return bool but returns object\\.$#" + message: "#^Method Mage_Core_Model_Config\\:\\:getNode\\(\\) should return Mage_Core_Model_Config_Element but returns Varien_Simplexml_Element\\|false\\.$#" count: 1 path: app/code/core/Mage/Core/Model/Config.php @@ -2575,21 +2525,11 @@ parameters: count: 1 path: app/code/core/Mage/Core/Model/Resource/Iterator.php - - - message: "#^Call to an undefined method Varien_Simplexml_Element\\:\\:getClassName\\(\\)\\.$#" - count: 2 - path: app/code/core/Mage/Core/Model/Resource/Setup.php - - message: "#^Property Mage_Core_Model_Resource_Setup\\:\\:\\$_conn \\(Varien_Db_Adapter_Pdo_Mysql\\) does not accept Varien_Db_Adapter_Interface\\|false\\.$#" count: 1 path: app/code/core/Mage/Core/Model/Resource/Setup.php - - - message: "#^Property Mage_Core_Model_Resource_Setup\\:\\:\\$_moduleConfig \\(Mage_Core_Model_Config_Element\\) does not accept SimpleXMLElement\\.$#" - count: 1 - path: app/code/core/Mage/Core/Model/Resource/Setup.php - - message: "#^Property Mage_Core_Model_Resource_Setup\\:\\:\\$_resourceConfig \\(Mage_Core_Model_Config_Element\\) does not accept SimpleXMLElement\\.$#" count: 1 @@ -2625,11 +2565,6 @@ parameters: count: 1 path: app/code/core/Mage/Core/Model/Store.php - - - message: "#^Method Mage_Core_Model_Store\\:\\:formatPrice\\(\\) should return string but returns float\\.$#" - count: 1 - path: app/code/core/Mage/Core/Model/Store.php - - message: "#^PHPDoc type bool of property Mage_Core_Model_Store\\:\\:\\$_cacheTag is not covariant with PHPDoc type string\\|true of overridden property Mage_Core_Model_Abstract\\:\\:\\$_cacheTag\\.$#" count: 1 @@ -2675,16 +2610,6 @@ parameters: count: 1 path: app/code/core/Mage/Core/Model/Url/Rewrite/Request.php - - - message: "#^Function mageCoreErrorHandler\\(\\) never returns void so it can be removed from the return type\\.$#" - count: 1 - path: app/code/core/Mage/Core/functions.php - - - - message: "#^Function mageDebugBacktrace\\(\\) never returns void so it can be removed from the return type\\.$#" - count: 1 - path: app/code/core/Mage/Core/functions.php - - message: "#^Variable \\$arguments might not be defined\\.$#" count: 1 @@ -4200,11 +4125,6 @@ parameters: count: 3 path: app/code/core/Mage/Sales/Model/Quote/Address.php - - - message: "#^Call to an undefined method Varien_Simplexml_Element\\:\\:getClassName\\(\\)\\.$#" - count: 1 - path: app/code/core/Mage/Sales/Model/Quote/Address/Total/Collector.php - - message: "#^Return type \\(\\$this\\(Mage_Sales_Model_Quote_Address_Total_Discount\\)\\) of method Mage_Sales_Model_Quote_Address_Total_Discount\\:\\:fetch\\(\\) should be compatible with return type \\(array\\) of method Mage_Sales_Model_Quote_Address_Total_Abstract\\:\\:fetch\\(\\)$#" count: 1 @@ -4490,56 +4410,11 @@ parameters: count: 1 path: app/code/core/Mage/Sitemap/Model/Resource/Catalog/Abstract.php - - - message: "#^Method Mage_Tag_Model_Resource_Tag_Collection\\:\\:addPopularity\\(\\) invoked with 2 parameters, 0\\-1 required\\.$#" - count: 1 - path: app/code/core/Mage/Tag/Block/Customer/Tags.php - - - - message: "#^Method Mage_Tag_Block_Product_List\\:\\:_beforeToHtml\\(\\) should return \\$this\\(Mage_Tag_Block_Product_List\\) but returns false\\.$#" - count: 1 - path: app/code/core/Mage/Tag/Block/Product/List.php - - - - message: "#^Property Mage_Tag_Block_Product_Result\\:\\:\\$_productCollection \\(Mage_Tag_Model_Resource_Tag_Collection\\|null\\) does not accept Mage_Tag_Model_Resource_Product_Collection\\.$#" - count: 1 - path: app/code/core/Mage/Tag/Block/Product/Result.php - - - - message: "#^Variable \\$result might not be defined\\.$#" - count: 1 - path: app/code/core/Mage/Tag/Model/Api.php - - - - message: "#^Parameter \\#1 \\$data \\(object\\) of method Mage_Tag_Model_Api_V2\\:\\:_prepareDataForAdd\\(\\) should be compatible with parameter \\$data \\(array\\) of method Mage_Tag_Model_Api\\:\\:_prepareDataForAdd\\(\\)$#" - count: 1 - path: app/code/core/Mage/Tag/Model/Api/V2.php - - - - message: "#^Parameter \\#1 \\$data \\(object\\) of method Mage_Tag_Model_Api_V2\\:\\:_prepareDataForUpdate\\(\\) should be compatible with parameter \\$data \\(array\\) of method Mage_Tag_Model_Api\\:\\:_prepareDataForUpdate\\(\\)$#" - count: 1 - path: app/code/core/Mage/Tag/Model/Api/V2.php - - - - message: "#^Return type \\(object\\) of method Mage_Tag_Model_Api_V2\\:\\:info\\(\\) should be compatible with return type \\(array\\) of method Mage_Tag_Model_Api\\:\\:info\\(\\)$#" - count: 1 - path: app/code/core/Mage/Tag/Model/Api/V2.php - - message: "#^Access to an undefined property Mage_Tag_Model_Entity_Customer_Collection\\:\\:\\$_read\\.$#" count: 1 path: app/code/core/Mage/Tag/Model/Entity/Customer/Collection.php - - - message: "#^Variable \\$productsSku might not be defined\\.$#" - count: 1 - path: app/code/core/Mage/Tag/Model/Resource/Customer/Collection.php - - - - message: "#^Variable \\$result might not be defined\\.$#" - count: 1 - path: app/code/core/Mage/Tag/Model/Tag.php - - message: "#^Call to an undefined method Mage_Tax_Block_Sales_Order_Tax\\:\\:addTotal\\(\\)\\.$#" count: 7