Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New PHPStan Plugin Test #4411

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 105 additions & 70 deletions app/Mage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -278,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;
}
Expand Down Expand Up @@ -331,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");
}
}

Expand Down Expand Up @@ -493,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);
Expand All @@ -524,128 +529,157 @@ public static function dispatchEvent($name, array $data = [])
}

/**
* Retrieve model object
* Retrieve helper singleton by alias
*
* ```php
* $helper = Mage::helper('core'); // Mage_Core_Helper_Data
* $helper = Mage::helper('core/url'); // Mage_Core_Helper_Url
* ```
*
* @link Mage_Core_Model_Config::getModelInstance
* @param string $modelClass
* @param array|string|object $arguments
* @return Mage_Core_Model_Abstract|false
* @param string $helperAlias
* @return Mage_Core_Helper_Abstract|false
*/
public static function getModel($modelClass = '', $arguments = [])
public static function helper($helperAlias)
{
return self::getConfig()->getModelInstance($modelClass, $arguments);
$registryKey = '_helper/' . $helperAlias;
if (!isset(self::$_registry[$registryKey])) {
self::register($registryKey, self::getConfig()->getHelperInstance($helperAlias));
}
return self::$_registry[$registryKey];
}

/**
* Retrieve model object singleton
* Retrieve model instance by alias
*
* @param string $modelClass
* @return Mage_Core_Model_Abstract|false
* ```php
* $model = Mage::getModel('core/store'); // Mage_Core_Model_Store
* ```
*
* @param string $modelAlias
* @param array|string|object $arguments
* @return Mage_Core_Model_Abstract|false
*/
public static function getSingleton($modelClass = '', array $arguments = [])
public static function getModel($modelAlias, $arguments = [])
{
$registryKey = '_singleton/' . $modelClass;
if (!isset(self::$_registry[$registryKey])) {
self::register($registryKey, self::getModel($modelClass, $arguments));
}
return self::$_registry[$registryKey];
return self::getConfig()->getModelInstance($modelAlias, $arguments);
}

/**
* Retrieve object of resource model
* Retrieve model singleton by alias
*
* @param string $modelClass
* @param array $arguments
* @return Mage_Core_Model_Resource_Db_Collection_Abstract|false
* ```php
* $model = Mage::getModel('core/session'); // Mage_Core_Model_Session
* ```
*
* @param string $modelAlias
* @return Mage_Core_Model_Abstract|false
*/
public static function getResourceModel($modelClass, $arguments = [])
public static function getSingleton($modelAlias, array $arguments = [])
{
return self::getConfig()->getResourceModelInstance($modelClass, $arguments);
$registryKey = '_singleton/' . $modelAlias;
if (!isset(self::$_registry[$registryKey])) {
self::register($registryKey, self::getModel($modelAlias, $arguments));
}
return self::$_registry[$registryKey];
}

/**
* Retrieve Controller instance by ClassName
* Retrieve resource model by alias
*
* @param string $class
* @param Mage_Core_Controller_Request_Http $request
* @param Mage_Core_Controller_Response_Http $response
* @return Mage_Core_Controller_Front_Action
* ```php
* $model = Mage::getResourceModel('core/store_collection'); // Mage_Core_Model_Resource_Store_Collection
* ```
*
* @param string $modelAlias
* @param array $arguments
* @return Mage_Core_Model_Resource_Db_Collection_Abstract|false
*/
public static function getControllerInstance($class, $request, $response, array $invokeArgs = [])
public static function getResourceModel($modelAlias, $arguments = [])
{
return new $class($request, $response, $invokeArgs);
return self::getConfig()->getResourceModelInstance($modelAlias, $arguments);
}

