+
+ if ($constructor) {
+ array_unshift($methodCalls, array($constructor, $value->getArguments()));
+ }
+
+ $methodCalls = $this->autowireCalls($reflectionClass, $methodCalls);
+
+ if ($constructor) {
+ list(, $arguments) = array_shift($methodCalls);
+
+ if ($arguments !== $value->getArguments()) {
+ $value->setArguments($arguments);
+ }
+ }
+
+ if ($methodCalls !== $value->getMethodCalls()) {
+ $value->setMethodCalls($methodCalls);
+ }
+
+ return $value;
+ }
+
+ /**
+ * @param \ReflectionClass $reflectionClass
+ * @param array $methodCalls
+ *
+ * @return array
+ */
+ private function autowireCalls(\ReflectionClass $reflectionClass, array $methodCalls)
+ {
+ foreach ($methodCalls as $i => $call) {
+ list($method, $arguments) = $call;
+
+ if ($method instanceof \ReflectionFunctionAbstract) {
+ $reflectionMethod = $method;
+ } else {
+ $reflectionMethod = $this->getReflectionMethod(new Definition($reflectionClass->name), $method);
+ }
+
+ $arguments = $this->autowireMethod($reflectionMethod, $arguments);
+
+ if ($arguments !== $call[1]) {
+ $methodCalls[$i][1] = $arguments;
+ }
+ }
+
+ return $methodCalls;
+ }
+
+ /**
+ * Autowires the constructor or a method.
+ *
+ * @param \ReflectionFunctionAbstract $reflectionMethod
+ * @param array $arguments
+ *
+ * @return array The autowired arguments
+ *
+ * @throws AutowiringFailedException
+ */
+ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, array $arguments)
+ {
+ $class = $reflectionMethod instanceof \ReflectionMethod ? $reflectionMethod->class : $this->currentId;
+ $method = $reflectionMethod->name;
+ $parameters = $reflectionMethod->getParameters();
+ if (method_exists('ReflectionMethod', 'isVariadic') && $reflectionMethod->isVariadic()) {