diff --git a/src/Suhock/DependencyInjection/Provision/ClassInstanceProvider.php b/src/Suhock/DependencyInjection/Provision/ClassInstanceProvider.php index 17aff3a..fa80a5e 100644 --- a/src/Suhock/DependencyInjection/Provision/ClassInstanceProvider.php +++ b/src/Suhock/DependencyInjection/Provision/ClassInstanceProvider.php @@ -15,6 +15,7 @@ use ReflectionException; use ReflectionFunction; use ReflectionNamedType; +use ReflectionUnionType; use Suhock\DependencyInjection\DependencyInjectionException; use Suhock\DependencyInjection\InjectorInterface; @@ -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; } }