Skip to content

Commit

Permalink
Support union types for mutator function detection
Browse files Browse the repository at this point in the history
  • Loading branch information
suhock committed Aug 10, 2023
1 parent 1c02905 commit 5f07b4a
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/Suhock/DependencyInjection/Provision/ClassInstanceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use ReflectionException;
use ReflectionFunction;
use ReflectionNamedType;
use ReflectionUnionType;
use Suhock\DependencyInjection\DependencyInjectionException;
use Suhock\DependencyInjection\InjectorInterface;

Expand Down Expand Up @@ -79,13 +80,22 @@ public static function isMutator(callable $function, string $className): bool

$firstParamType = $closureReflection->getParameters()[0]->getType();

if (!$firstParamType instanceof ReflectionNamedType || $firstParamType->isBuiltin()) {
return false;
}
$paramTypes = match (true) {
$firstParamType instanceof ReflectionNamedType => [$firstParamType],
$firstParamType instanceof ReflectionUnionType => $firstParamType->getTypes(),
default => []
};

/** @var class-string $firstParamTypeName */
$firstParamTypeName = $firstParamType->getName();
foreach ($paramTypes as $paramType) {
if ($paramType->isBuiltin()) {
continue;
}

if (is_a($className, $paramType->getName(), true)) {
return true;
}
}

return $firstParamTypeName === $className || is_subclass_of($className, $firstParamTypeName);
return false;
}
}

0 comments on commit 5f07b4a

Please sign in to comment.