8000 [DependencyInjection] Factorize listing of setters · symfony/symfony@163fbf4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 163fbf4

Browse files
dunglasfabpot
authored andcommitted
[DependencyInjection] Factorize listing of setters
1 parent 5cc9ed2 commit 163fbf4

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,8 @@ public static function createResourceForClass(\ReflectionClass $reflectionClass)
7878
$metadata['__construct'] = self::getResourceMetadataForMethod($constructor);
7979
}
8080

81-
// todo - when #17608 is merged, could refactor to private function to remove duplication
82-
// of determining valid "setter" methods
83-
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
84-
$name = $reflectionMethod->getName();
85-
if ($reflectionMethod->isStatic() || 1 !== $reflectionMethod->getNumberOfParameters() || 0 !== strpos($name, 'set')) {
86-
continue;
87-
}
88-
89-
$metadata[$name] = self::getResourceMetadataForMethod($reflectionMethod);
81+
foreach (self::getSetters($reflectionClass) as $reflectionMethod) {
82+
$metadata[$reflectionMethod->name] = self::getResourceMetadataForMethod($reflectionMethod);
9083
}
9184

9285
return new AutowireServiceResource($reflectionClass->name, $reflectionClass->getFileName(), $metadata);
@@ -119,13 +112,10 @@ private function completeDefinition($id, Definition $definition)
119112
$methodsCalled[$methodCall[0]] = true;
120113
}
121114

122-
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
123-
$name = $reflectionMethod->getName();
124-
if (isset($methodsCalled[$name]) || $reflectionMethod->isStatic() || 1 !== $reflectionMethod->getNumberOfParameters() || 0 !== strpos($name, 'set')) {
125-
continue;
115+
foreach (self::getSetters($reflectionClass) as $reflectionMethod) {
116+
if (!isset($methodsCalled[$reflectionMethod->name])) {
117+
$this->autowireMethod($id, $definition, $reflectionMethod, false);
126118
}
127-
128-
$this->autowireMethod($id, $definition, $reflectionMethod, false);
129119
}
130120
}
131121

@@ -386,6 +376,20 @@ private function addServiceToAmbiguousType($id, $type)
386376
$this->ambiguousServiceTypes[$type][] = $id;
387377
}
388378

379+
/**
380+
* @param \ReflectionClass $reflectionClass
381+
*
382+
* @return \ReflectionMethod[]
383+
*/
384+
private static function getSetters(\ReflectionClass $reflectionClass)
385+
{
386+
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
387+
if (!$reflectionMethod->isStatic() && 1 === $reflectionMethod->getNumberOfParameters() && 0 === strpos($reflectionMethod->name, 'set')) {
388+
yield $reflectionMethod;
389+
}
390+
}
391+
}
392+
389393
private static function getResourceMetadataForMethod(\ReflectionMethod $method)
390394
{
391395
$methodArgumentsMetadata = array();

0 commit comments

Comments
 (0)
0