From e8d2460887c4717e9b7dee62565cc37964ee670f Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Thu, 26 Oct 2023 10:07:04 +0100 Subject: [PATCH] tweak: remove unused code after testing in real-world app --- src/Injector.php | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/Injector.php b/src/Injector.php index 54e5c74..bca9c75 100644 --- a/src/Injector.php +++ b/src/Injector.php @@ -11,11 +11,19 @@ public function __construct( ) { } - /** @param array $extraArgs */ + /** + * @param object|null $instance The instance of the object containing + * the method to invoke. + * @param string $functionName The method name to invoke. + * @param array $extraArgs An associative array where the + * keys will match the method parameters by *name*, for passing values + * of PHP's inbuilt types like scalar values. + * @return mixed The return value of the invoked method. + */ public function invoke( ?object $instance, string|callable $functionName, - array $extraArgs = [], + array $extraArgs = [] ):mixed { $arguments = []; @@ -30,23 +38,12 @@ public function invoke( foreach($refFunction->getParameters() as $refParam) { /** @var ReflectionNamedType|null $refType */ $refType = $refParam->getType(); + $refParamTypeName = $refType->getName(); -// Check if we have a match in $extraArgs, otherwise get from the container: - /** @var class-string $className */ - $className = $refType->getName(); - if(array_key_exists($className, $extraArgs)) { - array_push( - $arguments, - $extraArgs[$className], - ); - } - else { - array_push( - $arguments, - $this->container->get($className) - ); - } - + array_push( + $arguments, + $extraArgs[$refParamTypeName] ?? $this->container->get($refType->getName()) + ); } if($instance) {