[DependencyInjection] Factorize listing of setters

This commit is contained in:
Kévin Dunglas 2016-08-16 17:02:56 +02:00 committed by Fabien Potencier
parent 5cc9ed2048
commit 163fbf461f

View File

@ -78,15 +78,8 @@ class AutowirePass implements CompilerPassInterface
$metadata['__construct'] = self::getResourceMetadataForMethod($constructor);
}
// todo - when #17608 is merged, could refactor to private function to remove duplication
// of determining valid "setter" methods
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
$name = $reflectionMethod->getName();
if ($reflectionMethod->isStatic() || 1 !== $reflectionMethod->getNumberOfParameters() || 0 !== strpos($name, 'set')) {
continue;
}
$metadata[$name] = self::getResourceMetadataForMethod($reflectionMethod);
foreach (self::getSetters($reflectionClass) as $reflectionMethod) {
$metadata[$reflectionMethod->name] = self::getResourceMetadataForMethod($reflectionMethod);
}
return new AutowireServiceResource($reflectionClass->name, $reflectionClass->getFileName(), $metadata);
@ -119,13 +112,10 @@ class AutowirePass implements CompilerPassInterface
$methodsCalled[$methodCall[0]] = true;
}
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
$name = $reflectionMethod->getName();
if (isset($methodsCalled[$name]) || $reflectionMethod->isStatic() || 1 !== $reflectionMethod->getNumberOfParameters() || 0 !== strpos($name, 'set')) {
continue;
foreach (self::getSetters($reflectionClass) as $reflectionMethod) {
if (!isset($methodsCalled[$reflectionMethod->name])) {
$this->autowireMethod($id, $definition, $reflectionMethod, false);
}
$this->autowireMethod($id, $definition, $reflectionMethod, false);
}
}
@ -386,6 +376,20 @@ class AutowirePass implements CompilerPassInterface
$this->ambiguousServiceTypes[$type][] = $id;
}
/**
* @param \ReflectionClass $reflectionClass
*
* @return \ReflectionMethod[]
*/
private static function getSetters(\ReflectionClass $reflectionClass)
{
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
if (!$reflectionMethod->isStatic() && 1 === $reflectionMethod->getNumberOfParameters() && 0 === strpos($reflectionMethod->name, 'set')) {
yield $reflectionMethod;
}
}
}
private static function getResourceMetadataForMethod(\ReflectionMethod $method)
{
$methodArgumentsMetadata = array();