Skip to content
PHP Ecosystem
Explore

ServiceMap

API reference › PHPStan › ServiceMap

final classyii2\extensions\phpstan\ServiceMap
Sourcesrc/ServiceMap.php

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.

MethodDescriptionDefined 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, params] 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
public function __construct(string $configPath = '')

Creates a new instance of the ServiceMap class.

Parameters

ParameterTypeDescription
$configPathstringPath 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

public function getApplicationType(): string

Retrieves the fully qualified class name of the application type for PHPStan analysis.

Returns

string: Fully qualified class name of the application type.

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

ParameterTypeDescription
$classstringFully 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.

public function getComponentClassById(string $id): string|null

Retrieves 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

ParameterTypeDescription
$idstringComponent identifier to look up in the component map.

Returns

string|null: Fully qualified class name of the component, or null if not found.

public function getComponentDefinitionById(string $id): array

Retrieves 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

ParameterTypeDescription
$idstringComponent identifier to look up in the component definitions map.

Returns

array: Component definition array with configuration options, or empty array if not found.

public function getConfiguredClassById(string $id): string|null

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.

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

ParameterTypeDescription
$idstringContainer 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.

public function getConstructedClassById(string $id): string|null

Returns 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

ParameterTypeDescription
$idstringContainer 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.

public function getDefinitionConstructKeysById(string $id): list<int|string>|null

Returns 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

ParameterTypeDescription
$idstringContainer 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.

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

ParameterTypeDescription
$idstringContainer 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.

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

ParameterTypeDescription
$idstringContainer 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.

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.

public function getServiceById(string $id): string|null

Retrieves 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

ParameterTypeDescription
$idstringService identifier to look up in the service map.

Returns

string|null: Fully qualified class name of the service, or null if not found.

public function isComponentWithoutClass(string $id): bool

Returns 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

ParameterTypeDescription
$idstringComponent identifier to look up.

Returns

bool: true if the component is defined by an array without a class key; false otherwise.

public function isUnresolvedComponent(string $id): bool

Returns 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

ParameterTypeDescription
$idstringComponent identifier to look up.

Returns

bool: true if the component is defined with an unknown class; false otherwise.

public function isUnresolvedService(string $id): bool

Returns 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

ParameterTypeDescription
$idstringService identifier to look up.

Returns

bool: true if the service is defined with an unknown class; false otherwise.