VOOZH about

URL: https://deepwiki.com/MahoCommerce/maho-phpstan-plugin/4.2-magecoreconfig

⇱ MageCoreConfig | MahoCommerce/maho-phpstan-plugin | DeepWiki


Loading...
Menu

MageCoreConfig

Purpose and Scope

MageCoreConfig is the configuration loader service that bridges PHPStan analysis with Magento/Maho's XML-based configuration system. It initializes the Mage application context and provides converter functions that resolve class aliases (e.g., 'catalog/product') to fully qualified class names (e.g., 'Mage_Catalog_Model_Product').

This page covers the implementation details of MageCoreConfig. For information about how these converter functions are used during type inference, see MageTypeExtension. For validation of resolved class names, see MageInvalidTypeRule.

Sources: src/Config/MageCoreConfig.php1-76

Architecture Position

MageCoreConfig serves as the central configuration service for the plugin. It is dependency-injected into both MageTypeExtension and MageInvalidTypeRule through the service container configuration in extension.neon.


Sources: src/Config/MageCoreConfig.php1-76

Class Structure

The MageCoreConfig class is declared as final to prevent inheritance and contains two instance properties and two public methods:

PropertyTypePurpose
$useLocalXmlboolConfiguration flag controlling initialization behavior
$hasInitializedboolTracks whether Mage has been initialized to avoid duplicate initialization
MethodReturn TypePurpose
__construct(bool)voidAccepts configuration parameter
getConfig()Mage_Core_Model_ConfigProvides access to initialized configuration object
getClassNameConverterFunction(string, string)?callableReturns converter function for specific class::method combinations

Sources: src/Config/MageCoreConfig.php8-16

Initialization Process

The initialization logic in the getConfig() method uses lazy initialization to avoid unnecessary overhead:


Cache Directory Isolation

The initialization explicitly sets a separate cache directory (BP . '/var/phpstan-cache') to prevent PHPStan analysis from corrupting the application's production cache. This is critical because PHPStan runs in a different context and may generate incomplete or invalid cache entries.

Sources: src/Config/MageCoreConfig.php18-29

Converter Function System

The getClassNameConverterFunction() method returns type-safe callable closures that delegate to Mage_Core_Model_Config methods. The system uses a match expression to map class-method pairs to appropriate converter functions.

Match Expression Architecture


Sources: src/Config/MageCoreConfig.php34-74

Supported Method Patterns

The match expression recognizes 30 distinct class-method combinations across 6 configuration resolution categories:

Model and Singleton Resolution

These methods resolve model class aliases to full class names. All return string.

ClassMethodConfig Method Called
MagegetModelgetModelClassName()
MagegetSingletongetModelClassName()
Mage_Core_Model_ConfiggetModelInstancegetModelClassName()
Mage_Core_Model_FactorygetModelgetModelClassName()
Mage_Core_Model_FactorygetSingletongetModelClassName()

Example: Mage::getModel('catalog/product')'Mage_Catalog_Model_Product'

Sources: src/Config/MageCoreConfig.php37-42

Resource Model Resolution

These methods resolve resource model aliases. Return type is string|false because resource models may not exist for all models.

ClassMethodConfig Method Called
MagegetResourceModelgetResourceModelClassName()
MagegetResourceSingletongetResourceModelClassName()
Mage_Core_Model_ConfiggetResourceModelInstancegetResourceModelClassName()
Mage_Core_Model_FactorygetResourceModelgetResourceModelClassName()

Example: Mage::getResourceModel('catalog/product')'Mage_Catalog_Model_Resource_Product'

Sources: src/Config/MageCoreConfig.php44-48

Resource Helper Resolution

These methods resolve resource helper aliases. Return type is string|false.

ClassMethodConfig Method Called
MagegetResourceHelpergetResourceHelperClassName()
Mage_Core_Model_ConfiggetResourceHelpergetResourceHelperClassName()
Mage_Core_Model_ConfiggetResourceHelperInstancegetResourceHelperClassName()

Example: Mage::getResourceHelper('catalog')'Mage_Catalog_Model_Resource_Helper_Mysql4'

