Skip to content

Commit

Permalink
[FrameworkExtraBundle] extract arguments classes via type api
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Dec 20, 2023
1 parent fef24e0 commit 78bd771
Showing 1 changed file with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ public function process(ContainerBuilder $container): void
$priority = 0;
}

$emailType = (new \ReflectionMethod($class, $methodName))->getParameters()[0]->getClass()->name;
$emailWriterListenerDefinition
->addMethodCall('addWriter', [$emailType, $id, $methodName, $priority]);
foreach ($this->getClasses((new \ReflectionMethod($class, $methodName))->getParameters()[0]) as $emailType) {
$emailWriterListenerDefinition
->addMethodCall('addWriter', [$emailType, $id, $methodName, $priority]);
}
}
}

Expand All @@ -44,4 +45,27 @@ public function process(ContainerBuilder $container): void
ServiceLocatorTagPass::register($container, $writers)
);
}

/**
* Extract classes base on union and name type.
*
* @return array<class-string>
*/
private function getClasses(\ReflectionParameter $reflectionParameter): array
{
$type = $reflectionParameter->getType();

if ($type instanceof \ReflectionNamedType) {
return [$type->getName()];
}

if ($type instanceof \ReflectionUnionType) {
return array_map(
fn (\ReflectionNamedType $type) => $type->getName(),
$type->getTypes()
);
}

throw new \InvalidArgumentException('Unable to extract classes from parameter. Only named type and union type are supported.');
}
}

0 comments on commit 78bd771

Please sign in to comment.