Skip to content

Commit 39f445e

Browse files
committed
- improvement: uses match()
- improvement: do not create a storage variable if not needed
1 parent e25f51e commit 39f445e

1 file changed

Lines changed: 9 additions & 12 deletions

File tree

DIReflector.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,13 @@ public function processMethodArguments(
7272
*/
7373
public function newMethodFromCallable(callable $callable): ReflectionFunctionAbstract
7474
{
75-
switch (\gettype($callable)) {
76-
case 'array':
77-
return new ReflectionMethod(...$callable);
78-
case 'object':
79-
if ($callable instanceof Closure) {
80-
return new ReflectionFunction($callable);
81-
}
82-
return (new ReflectionClass($callable))->getMethod('__invoke');
83-
default:
84-
return new ReflectionFunction($callable);
85-
}
75+
return match (\gettype($callable)) {
76+
'array' => new ReflectionMethod(...$callable),
77+
'object' => $callable instanceof Closure
78+
? new ReflectionFunction($callable)
79+
: (new ReflectionClass($callable))->getMethod('__invoke'),
80+
default => new ReflectionFunction($callable)
81+
};
8682
}
8783

8884
protected function getFromParameterType(
@@ -109,14 +105,14 @@ protected function getFromParameter(
109105
ReflectionParameter $parameter,
110106
mixed $value
111107
): mixed {
112-
$storage = $container->getStorage();
113108
try {
114109
$type = ($parameter->getType() ?? $parameter)->getName();
115110
} catch (\Error) {
116111
// i.e. for ReflectionUnionType, continue with processing
117112
return $value;
118113
}
119114

115+
$storage = $container->getStorage();
120116
if (isset($storage[DIContainer::BINDINGS][$type])) {
121117
$type = $storage[DIContainer::BINDINGS][$type];
122118
}
@@ -134,6 +130,7 @@ protected function getFromParameter(
134130
if (isset($storage[DIContainer::NAMED]['$' . $parameter->name])) {
135131
return $storage[DIContainer::NAMED]['$' . $parameter->name];
136132
}
133+
137134
try {
138135
return $value ?? $parameter?->getDefaultValue();
139136
} catch (\ReflectionException $e) {

0 commit comments

Comments
 (0)