Skip to content

Commit 8d9c1cf

Browse files
committed
minor #19638 [DependencyInjection] Factorize listing of setters (dunglas)
This PR was squashed before being merged into the 3.2-dev branch (closes #19638). Discussion ---------- [DependencyInjection] Factorize listing of setters | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a Add a new method to list setters of a class. Commits ------- 163fbf4 [DependencyInjection] Factorize listing of setters
2 parents acbd4ac + 6ac4d31 commit 8d9c1cf

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

Compiler/AutowirePass.php

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

75-
// todo - when #17608 is merged, could refactor to private function to remove duplication
76-
// of determining valid "setter" methods
77-
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
78-
$name = $reflectionMethod->getName();
79-
if ($reflectionMethod->isStatic() || 1 !== $reflectionMethod->getNumberOfParameters() || 0 !== strpos($name, 'set')) {
80-
continue;
81-
}
82-
83-
$metadata[$name] = self::getResourceMetadataForMethod($reflectionMethod);
75+
foreach (self::getSetters($reflectionClass) as $reflectionMethod) {
76+
$metadata[$reflectionMethod->name] = self::getResourceMetadataForMethod($reflectionMethod);
8477
}
8578

8679
return new AutowireServiceResource($reflectionClass->name, $reflectionClass->getFileName(), $metadata);
@@ -113,13 +106,10 @@ private function completeDefinition($id, Definition $definition)
113106
$methodsCalled[$methodCall[0]] = true;
114107
}
115108

116-
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
117-
$name = $reflectionMethod->getName();
118-
if (isset($methodsCalled[$name]) || $reflectionMethod->isStatic() || 1 !== $reflectionMethod->getNumberOfParameters() || 0 !== strpos($name, 'set')) {
119-
continue;
109+
foreach (self::getSetters($reflectionClass) as $reflectionMethod) {
110+
if (!isset($methodsCalled[$reflectionMethod->name])) {
111+
$this->autowireMethod($id, $definition, $reflectionMethod, false);
120112
}
121-
122-
$this->autowireMethod($id, $definition, $reflectionMethod, false);
123113
}
124114
}
125115

@@ -380,6 +370,20 @@ private function addServiceToAmbiguousType($id, $type)
380370
$this->ambiguousServiceTypes[$type][] = $id;
381371
}
382372

373+
/**
374+
* @param \ReflectionClass $reflectionClass
375+
*
376+
* @return \ReflectionMethod[]
377+
*/
378+
private static function getSetters(\ReflectionClass $reflectionClass)
379+
{
380+
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
381+
if (!$reflectionMethod->isStatic() && 1 === $reflectionMethod->getNumberOfParameters() && 0 === strpos($reflectionMethod->name, 'set')) {
382+
yield $reflectionMethod;
383+
}
384+
}
385+
}
386+
383387
private static function getResourceMetadataForMethod(\ReflectionMethod $method)
384388
{
385389
$methodArgumentsMetadata = array();

0 commit comments

Comments
 (0)