Skip to content

Commit bf0d406

Browse files
Merge branch '4.4' into 5.4
* 4.4: [Stopwatch] Fix test expectation [SecurityBundle] fix autoconfiguring Monolog's ProcessorInterface KernelTestCase resets internal state on tearDown [HttpKernel] Fix extracting controller name from closures [Intl] fix wrong offset timezone PHP 8.1 Fix type binding Remove duplicated test Make document type nodes ignorable Initialize Symfony\Component\Security\Core\Exception\AccountStatusException:: property
2 parents 8a2ad2a + af1ed74 commit bf0d406

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

ControllerMetadata/ArgumentMetadataFactory.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ public function createArgumentMetadata($controller): array
2727

2828
if (\is_array($controller)) {
2929
$reflection = new \ReflectionMethod($controller[0], $controller[1]);
30+
$class = $reflection->class;
3031
} elseif (\is_object($controller) && !$controller instanceof \Closure) {
31-
$reflection = (new \ReflectionObject($controller))->getMethod('__invoke');
32+
$reflection = new \ReflectionMethod($controller, '__invoke');
33+
$class = $reflection->class;
3234
} else {
3335
$reflection = new \ReflectionFunction($controller);
36+
if ($class = str_contains($reflection->name, '{closure}') ? null : $reflection->getClosureScopeClass()) {
37+
$class = $class->name;
38+
}
3439
}
3540

3641
foreach ($reflection->getParameters() as $param) {
@@ -43,7 +48,7 @@ public function createArgumentMetadata($controller): array
4348
}
4449
}
4550

46-
$arguments[] = new ArgumentMetadata($param->getName(), $this->getType($param, $reflection), $param->isVariadic(), $param->isDefaultValueAvailable(), $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null, $param->allowsNull(), $attributes);
51+
$arguments[] = new ArgumentMetadata($param->getName(), $this->getType($param, $reflection, $class), $param->isVariadic(), $param->isDefaultValueAvailable(), $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null, $param->allowsNull(), $attributes);
4752
}
4853

4954
return $arguments;
@@ -52,20 +57,19 @@ public function createArgumentMetadata($controller): array
5257
/**
5358
* Returns an associated type to the given parameter if available.
5459
*/
55-
private function getType(\ReflectionParameter $parameter, \ReflectionFunctionAbstract $function): ?string
60+
private function getType(\ReflectionParameter $parameter, \ReflectionFunctionAbstract $function, ?string $class): ?string
5661
{
5762
if (!$type = $parameter->getType()) {
5863
return null;
5964
}
6065
$name = $type instanceof \ReflectionNamedType ? $type->getName() : (string) $type;
6166

62-
if ($function instanceof \ReflectionMethod) {
63-
$lcName = strtolower($name);
64-
switch ($lcName) {
67+
if (null !== $class) {
68+
switch (strtolower($name)) {
6569
case 'self':
66-
return $function->getDeclaringClass()->name;
70+
return $class;
6771
case 'parent':
68-
return ($parent = $function->getDeclaringClass()->getParentClass()) ? $parent->name : null;
72+
return get_parent_class($class) ?: null;
6973
}
7074
}
7175

DependencyInjection/RegisterControllerArgumentLocatorsPass.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,12 @@ public function process(ContainerBuilder $container)
164164
$args[$p->name] = $bindingValue;
165165
}
166166

167+
continue;
168+
} elseif (!$type || !$autowire || '\\' !== $target[0]) {
167169
continue;
168170
} elseif (is_subclass_of($type, \UnitEnum::class)) {
169171
// do not attempt to register enum typed arguments if not already present in bindings
170172
continue;
171-
} elseif (!$type || !$autowire || '\\' !== $target[0]) {
172-
continue;
173173
} elseif (!$p->allowsNull()) {
174174
$invalidBehavior = ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE;
175175
}

Tests/ControllerMetadata/ArgumentMetadataFactoryTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,17 @@ public function testBasicTypesSignature()
107107
], $arguments);
108108
}
109109

110+
public function testNamedClosure()
111+
{
112+
$arguments = $this->factory->createArgumentMetadata(\Closure::fromCallable([$this, 'signature1']));
113+
114+
$this->assertEquals([
115+
new ArgumentMetadata('foo', self::class, false, false, null),
116+
new ArgumentMetadata('bar', 'array', false, false, null),
117+
new ArgumentMetadata('baz', 'callable', false, false, null),
118+
], $arguments);
119+
}
120+
110121
public function testNullableTypesSignature()
111122
{
112123
$arguments = $this->factory->createArgumentMetadata([new NullableController(), 'action']);

0 commit comments

Comments
 (0)