Skip to content

Commit 5f07b4a

Browse files
committed
Support union types for mutator function detection
1 parent 1c02905 commit 5f07b4a

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/Suhock/DependencyInjection/Provision/ClassInstanceProvider.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use ReflectionException;
1616
use ReflectionFunction;
1717
use ReflectionNamedType;
18+
use ReflectionUnionType;
1819
use Suhock\DependencyInjection\DependencyInjectionException;
1920
use Suhock\DependencyInjection\InjectorInterface;
2021

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

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

82-
if (!$firstParamType instanceof ReflectionNamedType || $firstParamType->isBuiltin()) {
83-
return false;
84-
}
83+
$paramTypes = match (true) {
84+
$firstParamType instanceof ReflectionNamedType => [$firstParamType],
85+
$firstParamType instanceof ReflectionUnionType => $firstParamType->getTypes(),
86+
default => []
87+
};
8588

86-
/** @var class-string $firstParamTypeName */
87-
$firstParamTypeName = $firstParamType->getName();
89+
foreach ($paramTypes as $paramType) {
90+
if ($paramType->isBuiltin()) {
91+
continue;
92+
}
93+
94+
if (is_a($className, $paramType->getName(), true)) {
95+
return true;
96+
}
97+
}
8898

89-
return $firstParamTypeName === $className || is_subclass_of($className, $firstParamTypeName);
99+
return false;
90100
}
91101
}

0 commit comments

Comments
 (0)