Sources: src/Config/MageCoreConfig.php50-53

Block Resolution

These methods resolve block class aliases to full class names. All return string.

ClassMethodConfig Method Called
MagegetBlockSingletongetBlockClassName()
Mage_Core_Block_AbstractgetHelpergetBlockClassName()
Mage_Core_Model_LayoutaddBlockgetBlockClassName()
Mage_Core_Model_LayoutcreateBlockgetBlockClassName()
Mage_Core_Model_LayoutgetBlockSingletongetBlockClassName()

Example: Mage::getBlockSingleton('catalog/product_view')'Mage_Catalog_Block_Product_View'

Sources: src/Config/MageCoreConfig.php55-60

Helper Resolution

These methods resolve helper class aliases to full class names. All return string.

ClassMethodConfig Method Called
MagehelpergetHelperClassName()
Mage_Core_Block_AbstracthelpergetHelperClassName()
Mage_Core_Model_ConfiggetHelperInstancegetHelperClassName()
Mage_Core_Model_FactorygetHelpergetHelperClassName()
Mage_Core_Model_LayouthelpergetHelperClassName()

Example: Mage::helper('catalog')'Mage_Catalog_Helper_Data'

Sources: src/Config/MageCoreConfig.php62-67

Node Class Resolution

This method resolves XML node paths to class names. Returns string.

ClassMethodConfig Method Called
Mage_Core_Model_ConfiggetNodeClassInstancegetNodeClassName()

Example: $config->getNodeClassInstance('global/models/catalog/class') → resolved class name

Sources: src/Config/MageCoreConfig.php69-70

Return Value Semantics

The converter functions follow specific return type patterns:


Model/Block/Helper Methods: Always return string because these aliases must resolve to valid classes.

Resource Model/Helper Methods: Return string|false because not all models have corresponding resource models, and not all modules have resource helpers.

Sources: src/Config/MageCoreConfig.php32-74

Integration Flow

The following diagram shows how MageCoreConfig integrates into the complete type resolution pipeline:


Sources: src/Config/MageCoreConfig.php1-76

Mock System Dependencies

MageCoreConfig relies on mock definitions to provide type information for Mage core classes without requiring a full Maho installation:

Mock FileClasses DefinedPurpose
mock/maho-phpstan-plugin/Mage.phpMage, Mage_Core_Model_App, Mage_Core_Model_ConfigProvides signatures for initialization and config methods
mock/maho-phpstan-plugin/Mage.php6BP constantDefines base path constant used in cache directory path

The mock Mage_Core_Model_Config class defines the converter method signatures that MageCoreConfig delegates to:

getModelClassName(string): string
getResourceModelClassName(string): string|false
getResourceHelperClassName(string): string|false
getBlockClassName(string): string
getHelperClassName(string): string
getNodeClassName(string): string

Sources: mock/maho-phpstan-plugin/Mage.php1-119

Configuration Parameter

The $useLocalXml constructor parameter controls whether MageCoreConfig initializes Mage during analysis:

ValueBehavior
falseCalls Mage::init() on first getConfig() invocation
trueAssumes Mage is already initialized, skips initialization

This parameter is configured in extension.neon when the service is registered. The default value is false for standard PHPStan usage, but can be set to true for advanced scenarios where Mage initialization should be handled externally.

Sources: src/Config/MageCoreConfig.php10-27

Performance Considerations

Lazy Initialization

The getConfig() method uses the $hasInitialized flag to ensure Mage::init() is called at most once per PHPStan analysis run, avoiding expensive repeated initialization.

Match Expression Efficiency

The match expression in getClassNameConverterFunction() is evaluated only once per unique class-method combination. The returned closures are then cached by MageTypeExtension instances, so the matching overhead is minimal.

XML Configuration Caching

By using Mage::init() with 'is_installed' => false, the configuration leverages Mage's built-in XML configuration caching system, avoiding repeated XML parsing across multiple PHPStan invocations.

Sources: src/Config/MageCoreConfig.php18-29 src/Config/MageCoreConfig.php34-74