From a2e3da6b74926cb276feb8c072e0cd93115e9ba8 Mon Sep 17 00:00:00 2001 From: Sven Reichel Date: Thu, 9 Jan 2025 04:42:43 +0100 Subject: [PATCH 1/8] check defined constants - ref https://github.com/phpstan/phpstan/issues/6744 --- app/Mage.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/Mage.php b/app/Mage.php index ec7df4782f2..5fe2ff1b31a 100644 --- a/app/Mage.php +++ b/app/Mage.php @@ -14,8 +14,9 @@ * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) */ -define('DS', DIRECTORY_SEPARATOR); -define('PS', PATH_SEPARATOR); +defined('DS') || define('DS', DIRECTORY_SEPARATOR); +defined('PS') || define('PS', PATH_SEPARATOR); + define('BP', dirname(__DIR__)); Mage::register('original_include_path', get_include_path()); From 2fb3622910b25fe19aa439dd3e556036d141e436 Mon Sep 17 00:00:00 2001 From: Sven Reichel Date: Thu, 9 Jan 2025 04:44:10 +0100 Subject: [PATCH 2/8] cs --- app/Mage.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Mage.php b/app/Mage.php index 5fe2ff1b31a..5bb2bba46f0 100644 --- a/app/Mage.php +++ b/app/Mage.php @@ -279,7 +279,7 @@ public static function register($key, $value, $graceful = false) if ($graceful) { return; } - self::throwException('Mage registry key "' . $key . '" already exists'); + self::throwException("Mage registry key $key already exists"); } self::$_registry[$key] = $value; } @@ -332,7 +332,7 @@ public static function setRoot($appRoot = '') if (is_dir($appRoot) && is_readable($appRoot)) { self::$_appRoot = $appRoot; } else { - self::throwException($appRoot . ' is not a directory or not readable by this user'); + self::throwException("$appRoot is not a directory or not readable by this user"); } } From aaf02c732a1bdfd1bade64e71d4118f7b0f518d8 Mon Sep 17 00:00:00 2001 From: Sven Reichel Date: Thu, 9 Jan 2025 04:46:22 +0100 Subject: [PATCH 3/8] check observer class exists --- app/Mage.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/Mage.php b/app/Mage.php index 5bb2bba46f0..7aa5fc035e6 100644 --- a/app/Mage.php +++ b/app/Mage.php @@ -494,14 +494,18 @@ public static function getConfig() * @param callback $callback * @param array $data * @param string $observerName - * @param string $observerClass + * @param class-string|'' $observerClass * @return Varien_Event_Collection + * @throws Mage_Core_Exception */ public static function addObserver($eventName, $callback, $data = [], $observerName = '', $observerClass = '') { if ($observerClass == '') { $observerClass = 'Varien_Event_Observer'; } + if (!class_exists($observerClass)) { + self::throwException("Invalid observer class: $observerClass"); + } $observer = new $observerClass(); $observer->setName($observerName)->addData($data)->setEventName($eventName)->setCallback($callback); return self::getEvents()->addObserver($observer); From 3863252fc49f4ee4317a10bbdf0cc15f9b6c3390 Mon Sep 17 00:00:00 2001 From: Sven Reichel Date: Thu, 9 Jan 2025 04:48:10 +0100 Subject: [PATCH 4/8] check exception class exists --- app/Mage.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/Mage.php b/app/Mage.php index 7aa5fc035e6..29c98227f1b 100644 --- a/app/Mage.php +++ b/app/Mage.php @@ -643,14 +643,17 @@ public static function getResourceHelper($moduleName) /** * Return new exception by module to be thrown * - * @param string $module + * @param string $moduleName * @param string $message - * @param integer $code + * @param int $code * @return Mage_Core_Exception */ - public static function exception($module = 'Mage_Core', $message = '', $code = 0) + public static function exception($moduleName = 'Mage_Core', $message = '', $code = 0) { - $className = $module . '_Exception'; + $className = $moduleName . '_Exception'; + if (!class_exists($className)) { + $className = 'Mage_Core_Exception'; + } return new $className($message, $code); } From 6bbff86eeb7cb2f2cb9a0f6acb442aa03e936f0c Mon Sep 17 00:00:00 2001 From: Sven Reichel Date: Thu, 9 Jan 2025 04:50:07 +0100 Subject: [PATCH 5/8] allow set instance to false --- app/Mage.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Mage.php b/app/Mage.php index 29c98227f1b..fbec0ef95f6 100644 --- a/app/Mage.php +++ b/app/Mage.php @@ -792,8 +792,8 @@ public static function run($code = '', $type = 'store', $options = []) */ private static function _setIsInstalled($options = []) { - if (isset($options['is_installed']) && $options['is_installed']) { - self::$_isInstalled = true; + if (isset($options['is_installed'])) { + self::$_isInstalled = (bool) $options['is_installed']; } } From 344421ae8ab84dcf5037d71bb7bfb408aec286a2 Mon Sep 17 00:00:00 2001 From: Sven Reichel Date: Thu, 9 Jan 2025 04:55:06 +0100 Subject: [PATCH 6/8] variable names --- app/code/core/Mage/Core/Model/Config.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/app/code/core/Mage/Core/Model/Config.php b/app/code/core/Mage/Core/Model/Config.php index f88d34aa119..12f58250faa 100644 --- a/app/code/core/Mage/Core/Model/Config.php +++ b/app/code/core/Mage/Core/Model/Config.php @@ -1333,17 +1333,17 @@ public function getPathVars($args = null) * Retrieve class name by class group * * @param string $groupType currently supported model, block, helper - * @param string $classId slash separated class identifier, ex. group/class + * @param string $classAlias slash separated class identifier, ex. group/class * @param string $groupRootNode optional config path for group config * @return string */ - public function getGroupedClassName($groupType, $classId, $groupRootNode = null) + public function getGroupedClassName($groupType, $classAlias, $groupRootNode = null) { if (empty($groupRootNode)) { $groupRootNode = 'global/' . $groupType . 's'; } - $classArr = explode('/', trim($classId)); + $classArr = explode('/', trim($classAlias)); $group = $classArr[0]; $class = !empty($classArr[1]) ? $classArr[1] : null; @@ -1395,8 +1395,8 @@ public function getGroupedClassName($groupType, $classId, $groupRootNode = null) /** * Retrieve block class name * - * @param string $blockType - * @return string + * @param string $blockType + * @return string */ public function getBlockClassName($blockType) { @@ -1409,15 +1409,15 @@ public function getBlockClassName($blockType) /** * Retrieve helper class name * - * @param string $helperName + * @param string $helperAlias * @return string */ - public function getHelperClassName($helperName) + public function getHelperClassName($helperAlias) { - if (!str_contains($helperName, '/')) { - $helperName .= '/data'; + if (!str_contains($helperAlias, '/')) { + $helperAlias .= '/data'; } - return $this->getGroupedClassName('helper', $helperName); + return $this->getGroupedClassName('helper', $helperAlias); } /** @@ -1445,8 +1445,8 @@ public function getResourceHelper($moduleName) /** * Retrieve module class name * - * @param string $modelClass - * @return string + * @param string $modelClass + * @return string */ public function getModelClassName($modelClass) { From ee85b4c91fefdbebf211b5735fcf27ed449d5ba8 Mon Sep 17 00:00:00 2001 From: Sven Reichel Date: Thu, 9 Jan 2025 04:59:56 +0100 Subject: [PATCH 7/8] check block class exists --- app/code/core/Mage/Core/Model/Layout.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/code/core/Mage/Core/Model/Layout.php b/app/code/core/Mage/Core/Model/Layout.php index 576c5a32476..eea3b34c56d 100644 --- a/app/code/core/Mage/Core/Model/Layout.php +++ b/app/code/core/Mage/Core/Model/Layout.php @@ -595,12 +595,13 @@ public function getMessagesBlock() /** * @param string $type * @return Mage_Core_Block_Abstract|object + * @throws Mage_Core_Exception */ public function getBlockSingleton($type) { if (!isset($this->_helpers[$type])) { $className = Mage::getConfig()->getBlockClassName($type); - if (!$className) { + if (!$className || !class_exists($className)) { Mage::throwException(Mage::helper('core')->__('Invalid block type: %s', $type)); } From 24b57ea83af431759b7c14853eed44aa40596eb5 Mon Sep 17 00:00:00 2001 From: Sven Reichel Date: Thu, 9 Jan 2025 05:05:46 +0100 Subject: [PATCH 8/8] doc --- app/Mage.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Mage.php b/app/Mage.php index fbec0ef95f6..5a851158a5b 100644 --- a/app/Mage.php +++ b/app/Mage.php @@ -662,6 +662,7 @@ public static function exception($moduleName = 'Mage_Core', $message = '', $code * * @param string $message * @param string $messageStorage + * @return never * @throws Mage_Core_Exception */ public static function throwException($message, $messageStorage = null)