Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 00ba958

Browse files
committed
Refactor array_map
`resolveParameter()` now accepts the container and requested name, and returns a callback for resolving a parameter to a value, closing over the two provided values. This simplifies the array_map call, making it more readable.
1 parent 7d30314 commit 00ba958

File tree

1 file changed

+44
-36
lines changed

1 file changed

+44
-36
lines changed

src/Controller/LazyControllerAbstractFactory.php

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,10 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
114114
return new $requestedName();
115115
}
116116

117-
$parameters = array_map(function (ReflectionParameter $parameter) use ($container, $requestedName) {
118-
return $this->resolveParameter($parameter, $container, $requestedName);
119-
}, $reflectionParameters);
117+
$parameters = array_map(
118+
$this->resolveParameter($container, $requestedName),
119+
$reflectionParameters
120+
);
120121

121122
return new $requestedName(...$parameters);
122123
}
@@ -136,42 +137,49 @@ public function canCreate(ContainerInterface $container, $requestedName)
136137
/**
137138
* Resolve a parameter to a value.
138139
*
139-
* @param ReflectionClass $parameter
140+
* Returns a callback for resolving a parameter to a value.
141+
*
140142
* @param ContainerInterface $container
141143
* @param string $requestedName
142-
* @return mixed
143-
* @throws ServiceNotFoundException If type-hinted parameter cannot be
144-
* resolved to a service in the container.
144+
* @return callable
145145
*/
146-
private function resolveParameter(ReflectionParameter $parameter, ContainerInterface $container, $requestedName)
146+
private function resolveParameter(ContainerInterface $container, $requestedName)
147147
{
148-
if ($parameter->isArray()
149-
&& $parameter->getName() === 'config'
150-
&& $container->has('config')
151-
) {
152-
return $container->get('config');
153-
}
154-
155-
if ($parameter->isArray()) {
156-
return [];
157-
}
158-
159-
if (! $parameter->getClass()) {
160-
return;
161-
}
162-
163-
$type = $parameter->getClass()->getName();
164-
$type = isset($this->aliases[$type]) ? $this->aliases[$type] : $type;
165-
166-
if (! $container->has($type)) {
167-
throw new ServiceNotFoundException(sprintf(
168-
'Unable to create controller "%s"; unable to resolve parameter "%s" using type hint "%s"',
169-
$requestedName,
170-
$parameter->getName(),
171-
$type
172-
));
173-
}
174-
175-
return $container->get($type);
148+
/**
149+
* @param ReflectionClass $parameter
150+
* @return mixed
151+
* @throws ServiceNotFoundException If type-hinted parameter cannot be
152+
* resolved to a service in the container.
153+
*/
154+
return function (ReflectionParameter $parameter) use ($container, $requestedName) {
155+
if ($parameter->isArray()
156+
&& $parameter->getName() === 'config'
157+
&& $container->has('config')
158+
) {
159+
return $container->get('config');
160+
}
161+
162+
if ($parameter->isArray()) {
163+
return [];
164+
}
165+
166+
if (! $parameter->getClass()) {
167+
return;
168+
}
169+
170+
$type = $parameter->getClass()->getName();
171+
$type = isset($this->aliases[$type]) ? $this->aliases[$type] : $type;
172+
173+
if (! $container->has($type)) {
174+
throw new ServiceNotFoundException(sprintf(
175+
'Unable to create controller "%s"; unable to resolve parameter "%s" using type hint "%s"',
176+
$requestedName,
177+
$parameter->getName(),
178+
$type
179+
));
180+
}
181+
182+
return $container->get($type);
183+
};
176184
}
177185
}

0 commit comments

Comments
 (0)