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
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 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.