# BehaviorMethodsClassReflectionExtension

> Resolves methods provided by behaviors attached to yii\base\Component subclasses for PHPStan analysis.

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

| final class | `yii2\extensions\phpstan\method\BehaviorMethodsClassReflectionExtension` |
| --- | --- |
| Implements | [`MethodsClassReflectionExtension`](https://apiref.phpstan.org/2.3.x/PHPStan.Reflection.MethodsClassReflectionExtension.html) |
| Source | [`src/method/BehaviorMethodsClassReflectionExtension.php`](https://github.com/yii2-extensions/phpstan/blob/main/src/method/BehaviorMethodsClassReflectionExtension.php#L24) |

Resolves methods provided by behaviors attached to [`Component`](/docs/api/yii/base/component/index.md) subclasses for PHPStan analysis.

Inspects the behaviors attached to a given class via the [`ServiceMap`](/docs/extensions/phpstan/reference/servicemap/index.md) and determines whether any provide the
requested method, allowing PHPStan to recognize behavior methods as if they were natively declared.

[`Component`](/docs/api/yii/base/component/index.md) for Yii Component class.
[`MethodsClassReflectionExtension`](https://apiref.phpstan.org/2.3.x/PHPStan.Reflection.MethodsClassReflectionExtension.html) for custom methods class reflection extension contract.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Creates a new instance of the [`BehaviorMethodsClassReflectionExtension`](/docs/extensions/phpstan/reference/method/behaviormethodsclassreflectionextension/index.md) class. | `BehaviorMethodsClassReflectionExtension` |
| [`getMethod()`](#method-getMethod) | Retrieves the reflection of a method provided by a behavior attached to the given class. | `BehaviorMethodsClassReflectionExtension` |
| [`hasMethod()`](#method-hasMethod) | Determines whether the specified method exists on the given class, including methods provided by attached behaviors. | `BehaviorMethodsClassReflectionExtension` |

## Method details

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

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

Creates a new instance of the [`BehaviorMethodsClassReflectionExtension`](/docs/extensions/phpstan/reference/method/behaviormethodsclassreflectionextension/index.md) class.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$reflectionProvider` | `PHPStan\Reflection\ReflectionProvider` | Reflection provider for class and property lookups. |
| `$serviceMap` | [`yii2\extensions\phpstan\ServiceMap`](/docs/extensions/phpstan/reference/servicemap/index.md) | Service and component map for Yii Application static analysis. |
| `$dependencyTracker` | `PHPStan\Analyser\DeclarationDependencyTracker` | Records the configuration values each class declaration depends on. |

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

```php
public function getMethod(PHPStan\Reflection\ClassReflection $classReflection, string $methodName): PHPStan\Reflection\MethodReflection
```

Retrieves the reflection of a method provided by a behavior attached to the given class.

PHPStan calls this method only after [`hasMethod()`](/docs/extensions/phpstan/reference/method/behaviormethodsclassreflectionextension/index.md#method-hasMethod) returned `true` for the same class and method name.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$classReflection` | `PHPStan\Reflection\ClassReflection` | Reflection of the class being analyzed. |
| `$methodName` | `string` | Name of the method to resolve. |

**Returns**

`PHPStan\Reflection\MethodReflection`: Reflection instance for the resolved method.

**Throws**

- `PHPStan\ShouldNotHappenException`: if no attached behavior provides the method.

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

```php
public function hasMethod(PHPStan\Reflection\ClassReflection $classReflection, string $methodName): bool
```

Determines whether the specified method exists on the given class, including methods provided by attached
behaviors.

Checks if the class is a subclass of [`Component`](/docs/api/yii/base/component/index.md) and doesn't already declare the method natively. If so,
inspect all behaviors attached to the class to determine if any provide the requested method.

This enables PHPStan to recognize available methods from behaviors as if they were natively declared on the
component class, supporting accurate static analysis and autocompletion.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$classReflection` | `PHPStan\Reflection\ClassReflection` | Reflection of the class being analyzed. |
| `$methodName` | `string` | Name of the method to check for existence. |

**Returns**

`bool`: `true` if the method exists on the class via an attached behavior; `false` otherwise.