From a545bd5fd87031f72194ec583004cf6e864612d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=A1=D0=BF=D0=B8?= =?UTF-8?q?=D1=80=D0=BA=D0=BE=D0=B2?= Date: Thu, 17 Apr 2025 11:46:43 +0400 Subject: [PATCH 1/5] feat: generic types for DI classes --- framework/di/Container.php | 8 ++++---- framework/di/Instance.php | 6 ++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/framework/di/Container.php b/framework/di/Container.php index 817f0b0a23a..f4096b45e55 100644 --- a/framework/di/Container.php +++ b/framework/di/Container.php @@ -160,10 +160,10 @@ class Container extends Component * * * @template T of class-string - * @psalm-param class-string|array{class: class-string} $class - * @phpstan-param class-string|array{class: class-string} $class - * @psalm-return T - * @phpstan-return T + * @psalm-param class-string|Instance $class + * @phpstan-param class-string|Instance $class + * @psalm-return ($class is class-string ? T : object) + * @phpstan-return ($class is class-string ? T : object) */ public function get($class, $params = [], $config = []) { diff --git a/framework/di/Instance.php b/framework/di/Instance.php index edecbe9be80..64073ab1a1b 100644 --- a/framework/di/Instance.php +++ b/framework/di/Instance.php @@ -114,6 +114,12 @@ public static function of($id, $optional = false) * @param ServiceLocator|Container|null $container the container. This will be passed to [[get()]]. * @return object the object referenced by the Instance, or `$reference` itself if it is an object. * @throws InvalidConfigException if the reference is invalid + * + * @template T of class-string + * @psalm-param class-string|null $type + * @phpstan-param class-string|null $type + * @psalm-return ($type is class-string ? T : object) + * @phpstan-return ($type is class-string ? T : object) */ public static function ensure($reference, $type = null, $container = null) { From d434bf906a89162cbeaaa25118727444302a6cef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=A1=D0=BF=D0=B8?= =?UTF-8?q?=D1=80=D0=BA=D0=BE=D0=B2?= Date: Thu, 17 Apr 2025 12:21:51 +0400 Subject: [PATCH 2/5] fix: Unable to resolve the template type T in call to method static method --- framework/di/Instance.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/framework/di/Instance.php b/framework/di/Instance.php index 64073ab1a1b..020e8171829 100644 --- a/framework/di/Instance.php +++ b/framework/di/Instance.php @@ -115,11 +115,11 @@ public static function of($id, $optional = false) * @return object the object referenced by the Instance, or `$reference` itself if it is an object. * @throws InvalidConfigException if the reference is invalid * - * @template T of class-string + * @template T of object * @psalm-param class-string|null $type * @phpstan-param class-string|null $type - * @psalm-return ($type is class-string ? T : object) - * @phpstan-return ($type is class-string ? T : object) + * @psalm-return ($type is null ? object : T) + * @phpstan-return ($type is null ? object : T) */ public static function ensure($reference, $type = null, $container = null) { From b1334f04e9603fb568e6a5957ee4a8151d04d7e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=A1=D0=BF=D0=B8?= =?UTF-8?q?=D1=80=D0=BA=D0=BE=D0=B2?= Date: Thu, 17 Apr 2025 13:54:51 +0400 Subject: [PATCH 3/5] fix: Unable to resolve the template type T --- framework/di/Container.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/framework/di/Container.php b/framework/di/Container.php index f4096b45e55..7d4d56ac441 100644 --- a/framework/di/Container.php +++ b/framework/di/Container.php @@ -159,11 +159,11 @@ class Container extends Component * @throws NotInstantiableException If resolved to an abstract class or an interface (since 2.0.9) * * - * @template T of class-string + * @template T of object * @psalm-param class-string|Instance $class * @phpstan-param class-string|Instance $class - * @psalm-return ($class is class-string ? T : object) - * @phpstan-return ($class is class-string ? T : object) + * @psalm-return ($class is Instance ? object : T) + * @phpstan-return ($class is Instance ? object : T) */ public function get($class, $params = [], $config = []) { From 31ae93a9a2161352943a28ee6d70a6b7862d8a8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=A1=D0=BF=D0=B8?= =?UTF-8?q?=D1=80=D0=BA=D0=BE=D0=B2?= Date: Thu, 17 Apr 2025 14:06:17 +0400 Subject: [PATCH 4/5] support string in Container::get --- framework/di/Container.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/framework/di/Container.php b/framework/di/Container.php index 7d4d56ac441..9c4a87ba1e5 100644 --- a/framework/di/Container.php +++ b/framework/di/Container.php @@ -160,10 +160,10 @@ class Container extends Component * * * @template T of object - * @psalm-param class-string|Instance $class - * @phpstan-param class-string|Instance $class - * @psalm-return ($class is Instance ? object : T) - * @phpstan-return ($class is Instance ? object : T) + * @psalm-param string|class-string|Instance $class + * @phpstan-param string|class-string|Instance $class + * @psalm-return ($class is class-string ? T : object) + * @phpstan-return ($class is class-string ? T : object) */ public function get($class, $params = [], $config = []) { From ba569b615ebfc9998c3e43dd3697f4b628ffea0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=A1=D0=BF=D0=B8?= =?UTF-8?q?=D1=80=D0=BA=D0=BE=D0=B2?= Date: Sat, 19 Apr 2025 22:44:28 +0400 Subject: [PATCH 5/5] run checks --- framework/di/Container.php | 1 - 1 file changed, 1 deletion(-) diff --git a/framework/di/Container.php b/framework/di/Container.php index 9c4a87ba1e5..1d34887f45e 100644 --- a/framework/di/Container.php +++ b/framework/di/Container.php @@ -158,7 +158,6 @@ class Container extends Component * @throws InvalidConfigException if the class cannot be recognized or correspond to an invalid definition * @throws NotInstantiableException If resolved to an abstract class or an interface (since 2.0.9) * - * * @template T of object * @psalm-param string|class-string|Instance $class * @phpstan-param string|class-string|Instance $class