Fix #20650: Add PHPStan/Psalm annotations for yii\di\Container

This commit is contained in:
Maksim Spirkov
2025-10-29 14:43:20 +03:00
committed by GitHub
parent 5dcf4b853d
commit 66d9db17bc
2 changed files with 26 additions and 1 deletions

View File

@@ -73,6 +73,7 @@ Yii Framework 2 Change Log
- Bug #20639: Add missing generics in `yii\web` namespace (mspirkov) - Bug #20639: Add missing generics in `yii\web` namespace (mspirkov)
- Bug #20645: Add missing generics in `yii\helpers` and `yii\test` namespaces. Fix PHPDoc annotations in `ArrayAccessTrait` (mspirkov) - Bug #20645: Add missing generics in `yii\helpers` and `yii\test` namespaces. Fix PHPDoc annotations in `ArrayAccessTrait` (mspirkov)
- Bug #20640: Fix `@param` annotation for `$block` in `yii\console\Markdown::renderParagraph()` (mspirkov) - Bug #20640: Fix `@param` annotation for `$block` in `yii\console\Markdown::renderParagraph()` (mspirkov)
- Enh #20650: Add PHPStan/Psalm annotations for `yii\di\Container` (mspirkov)
- Bug #20654: Add missing generics in `yii\db` namespace. Fix PHPDoc annotations in `yii\db\ArrayExpression` (mspirkov) - Bug #20654: Add missing generics in `yii\db` namespace. Fix PHPDoc annotations in `yii\db\ArrayExpression` (mspirkov)
- Bug #20651: Add missing generics in `yii\filters` namespace (mspirkov) - Bug #20651: Add missing generics in `yii\filters` namespace (mspirkov)

View File

@@ -124,6 +124,8 @@ class Container extends Component
/** /**
* @var array cached dependencies indexed by class/interface names. Each class name * @var array cached dependencies indexed by class/interface names. Each class name
* is associated with a list of constructor parameter types or default values. * is associated with a list of constructor parameter types or default values.
*
* @phpstan-var array<class-string, array<string, mixed>>
*/ */
private $_dependencies = []; private $_dependencies = [];
/** /**
@@ -386,10 +388,21 @@ class Container extends Component
* @param array $config configurations to be applied to the new instance * @param array $config configurations to be applied to the new instance
* @return object the newly created instance of the specified class * @return object the newly created instance of the specified class
* @throws NotInstantiableException If resolved to an abstract class or an interface (since 2.0.9) * @throws NotInstantiableException If resolved to an abstract class or an interface (since 2.0.9)
*
* @template T of object
*
* @phpstan-param class-string<T> $class
* @psalm-param class-string<T> $class
*
* @phpstan-return T
* @psalm-return T
*/ */
protected function build($class, $params, $config) protected function build($class, $params, $config)
{ {
/** @var ReflectionClass $reflection */ /**
* @var ReflectionClass $reflection
* @phpstan-var ReflectionClass<T> $reflection
*/
list($reflection, $dependencies) = $this->getDependencies($class); list($reflection, $dependencies) = $this->getDependencies($class);
$addDependencies = []; $addDependencies = [];
@@ -503,6 +516,14 @@ class Container extends Component
* @param string $class class name, interface name or alias name * @param string $class class name, interface name or alias name
* @return array the dependencies of the specified class. * @return array the dependencies of the specified class.
* @throws NotInstantiableException if a dependency cannot be resolved or if a dependency cannot be fulfilled. * @throws NotInstantiableException if a dependency cannot be resolved or if a dependency cannot be fulfilled.
*
* @template T of object
*
* @phpstan-param class-string<T> $class
* @psalm-param class-string<T> $class
*
* @phpstan-return array{ReflectionClass<T>, array<string, mixed>}
* @psalm-return array{ReflectionClass<T>, array<string, mixed>}
*/ */
protected function getDependencies($class) protected function getDependencies($class)
{ {
@@ -590,6 +611,9 @@ class Container extends Component
* @param ReflectionClass|null $reflection the class reflection associated with the dependencies * @param ReflectionClass|null $reflection the class reflection associated with the dependencies
* @return array the resolved dependencies * @return array the resolved dependencies
* @throws InvalidConfigException if a dependency cannot be resolved or if a dependency cannot be fulfilled. * @throws InvalidConfigException if a dependency cannot be resolved or if a dependency cannot be fulfilled.
*
* @phpstan-param ReflectionClass<object>|null $reflection
* @psalm-param ReflectionClass<object>|null $reflection
*/ */
protected function resolveDependencies($dependencies, $reflection = null) protected function resolveDependencies($dependencies, $reflection = null)
{ {