Skip to content
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
41 changes: 28 additions & 13 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,11 @@ public function extend($abstract, Closure $closure)
/**
* Register an existing instance as shared in the container.
*
* @template TInstance of mixed
*
* @param string $abstract
* @param mixed $instance
* @return mixed
* @param TInstance $instance
* @return TInstance
*/
public function instance($abstract, $instance)
{
Expand Down Expand Up @@ -719,8 +721,10 @@ protected function getClassForCallable($callback)
/**
* Get a closure to resolve the given type from the container.
*
* @param string $abstract
* @return \Closure
* @template TClass
*
* @param string|class-string<TClass> $abstract
* @return ($abstract is class-string<TClass> ? \Closure(): TClass : \Closure(): mixed)
*/
public function factory($abstract)
{
Expand All @@ -730,9 +734,11 @@ public function factory($abstract)
/**
* An alias function name for make().
*
* @param string|callable $abstract
* @template TClass
*
* @param string|class-string<TClass>|callable $abstract
* @param array $parameters
* @return mixed
* @return ($abstract is class-string<TClass> ? TClass : mixed)
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
Expand All @@ -744,9 +750,11 @@ public function makeWith($abstract, array $parameters = [])
/**
* Resolve the given type from the container.
*
* @param string $abstract
* @template TClass
*
* @param string|class-string<TClass> $abstract
* @param array $parameters
* @return mixed
* @return ($abstract is class-string<TClass> ? TClass : mixed)
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
Expand All @@ -758,7 +766,10 @@ public function make($abstract, array $parameters = [])
/**
* {@inheritdoc}
*
* @return mixed
* @template TClass
*
* @param string|class-string<TClass> $id
* @return ($id is class-string<TClass> ? TClass : mixed)
*/
public function get(string $id)
{
Expand All @@ -776,10 +787,12 @@ public function get(string $id)
/**
* Resolve the given type from the container.
*
* @param string|callable $abstract
* @template TClass
*
* @param string|class-string<TClass>|callable $abstract
* @param array $parameters
* @param bool $raiseEvents
* @return mixed
* @return ($abstract is class-string<TClass> ? TClass : mixed)
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
* @throws \Illuminate\Contracts\Container\CircularDependencyException
Expand Down Expand Up @@ -919,8 +932,10 @@ protected function isBuildable($concrete, $abstract)
/**
* Instantiate a concrete instance of the given type.
*
* @param \Closure|string $concrete
* @return mixed
* @template TClass
*
* @param \Closure(static, array): TClass|class-string<TClass> $concrete
* @return TClass
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
* @throws \Illuminate\Contracts\Container\CircularDependencyException
Expand Down
28 changes: 22 additions & 6 deletions src/Illuminate/Contracts/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@

interface Container extends ContainerInterface
{
/**
* {@inheritdoc}
*
* @template TClass
*
* @param string|class-string<TClass> $id
* @return ($id is class-string<TClass> ? TClass : mixed)
*/
public function get(string $id);

/**
* Determine if the given abstract type has been bound.
*
Expand Down Expand Up @@ -122,9 +132,11 @@ public function extend($abstract, Closure $closure);
/**
* Register an existing instance as shared in the container.
*
* @template TInstance of mixed
*
* @param string $abstract
* @param mixed $instance
* @return mixed
* @param TInstance $instance
* @return TInstance
*/
public function instance($abstract, $instance);

Expand All @@ -149,8 +161,10 @@ public function when($concrete);
/**
* Get a closure to resolve the given type from the container.
*
* @param string $abstract
* @return \Closure
* @template TClass
*
* @param string|class-string<TClass> $abstract
* @return ($abstract is class-string<TClass> ? \Closure(): TClass : \Closure(): mixed)
*/
public function factory($abstract);

Expand All @@ -164,9 +178,11 @@ public function flush();
/**
* Resolve the given type from the container.
*
* @param string $abstract
* @template TClass
*
* @param string|class-string<TClass> $abstract
* @param array $parameters
* @return mixed
* @return ($abstract is class-string<TClass> ? TClass : mixed)
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
Expand Down
12 changes: 8 additions & 4 deletions src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -1043,9 +1043,11 @@ public function registerDeferredProvider($provider, $service = null)
/**
* Resolve the given type from the container.
*
* @param string $abstract
* @template TClass
*
* @param string|class-string<TClass> $abstract
* @param array $parameters
* @return mixed
* @return ($abstract is class-string<TClass> ? TClass : mixed)
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
Expand All @@ -1059,10 +1061,12 @@ public function make($abstract, array $parameters = [])
/**
* Resolve the given type from the container.
*
* @param string $abstract
* @template TClass
*
* @param string|class-string<TClass>|callable $abstract
* @param array $parameters
* @param bool $raiseEvents
* @return mixed
* @return ($abstract is class-string<TClass> ? TClass : mixed)
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
* @throws \Illuminate\Contracts\Container\CircularDependencyException
Expand Down
30 changes: 30 additions & 0 deletions types/Container/Container.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Config\Repository;
use Illuminate\Container\Container;

use function PHPStan\Testing\assertType;

$container = resolve(Container::class);

assertType('stdClass', $container->instance('foo', new stdClass));

assertType('mixed', $container->get('foo'));
assertType('Illuminate\Config\Repository', $container->get(Repository::class));

assertType('Closure(): mixed', $container->factory('foo'));
assertType('Closure(): Illuminate\Config\Repository', $container->factory(Repository::class));

assertType('mixed', $container->make('foo'));
assertType('Illuminate\Config\Repository', $container->make(Repository::class));

assertType('mixed', $container->makeWith('foo'));
assertType('Illuminate\Config\Repository', $container->makeWith(Repository::class));

assertType('Illuminate\Config\Repository', $container->build(Repository::class));
assertType('Illuminate\Config\Repository', $container->build(function (Container $container, array $parameters) {
return new Repository($parameters);
}));
assertType('stdClass', $container->build(function () {
return new stdClass();
}));
19 changes: 19 additions & 0 deletions types/Contracts/Container/Container.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use Illuminate\Config\Repository;
use Illuminate\Contracts\Container\Container;

use function PHPStan\Testing\assertType;

$container = resolve(Container::class);

assertType('stdClass', $container->instance('foo', new stdClass));

assertType('mixed', $container->get('foo'));
assertType('Illuminate\Config\Repository', $container->get(Repository::class));

assertType('Closure(): mixed', $container->factory('foo'));
assertType('Closure(): Illuminate\Config\Repository', $container->factory(Repository::class));

assertType('mixed', $container->make('foo'));
assertType('Illuminate\Config\Repository', $container->make(Repository::class));
19 changes: 19 additions & 0 deletions types/Contracts/Foundation/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use Illuminate\Config\Repository;
use Illuminate\Contracts\Foundation\Application;

use function PHPStan\Testing\assertType;

$app = resolve(Application::class);

assertType('stdClass', $app->instance('foo', new stdClass));

assertType('mixed', $app->get('foo'));
assertType('Illuminate\Config\Repository', $app->get(Repository::class));

assertType('Closure(): mixed', $app->factory('foo'));
assertType('Closure(): Illuminate\Config\Repository', $app->factory(Repository::class));

assertType('mixed', $app->make('foo'));
assertType('Illuminate\Config\Repository', $app->make(Repository::class));
30 changes: 30 additions & 0 deletions types/Foundation/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Config\Repository;
use Illuminate\Foundation\Application;

use function PHPStan\Testing\assertType;

$app = resolve(Application::class);

assertType('stdClass', $app->instance('foo', new stdClass));

assertType('mixed', $app->get('foo'));
assertType('Illuminate\Config\Repository', $app->get(Repository::class));

assertType('Closure(): mixed', $app->factory('foo'));
assertType('Closure(): Illuminate\Config\Repository', $app->factory(Repository::class));

assertType('mixed', $app->make('foo'));
assertType('Illuminate\Config\Repository', $app->make(Repository::class));

assertType('mixed', $app->makeWith('foo'));
assertType('Illuminate\Config\Repository', $app->makeWith(Repository::class));

assertType('Illuminate\Config\Repository', $app->build(Repository::class));
assertType('Illuminate\Config\Repository', $app->build(function (Application $app, array $parameters) {
return new Repository($parameters);
}));
assertType('stdClass', $app->build(function () {
return new stdClass();
}));