/**
* Retrieve resource model object singleton
* Retrieve resource model singleton by alias
*
* ```php
* $model = Mage::getResourceSingleton('core/session'); // Mage_Core_Model_Resource_Session
* ```
*
* @param string $modelClass
* @return object
* @param string $modelAlias
* @return Mage_Core_Model_Resource_Db_Collection_Abstract|false
*/
public static function getResourceSingleton($modelClass = '', array $arguments = [])
public static function getResourceSingleton($modelAlias, array $arguments = [])
{
$registryKey = '_resource_singleton/' . $modelClass;
$registryKey = '_resource_singleton/' . $modelAlias;
if (!isset(self::$_registry[$registryKey])) {
self::register($registryKey, self::getResourceModel($modelClass, $arguments));
self::register($registryKey, self::getResourceModel($modelAlias, $arguments));
}
return self::$_registry[$registryKey];
}

/**
* Retrieve block object
* Retrieve resource helper model singleton
*
* @param string $type
* @return Mage_Core_Block_Abstract|false
* ```php
* $model = Mage::getResourceHelper('core'); // Mage_Core_Model_Resource_Helper_Mysql4
* ```
*
* @param string $moduleAlias
* @return Mage_Core_Model_Resource_Helper_Abstract|false
*/
public static function getBlockSingleton($type)
public static function getResourceHelper($moduleAlias)
{
$action = self::app()->getFrontController()->getAction();
return $action ? $action->getLayout()->getBlockSingleton($type) : false;
$registryKey = '_resource_helper/' . $moduleAlias;
if (!isset(self::$_registry[$registryKey])) {
self::register($registryKey, self::getConfig()->getResourceHelperInstance($moduleAlias));
}
return self::$_registry[$registryKey];
}

/**
* Retrieve helper object
* Retrieve Controller instance by ClassName
*
* @param string $name the helper name
* @return Mage_Core_Helper_Abstract
* @param class-string $class
* @param Mage_Core_Controller_Request_Http $request
* @param Mage_Core_Controller_Response_Http $response
* @return Mage_Core_Controller_Front_Action
* @throws Mage_Core_Exception
*/
public static function helper($name)
public static function getControllerInstance($class, $request, $response, array $invokeArgs = [])
{
$registryKey = '_helper/' . $name;
if (!isset(self::$_registry[$registryKey])) {
$helperClass = self::getConfig()->getHelperClassName($name);
self::register($registryKey, new $helperClass());
if (!class_exists($class)) {
self::throwException("Invalid controller class: $class");
}
return self::$_registry[$registryKey];
return new $class($request, $response, $invokeArgs);
}

/**
* Retrieve resource helper object
* Retrieve block object
*
* @param string $moduleName
* @return Mage_Core_Model_Resource_Helper_Abstract
* @param string $type
* @return Mage_Core_Block_Abstract|false
*/
public static function getResourceHelper($moduleName)
public static function getBlockSingleton($type)
{
$registryKey = '_resource_helper/' . $moduleName;
if (!isset(self::$_registry[$registryKey])) {
$helperClass = self::getConfig()->getResourceHelper($moduleName);
self::register($registryKey, $helperClass);
}
return self::$_registry[$registryKey];
$action = self::app()->getFrontController()->getAction();
return $action ? $action->getLayout()->getBlockSingleton($type) : false;
}

/**
* Return new exception by module to be thrown
*
* @param string $module
* @param string $moduleName
* @param string $message
* @param integer $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);
}

Expand All @@ -654,6 +688,7 @@ public static function exception($module = 'Mage_Core', $message = '', $code = 0
*
* @param string $message
* @param string $messageStorage
* @return never
* @throws Mage_Core_Exception
*/
public static function throwException($message, $messageStorage = null)
Expand Down Expand Up @@ -784,8 +819,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'];
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/Core/Controller/Varien/Front.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @category Mage
* @package Mage_Core
*
* @method Mage_Core_Controller_Varien_Action getAction()
* @method Mage_Core_Controller_Varien_Action|null getAction()
* @method $this setAction(Mage_Core_Controller_Varien_Action $value)
* @method bool getNoRender()
*/
Expand Down
Loading
Loading