ServiceMap
API reference › PHPStan › ServiceMap
Maps and normalizes service and component definitions from Yii Application configuration for static analysis.
Loads, validates, and processes configuration files, exposing lookup methods to resolve class names and configuration
arrays by identifier. Registers the class of every definition form Yii accepts when it can be determined statically:
class name strings, arrays with a class or __class key, closures returning a single class, and object instances.
Definitions whose class can’t be determined, such as untyped closures, are skipped and flagged as unresolved, so the
lookups return null and the consumers type them as object.
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Creates a new instance of the ServiceMap class. | ServiceMap |
getApplicationType() | Retrieves the fully qualified class name of the application type for PHPStan analysis. | ServiceMap |
getBehaviorsByClassName() | Retrieves the behavior class names associated with the specified class. | ServiceMap |
getComponentClassById() | Retrieves the fully qualified class name of a Yii Application component by its identifier. | ServiceMap |
getComponentDefinitionById() | Retrieves the component definition array by its identifier. | ServiceMap |
getConfiguredClassById() | Returns the class that Container::get() builds for a container ID and configures with the configuration passed to it, after merging the options of the definitions on the chain. | ServiceMap |
getConstructedClassById() | Returns the class that Container::get() builds for a container ID and configures with the configuration passed to it. | ServiceMap |
getDefinitionConstructKeysById() | Returns the keys of the __construct() option that Container::build() receives from the definitions on the chain of getConfiguredClassById(). | ServiceMap |
getDefinitionOptionNamesById() | Returns the names of the options that the definitions on the chain of getConfiguredClassById() merge into the configuration, in the order Yii assigns them. | ServiceMap |
getDefinitionParamKeysById() | Returns the keys of the constructor params that the [definition, entries on the chain of getConfiguredClassById() pass to Container::build(). | ServiceMap |
getParams() | Retrieves the application params map for PHPStan type inference. | ServiceMap |
getServiceById() | Retrieves the fully qualified class name of a Yii Service by its identifier. | ServiceMap |
isComponentWithoutClass() | Returns whether a component is defined by an array without a class or __class key. | ServiceMap |
isUnresolvedComponent() | Returns whether a component is defined in the configuration but its class can’t be determined. | ServiceMap |
isUnresolvedService() | Returns whether a container service is defined in the configuration but its class can’t be determined. | ServiceMap |
Method details
Section titled “Method details”__construct()
Section titled “__construct() ”public function __construct(string $configPath = '')Creates a new instance of the ServiceMap class.
Parameters
| Parameter | Type | Description |
|---|---|---|
$configPath | string | Path to the Yii Application configuration file (default: ''). If provided, the configuration file must exist and be valid. If empty or not provided, operates with empty service/component maps. |
Throws
yii\base\InvalidArgumentException: if one or more arguments are invalid, of incorrect type or format.RuntimeException: if a runtime error prevents the operation from completing successfully.
getApplicationType()
Section titled “getApplicationType() ”public function getApplicationType(): stringRetrieves the fully qualified class name of the application type for PHPStan analysis.
Returns
string: Fully qualified class name of the application type.
getBehaviorsByClassName()
Section titled “getBehaviorsByClassName() ”public function getBehaviorsByClassName(string $class): string[]Retrieves the behavior class names associated with the specified class.
Looks up the internal behavior definitions map for the provided fully qualified class name and return an array of associated behavior class names.
Parameters
| Parameter | Type | Description |
|---|---|---|
$class | string | Fully qualified class name for which to retrieve behavior class names. |
Returns
string[]: Array of behavior class names, or an empty array if none are defined.
getComponentClassById()
Section titled “getComponentClassById() ”public function getComponentClassById(string $id): string|nullRetrieves the fully qualified class name of a Yii Application component by its identifier.
Looks up the component class name registered under the specified component ID in the internal component map.
Parameters
| Parameter | Type | Description |
|---|---|---|
$id | string | Component identifier to look up in the component map. |
Returns
string|null: Fully qualified class name of the component, or null if not found.
getComponentDefinitionById()
Section titled “getComponentDefinitionById() ”public function getComponentDefinitionById(string $id): arrayRetrieves the component definition array by its identifier.
Looks up the component definition registered under the specified component ID in the internal component definitions map.
Parameters
| Parameter | Type | Description |
|---|---|---|
$id | string | Component identifier to look up in the component definitions map. |
Returns
array: Component definition array with configuration options, or empty array if not found.
getConfiguredClassById()
Section titled “getConfiguredClassById() ”public function getConfiguredClassById(string $id): string|nullReturns the class that Container::get() builds for a container ID and configures with the
configuration passed to it, after merging the options of the definitions on the chain.
Follows the chain of getConstructedClassById() and also definitions with options: an array with a class
or __class key and other string keys, or, under an ID that contains a backslash, an array without either key,
which Yii builds as the ID itself, optionally with constructor params or an array __construct() option.
getDefinitionOptionNamesById(), getDefinitionParamKeysById() and
getDefinitionConstructKeysById() describe what the chain merges. The chain still ends without a class at a
singleton, a factory, an object, a __construct() option that isn’t an array, an integer key, both class keys
together and a cycle.
Parameters
| Parameter | Type | Description |
|---|---|---|
$id | string | Container ID or class name as written in the configuration. |
Returns
string|null: Class name without a leading backslash, or null when the chain doesn’t prove that Yii builds one class with the merged configuration.
getConstructedClassById()
Section titled “getConstructedClassById() ”public function getConstructedClassById(string $id): string|nullReturns the class that Container::get() builds for a container ID and configures with the
configuration passed to it.
An ID without a definition or singleton is built itself. A definition that only names a class, as a string, an
Instance reference, or an array holding only class or __class, with no constructor params, is
followed to that class, which may be another container ID. IDs are compared as written, as Yii does, so
'\app\Feed' doesn’t follow a definition of app\Feed. The chain ends without a class at a singleton, a
factory, an object, a definition with options, constructor params or a __construct() option, and a cycle: Yii
then returns an existing instance, lets other code apply the configuration, merges it with other values, or
never returns.
Parameters
| Parameter | Type | Description |
|---|---|---|
$id | string | Container ID or class name as written in the configuration. |
Returns
string|null: Class name without a leading backslash, or null when the chain doesn’t prove that Yii builds one class with the configuration as given.
getDefinitionConstructKeysById()
Section titled “getDefinitionConstructKeysById() ”public function getDefinitionConstructKeysById(string $id): list<int|string>|nullReturns the keys of the __construct() option that Container::build() receives from the
definitions on the chain of getConfiguredClassById().
Container::get() merges the definitions with array_merge(), so the __construct() option of the
definition nearest to the requested ID replaces those of later definitions, and one in the configuration passed
to it replaces them all.
Parameters
| Parameter | Type | Description |
|---|---|---|
$id | string | Container ID or class name as written in the configuration. |
Returns
list<int|string>|null: Keys of the nearest __construct() option, or null when no definition on the chain has one or the chain doesn’t prove a class.
getDefinitionOptionNamesById()
Section titled “getDefinitionOptionNamesById() ”public function getDefinitionOptionNamesById(string $id): list<string>Returns the names of the options that the definitions on the chain of getConfiguredClassById() merge into
the configuration, in the order Yii assigns them.
Container::get() merges each definition’s options before the configuration it receives, so the
options of the last definition come first, and a configuration key that a definition also sets takes the
position of the definition’s key.
Parameters
| Parameter | Type | Description |
|---|---|---|
$id | string | Container ID or class name as written in the configuration. |
Returns
list<string>: Option names, or an empty list when no definition on the chain has options or the chain doesn’t prove a class.
getDefinitionParamKeysById()
Section titled “getDefinitionParamKeysById() ”public function getDefinitionParamKeysById(string $id): list<int|string>Returns the keys of the constructor params that the [definition, params] entries on the chain of
getConfiguredClassById() pass to Container::build().
Container::mergeParams() merges the params of each definition by key, so the result holds the
keys of every definition on the chain, in the order of the definition nearest to the requested ID first.
Parameters
| Parameter | Type | Description |
|---|---|---|
$id | string | Container ID or class name as written in the configuration. |
Returns
list<int|string>: Param keys, or an empty list when no definition on the chain has params or the chain doesn’t prove a class.
getParams()
Section titled “getParams() ”public function getParams(): array<array-key, mixed>Retrieves the application params map for PHPStan type inference.
Returns the params key-value pairs extracted from the Yii Application configuration file, enabling static
analysis tools to infer precise array shape types for Yii::$app->params access.
Returns
array<array-key, mixed>: Params key-value pairs from configuration.
getServiceById()
Section titled “getServiceById() ”public function getServiceById(string $id): string|nullRetrieves the fully qualified class name of a Yii Service by its identifier.
Looks up the service class name registered under the specified service ID in the internal service map.
Parameters
| Parameter | Type | Description |
|---|---|---|
$id | string | Service identifier to look up in the service map. |
Returns
string|null: Fully qualified class name of the service, or null if not found.
isComponentWithoutClass()
Section titled “isComponentWithoutClass() ”public function isComponentWithoutClass(string $id): boolReturns whether a component is defined by an array without a class or __class key.
Application::preInit() completes such a definition with the class of the matching core
component, so the type the application class declares for the component still applies. Such a component is also
unresolved.
Parameters
| Parameter | Type | Description |
|---|---|---|
$id | string | Component identifier to look up. |
Returns
bool: true if the component is defined by an array without a class key; false otherwise.
isUnresolvedComponent()
Section titled “isUnresolvedComponent() ”public function isUnresolvedComponent(string $id): boolReturns whether a component is defined in the configuration but its class can’t be determined.
Covers closures without a single class return type, array callables, arrays without a class key, and classes
naming a container ID on a cycle or a container ID whose class can’t be determined. Components that are absent or
set to null aren’t unresolved, and an Instance reference is the component itself, since
ServiceLocator::get() returns it without resolving it.
Parameters
| Parameter | Type | Description |
|---|---|---|
$id | string | Component identifier to look up. |
Returns
bool: true if the component is defined with an unknown class; false otherwise.
isUnresolvedService()
Section titled “isUnresolvedService() ”public function isUnresolvedService(string $id): boolReturns whether a container service is defined in the configuration but its class can’t be determined.
Covers factories without a single class return type, array callables, arrays without a class key under an ID
that doesn’t name an instantiable class, and definitions, Instance references included, naming a container
ID on a cycle or a container ID whose class can’t be determined. Services that are absent, or defined empty,
aren’t unresolved, since Yii then uses the ID itself as the class.
Parameters
| Parameter | Type | Description |
|---|---|---|
$id | string | Service identifier to look up. |
Returns
bool: true if the service is defined with an unknown class; false otherwise.