Skip to content

Commit

Permalink
tweak: remove unused code after testing in real-world app
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Oct 26, 2023
1 parent 98dbecc commit e8d2460
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions src/Injector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ public function __construct(
) {
}

/** @param array<string, object> $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<string, mixed> $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 = [];

Expand All @@ -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) {
Expand Down

0 comments on commit e8d2460

Please sign in to comment.