# CreateObjectConfigurationResolver

> Resolves the class and options of a yii\BaseYii::createObject() call whose assignment path Yii makes certain.

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

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

Resolves the class and options of a [`BaseYii::createObject()`](/docs/api/yii/baseyii/index.md#method-createObject) call whose assignment path Yii makes certain.

Requires keys and class proven by literals or native types, a container ID that is unbound or bound only through
definitions that name a class ([`ServiceMap::getConfiguredClassById()`](/docs/extensions/phpstan/reference/servicemap/index.md#method-getConfiguredClassById)) and whose options, if any, are all plain
public properties of the created class, and a created class that inherits the constructor of the
[`CreateObjectConstructionContract`](/docs/extensions/phpstan/reference/rules/createobjectconstructioncontract/index.md) and the `__set()` of [`BaseObject`](/docs/api/yii/base/baseobject/index.md) or [`Component`](/docs/api/yii/base/component/index.md).

Constructor arguments from `$params`, a `__construct()` option and the definitions on the chain are accepted, when
the contract follows them, if their keys, merged as [`Container::build()`](/docs/api/yii/di/container/index.md#method-build) merges them, name at most
the configuration parameter (`0` or `config` for [`BaseObject`](/docs/api/yii/base/baseobject/index.md)). Yii then assigns the options of the
configuration when there are any, and otherwise uses that argument as the configuration, which is resolved only when
the call supplies it.

The resolved options are the ones Yii assigns, in order: the options of the `$type` configuration without the `class`
and `__class` keys that select the class, or every key of the argument used as the configuration, `class`, `__class`
and `__construct()` included, which [`BaseYii::configure()`](/docs/api/yii/baseyii/index.md#method-configure) assigns as ordinary properties.

[`ServiceMap`](/docs/extensions/phpstan/reference/servicemap/index.md) for service and component map for Yii Application static analysis.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Initializes the resolver with class reflection and the analysis configuration. | `CreateObjectConfigurationResolver` |
| [`rejectedOptions()`](#method-rejectedOptions) static | Returns the options of a resolved configuration whose value the created class never accepts, in configuration order. | `CreateObjectConfigurationResolver` |
| [`resolve()`](#method-resolve) | Returns the configuration that Yii assigns to the created class through `BaseObject::__set()` or `Component::__set()` semantics: the options of the call without its class keys, or every key of the constructor argument that the call supplies as configuration when neither the call nor a definition has options. | `CreateObjectConfigurationResolver` |
| [`resolveTypeOptions()`](#method-resolveTypeOptions) | Returns the configuration that Yii assigns for a `$type` configuration array that an API passes to [`BaseYii::createObject()`](/docs/api/yii/baseyii/index.md#method-createObject) with no `$params` besides the contract's fixed arguments, under the conditions of [`resolve()`](/docs/extensions/phpstan/reference/rules/createobjectconfigurationresolver/index.md#method-resolve). | `CreateObjectConfigurationResolver` |

## Constants

| Constant | Value | Description | Defined by |
| --- | --- | --- | --- |
| <span id="const-RESERVED_OPTIONS"></span>`RESERVED_OPTIONS` | `['__class', 'class']` | Options that [`BaseYii::createObject()`](/docs/api/yii/baseyii/index.md#method-createObject) removes from its `$type` configuration to select the class, before configuring the object; a constructor argument used as the configuration keeps them. | `CreateObjectConfigurationResolver` |

## Method details

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

```php
public function __construct(PHPStan\Reflection\ReflectionProvider $reflectionProvider, yii2\extensions\phpstan\ServiceMap $serviceMap)
```

Initializes the resolver with class reflection and the analysis configuration.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$reflectionProvider` | `PHPStan\Reflection\ReflectionProvider` | Provides class reflection for the created class. |
| `$serviceMap` | [`yii2\extensions\phpstan\ServiceMap`](/docs/extensions/phpstan/reference/servicemap/index.md) | Service and component map for Yii Application static analysis. |

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

```php
public static function rejectedOptions(yii2\extensions\phpstan\rules\CreateObjectConfiguration $configuration, PHPStan\Analyser\DependencyTracker&PHPStan\Analyser\Scope $scope): list<array{string, int, PHPStan\Type\Type, PHPStan\Type\Type, string}>
```

Returns the options of a resolved configuration whose value the created class never accepts, in configuration
order.

A value is returned only when PHP's weak type coercion can't convert it to the native type of the public property
or of the first setter parameter. A value that may be an [`Instance`](/docs/api/yii/di/instance/index.md) reference is also checked as the object
or `null` that the container may resolve it to before the assignment.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$configuration` | [`CreateObjectConfiguration`](/docs/extensions/phpstan/reference/rules/createobjectconfiguration/index.md) | Resolved configuration. |
| `$scope` | `PHPStan\Analyser\DependencyTracker&PHPStan\Analyser\Scope` | Current PHPStan analysis scope. |

**Returns**

`list<array{string, int, PHPStan\Type\Type, PHPStan\Type\Type, string}>`: Name, reported line, value type, expected native type and member description of each rejected option.

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

```php
public function resolve(PhpParser\Node\Expr\StaticCall $node, PHPStan\Analyser\DependencyTracker&PHPStan\Analyser\Scope $scope): yii2\extensions\phpstan\rules\CreateObjectConfiguration|null
```

Returns the configuration that Yii assigns to the created class through `BaseObject::__set()` or
`Component::__set()` semantics: the options of the call without its class keys, or every key of the constructor
argument that the call supplies as configuration when neither the call nor a definition has options.

Records the construction dependency of the container ID before deciding, and the declaration dependency of the
created class once known, so that the result cache reanalyses the call when a definition on the chain or that
class changes.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$node` | `PhpParser\Node\Expr\StaticCall` | Static call to inspect. |
| `$scope` | `PHPStan\Analyser\DependencyTracker&PHPStan\Analyser\Scope` | Current PHPStan analysis scope. |

**Returns**

[`CreateObjectConfiguration`](/docs/extensions/phpstan/reference/rules/createobjectconfiguration/index.md)`|null`: Resolved configuration, or `null` when the call, the configuration, the constructor arguments or the created class isn't proven, or a definition supplies the configuration.

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

```php
public function resolveTypeOptions(string $calledClass, list<array{string, int, PhpParser\Node\Expr|PHPStan\Type\Type}> $typeOptions, int $line, PHPStan\Analyser\DependencyTracker&PHPStan\Analyser\Scope $scope, yii2\extensions\phpstan\rules\CreateObjectConstructionContract|null $contract = null): yii2\extensions\phpstan\rules\CreateObjectConfiguration|null
```

Returns the configuration that Yii assigns for a `$type` configuration array that an API passes to
[`BaseYii::createObject()`](/docs/api/yii/baseyii/index.md#method-createObject) with no `$params` besides the contract's fixed arguments, under the conditions of
[`resolve()`](/docs/extensions/phpstan/reference/rules/createobjectconfigurationresolver/index.md#method-resolve).

Records the same construction and declaration dependencies as [`resolve()`](/docs/extensions/phpstan/reference/rules/createobjectconfigurationresolver/index.md#method-resolve).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$calledClass` | `string` | Class through which `createObject()` is called, as PHPStan displays it. |
| `$typeOptions` | `list<array{string, int, PhpParser\Node\Expr\|PHPStan\Type\Type}>` | Name, reported line and effective value of each option of the `$type` configuration, in the order Yii receives them. |
| `$line` | `int` | Line of the call, for options the configuration doesn't locate. |
| `$scope` | `PHPStan\Analyser\DependencyTracker&PHPStan\Analyser\Scope` | Current PHPStan analysis scope. |
| `$contract` | [`CreateObjectConstructionContract`](/docs/extensions/phpstan/reference/rules/createobjectconstructioncontract/index.md)`\|null` | Construction contract of the API, or `null` for [`CreateObjectConstructionContract::baseObject()`](/docs/extensions/phpstan/reference/rules/createobjectconstructioncontract/index.md#method-baseObject). |

**Returns**

[`CreateObjectConfiguration`](/docs/extensions/phpstan/reference/rules/createobjectconfiguration/index.md)`|null`: Resolved configuration, or `null` when the class value, the constructor arguments or the created class isn't proven, or a definition supplies the configuration.