# ServiceMap

> Maps and normalizes service and component definitions from Yii Application configuration for static analysis.

[API reference](/docs/api/index.md) › [PHPStan](/docs/extensions/phpstan/reference/index.md) › ServiceMap

| final class | `yii2\extensions\phpstan\ServiceMap` |
| --- | --- |
| Source | [`src/ServiceMap.php`](https://github.com/yii2-extensions/phpstan/blob/main/src/ServiceMap.php#L51) |

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

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Creates a new instance of the [`ServiceMap`](/docs/extensions/phpstan/reference/servicemap/index.md) class. | `ServiceMap` |
| [`getApplicationType()`](#method-getApplicationType) | Retrieves the fully qualified class name of the application type for PHPStan analysis. | `ServiceMap` |
| [`getBehaviorsByClassName()`](#method-getBehaviorsByClassName) | Retrieves the behavior class names associated with the specified class. | `ServiceMap` |
| [`getComponentClassById()`](#method-getComponentClassById) | Retrieves the fully qualified class name of a Yii Application component by its identifier. | `ServiceMap` |
| [`getComponentDefinitionById()`](#method-getComponentDefinitionById) | Retrieves the component definition array by its identifier. | `ServiceMap` |
| [`getParams()`](#method-getParams) | Retrieves the application params map for PHPStan type inference. | `ServiceMap` |
| [`getServiceById()`](#method-getServiceById) | Retrieves the fully qualified class name of a Yii Service by its identifier. | `ServiceMap` |
| [`isComponentWithoutClass()`](#method-isComponentWithoutClass) | Returns whether a component is defined by an array without a `class` or `__class` key. | `ServiceMap` |
| [`isUnresolvedComponent()`](#method-isUnresolvedComponent) | Returns whether a component is defined in the configuration but its class can't be determined. | `ServiceMap` |
| [`isUnresolvedService()`](#method-isUnresolvedService) | Returns whether a container service is defined in the configuration but its class can't be determined. | `ServiceMap` |

## Method details

### __construct() <span id="method-__construct"></span>

```php
public function __construct(string $configPath = '')
```

Creates a new instance of the [`ServiceMap`](/docs/extensions/phpstan/reference/servicemap/index.md) 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`](/docs/api/yii/base/invalidargumentexception/index.md): if one or more arguments are invalid, of incorrect type or format.
- [`RuntimeException`](https://www.php.net/manual/en/class.runtimeexception.php): if a runtime error prevents the operation from completing successfully.

### getApplicationType() <span id="method-getApplicationType"></span>

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

### getBehaviorsByClassName() <span id="method-getBehaviorsByClassName"></span>

```php
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() <span id="method-getComponentClassById"></span>

```php
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**

| 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() <span id="method-getComponentDefinitionById"></span>

```php
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**

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

### getParams() <span id="method-getParams"></span>

```php
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() <span id="method-getServiceById"></span>

```php
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**

| 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() <span id="method-isComponentWithoutClass"></span>

```php
public function isComponentWithoutClass(string $id): bool
```

Returns whether a component is defined by an array without a `class` or `__class` key.

[`Application::preInit()`](/docs/api/yii/base/application/index.md#method-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() <span id="method-isUnresolvedComponent"></span>

```php
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`](/docs/api/yii/di/instance/index.md) reference is the component itself, since
[`ServiceLocator::get()`](/docs/api/yii/di/servicelocator/index.md#method-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() <span id="method-isUnresolvedService"></span>

```php
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`](/docs/api/yii/di/instance/index.md) 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.