Skip to content

feat: generic types for DI classes #20354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions framework/di/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,11 @@ 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 class-string
* @psalm-param class-string<T>|array{class: class-string<T>} $class
* @phpstan-param class-string<T>|array{class: class-string<T>} $class
* @psalm-return T
* @phpstan-return T
* @template T of object
* @psalm-param string|class-string<T>|Instance $class
* @phpstan-param string|class-string<T>|Instance $class
* @psalm-return ($class is class-string<T> ? T : object)
* @phpstan-return ($class is class-string<T> ? T : object)
*/
public function get($class, $params = [], $config = [])
{
Expand Down
6 changes: 6 additions & 0 deletions framework/di/Instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 object
* @psalm-param class-string<T>|null $type
* @phpstan-param class-string<T>|null $type
* @psalm-return ($type is null ? object : T)
* @phpstan-return ($type is null ? object : T)
*/
public static function ensure($reference, $type = null, $container = null)
{
Expand Down