MNT Update generic types for Injector

- IDEs don't resolve the conditional return types phpstan provides, remove the conditional return types and add them as stubs in order to pass the test cases
This commit is contained in:
Cameron Bryers 2023-11-16 11:08:29 +13:00
parent 0f2e325e84
commit ba0f8910d6
No known key found for this signature in database
GPG Key ID: 8B8933A3D208A3F4
4 changed files with 80 additions and 11 deletions

View File

@ -967,13 +967,12 @@ class Injector implements ContainerInterface
* @throws NotFoundExceptionInterface No entry was found for **this** identifier.
*
* @template T of object
* @param class-string<T> $name The name of the service to retrieve. If not a registered
* @param class-string<T>|string $name The name of the service to retrieve. If not a registered
* service, then a class of the given name is instantiated
* @param bool $asSingleton If set to false a new instance will be returned.
* If true a singleton will be returned unless the spec is type=prototype'
* @param array $constructorArgs Args to pass in to the constructor. Note: Ignored for singletons
* @return mixed Instance of the specified object
* @phpstan-return ($name is class-string<T> ? T : mixed) Instance of the specified object
* @return T|mixed Instance of the specified object
*/
public function get($name, $asSingleton = true, $constructorArgs = [])
{
@ -1128,10 +1127,9 @@ class Injector implements ContainerInterface
* Additional parameters are passed through as
*
* @template T of object
* @param class-string<T> $name
* @param class-string<T>|string $name
* @param mixed ...$argument arguments to pass to the constructor
* @return mixed A new instance of the specified object
* @phpstan-return ($name is class-string<T> ? T : mixed) A new instance of the specified object
* @return T|mixed A new instance of the specified object
*/
public function create($name, $argument = null)
{
@ -1144,10 +1142,9 @@ class Injector implements ContainerInterface
* Creates an object with the supplied argument array
*
* @template T
* @param class-string<T> $name Name of the class to create an object of
* @param class-string<T>|string $name Name of the class to create an object of
* @param array $constructorArgs Arguments to pass to the constructor
* @return mixed
* @phpstan-return ($name is class-string<T> ? T : mixed)
* @return T|mixed
*/
public function createWithArgs($name, $constructorArgs)
{

View File

@ -24,4 +24,12 @@ class InjectorTypeTest extends TypeInferenceTestCase
{
$this->assertFileAsserts($assertType, $file, ...$args);
}
/**
* @return string[]
*/
public static function getAdditionalConfigFiles(): array
{
return [__DIR__ . '/phpstan.neon.dist'];
}
}

View File

@ -1,3 +1,5 @@
parameters:
bootstrapFiles:
- phpstan-bootstrap.php
bootstrapFiles:
- phpstan-bootstrap.php
stubFiles:
- stubs/SilverStripe/Core/Injector/Injector.stub

View File

@ -0,0 +1,62 @@
<?php
namespace SilverStripe\Core\Injector;
use Psr\Container\ContainerInterface;
class Injector implements ContainerInterface
{
/**
* Get a named managed object
*
* Will first check to see if the item has been registered as a configured service/bean
* and return that if so.
*
* Next, will check to see if there's any registered configuration for the given type
* and will then try and load that
*
* Failing all of that, will just return a new instance of the specified object.
*
* @throws NotFoundExceptionInterface No entry was found for **this** identifier.
*
* @template T of object
* @param class-string<T>|string $name The name of the service to retrieve. If not a registered
* service, then a class of the given name is instantiated
* @param bool $asSingleton If set to false a new instance will be returned.
* If true a singleton will be returned unless the spec is type=prototype'
* @param array $constructorArgs Args to pass in to the constructor. Note: Ignored for singletons
* @return T|mixed Instance of the specified object
* @phpstan-return ($name is class-string<T> ? T : mixed)
*/
public function get($name, $asSingleton = true, $constructorArgs = [])
{
}
/**
* Similar to get() but always returns a new object of the given type
*
* Additional parameters are passed through as
*
* @template T of object
* @param class-string<T>|string $name
* @param mixed ...$argument arguments to pass to the constructor
* @return T|mixed A new instance of the specified object
* @phpstan-return ($name is class-string<T> ? T : mixed) A new instance of the specified object
*/
public function create($name, $argument = null)
{
}
/**
* Creates an object with the supplied argument array
*
* @template T
* @param class-string<T>|string $name Name of the class to create an object of
* @param array $constructorArgs Arguments to pass to the constructor
* @return T|mixed
* @phpstan-return ($name is class-string<T> ? T : mixed)
*/
public function createWithArgs($name, $constructorArgs)
{
}
}