Skip to content

Commit

Permalink
Fixes for Psalm
Browse files Browse the repository at this point in the history
  • Loading branch information
suhock committed Aug 9, 2023
1 parent 2515ad0 commit f534e76
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ interface ContainerSingletonBuilderInterface
/**
* @template TClass of object
* @template TImplementation of TClass
*
* @param class-string<TClass> $className The fully qualified name of the class to add
* @param class-string<TImplementation>|object|null $source
*
* - If null, indicates that the container should provide an instance of the given class by autowiring its
* constructor.
* - If a string, indicates that the container should provide an instance of the given class by retrieving an
Expand Down Expand Up @@ -59,7 +61,9 @@ public function addSingletonInstanceProvider(
* constructor. An optional mutator function can be specified to perform additional initialization on the
* constructed object.
*
* @param class-string $className The fully qualified name of the class to add
* @template TClass of object
*
* @param class-string<TClass> $className The fully qualified name of the class to add
* @param callable|null $mutator [optional] This function will be called after an instance of the class has been
* created. The class instance will be provided as the first parameter. Any additional parameters will be injected.
*
Expand Down Expand Up @@ -91,7 +95,9 @@ public function addSingletonImplementation(string $className, string $implementa
* Indicates that the container should provide a singleton instance of the given class by calling the specified
* factory method.
*
* @param class-string $className The fully qualified name of the class to add
* @template TClass of object
*
* @param class-string<TClass> $className The fully qualified name of the class to add
* @param callable $factory A factory method that returns an instance of the class specified by {@see $className}.
* Any method parameters will be injected.
*
Expand Down
18 changes: 16 additions & 2 deletions src/Suhock/DependencyInjection/ContainerSingletonBuilderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Suhock\DependencyInjection;

use Closure;
use Suhock\DependencyInjection\Lifetime\SingletonStrategy;
use Suhock\DependencyInjection\Provision\InstanceProviderFactory;
use Suhock\DependencyInjection\Provision\InstanceProviderInterface;
Expand All @@ -23,6 +24,15 @@
*/
trait ContainerSingletonBuilderTrait
{
/**
* @template TClass of object
* @template TImplementation of TClass
*
* @param class-string<TClass> $className
* @param class-string<TImplementation>|TClass|Closure|null $source
*
* @return $this
*/
public function addSingleton(string $className, string|object|null $source = null): static
{
$this->addSingletonInstanceProvider(
Expand Down Expand Up @@ -51,7 +61,9 @@ public function addSingletonInstanceProvider(string $className, InstanceProvider
abstract protected function getInjector(): InjectorInterface;

/**
* @param class-string $className
* @template TClass of object
*
* @param class-string<TClass> $className
*/
public function addSingletonClass(string $className, ?callable $mutator = null): static
{
Expand Down Expand Up @@ -80,7 +92,9 @@ public function addSingletonImplementation(string $className, string $implementa
}

/**
* @param class-string $className
* @template TClass of object
*
* @param class-string<TClass> $className
*/
public function addSingletonFactory(string $className, callable $factory): static
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ interface ContainerTransientBuilderInterface
/**
* @template TClass of object
* @template TImplementation of TClass
*
* @param class-string<TClass> $className The fully qualified name of the class to add
* @param class-string<TImplementation>|Closure|null $source
*
* - If null, indicates that the container should provide an instance of the given class by autowiring its
* constructor.
* - If a string, indicates that the container should provide an instance of the given class by retrieving an
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ trait ContainerTransientBuilderTrait
{
abstract protected function getInjector(): InjectorInterface;

/**
* @template TClass of object
* @template TImplementation of TClass
*
* @param class-string<TClass> $className
* @param class-string<TImplementation>|Closure|null $source
*
* @return $this
*/
public function addTransient(string $className, string|Closure|null $source = null): static
{
$this->addSingletonInstanceProvider(
Expand Down
3 changes: 1 addition & 2 deletions src/Suhock/DependencyInjection/Injector.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ private function injectAutowireFunctions(object $instance): void

foreach ($rClass->getMethods(ReflectionMethod::IS_PUBLIC) as $rMethod) {
if (count($rMethod->getAttributes(Autowire::class)) > 0) {
/** @phpstan-ignore-next-line PHPStan complains about possible null return */
$this->call($rMethod->getClosure($instance));
$this->call($rMethod->getClosure($instance) ?? throw new InjectorException());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public function get(): object
}

/**
* @param callable $function The function to test
* @param Closure|string $function The function to test
* @psalm-param Closure|callable-string $function
* @param class-string $className The name of the class that the function must be able to mutate
*
* @return bool true if the function can mutate the specified class; otherwise, false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@

final class InstanceProviderFactory
{
/**
* @template TClass of object
* @template TImplementation of TClass
*
* @param InjectorInterface $injector
* @param ContainerInterface $container
* @param class-string<TClass> $className
* @param class-string<TImplementation>|TImplementation|null $source
*
* @return InstanceProviderInterface<TClass>|InstanceProviderInterface<TImplementation>
*/
public static function createInstanceProvider(
InjectorInterface $injector,
ContainerInterface $container,
Expand All @@ -43,6 +54,15 @@ public static function createInstanceProvider(
return self::createClosureInstanceProvider($injector, $className, $source);
}

/**
* @template TClass of object
*
* @param InjectorInterface $injector
* @param class-string<TClass> $className
* @param callable|null $mutator
*
* @return ClassInstanceProvider<TClass>
*/
public static function createClassInstanceProvider(
InjectorInterface $injector,
string $className,
Expand All @@ -51,6 +71,16 @@ public static function createClassInstanceProvider(
return new ClassInstanceProvider($className, $injector, $mutator);
}

/**
* @template TClass of object
* @template TImplementation of TClass
*
* @param ContainerInterface $container
* @param class-string<TClass> $className
* @param class-string<TImplementation> $implementationClassName
*
* @return ImplementationInstanceProvider<TClass, TImplementation>
*/
public static function createImplementationInstanceProvider(
ContainerInterface $container,
string $className,
Expand All @@ -59,13 +89,31 @@ public static function createImplementationInstanceProvider(
return new ImplementationInstanceProvider($className, $implementationClassName, $container);
}

/**
* @template TClass of object
* @template TInstance of TClass
*
* @param class-string<TClass> $className
* @param TInstance $object
*
* @return ObjectInstanceProvider<TClass>
*/
public static function createObjectInstanceProvider(
string $className,
object $object
): ObjectInstanceProvider {
return new ObjectInstanceProvider($className, $object);
}

/**
* @template TClass of object
*
* @param InjectorInterface $injector
* @param class-string<TClass> $className
* @param callable $closure
*
* @return ClosureInstanceProvider<TClass>
*/
public static function createClosureInstanceProvider(
InjectorInterface $injector,
string $className,
Expand Down

0 comments on commit f534e76

Please sign in to comment.