Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[HttpKernel] Fix extracting controller name from closures #45462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[HttpKernel] Fix extracting controller name from closures
  • Loading branch information
nicolas-grekas committed Feb 17, 2022
commit 7b1ada07cd933d2455ef449ade3687a1d28d8114
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,19 @@ public function createArgumentMetadata($controller): array

if (\is_array($controller)) {
$reflection = new \ReflectionMethod($controller[0], $controller[1]);
$class = $reflection->class;
} elseif (\is_object($controller) && !$controller instanceof \Closure) {
$reflection = (new \ReflectionObject($controller))->getMethod('__invoke');
$reflection = new \ReflectionMethod($controller, '__invoke');
$class = $reflection->class;
} else {
$reflection = new \ReflectionFunction($controller);
if ($class = str_contains($reflection->name, '{closure}') ? null : $reflection->getClosureScopeClass()) {
$class = $class->name;
}
}

foreach ($reflection->getParameters() as $param) {
$arguments[] = new ArgumentMetadata($param->getName(), $this->getType($param, $reflection), $param->isVariadic(), $param->isDefaultValueAvailable(), $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null, $param->allowsNull());
$arguments[] = new ArgumentMetadata($param->getName(), $this->getType($param, $reflection, $class), $param->isVariadic(), $param->isDefaultValueAvailable(), $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null, $param->allowsNull());
}

return $arguments;
Expand All @@ -43,20 +48,19 @@ public function createArgumentMetadata($controller): array
/**
* Returns an associated type to the given parameter if available.
*/
private function getType(\ReflectionParameter $parameter, \ReflectionFunctionAbstract $function): ?string
private function getType(\ReflectionParameter $parameter, \ReflectionFunctionAbstract $function, ?string $class): ?string
{
if (!$type = $parameter->getType()) {
return null;
}
$name = $type instanceof \ReflectionNamedType ? $type->getName() : (string) $type;

if ($function instanceof \ReflectionMethod) {
$lcName = strtolower($name);
switch ($lcName) {
if (null !== $class) {
switch (strtolower($name)) {
case 'self':
return $function->getDeclaringClass()->name;
return $class;
case 'parent':
return ($parent = $function->getDeclaringClass()->getParentClass()) ? $parent->name : null;
return get_parent_class($class) ?: null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ public function testBasicTypesSignature()
], $arguments);
}

public function testNamedClosure()
{
$arguments = $this->factory->createArgumentMetadata(\Closure::fromCallable([$this, 'signature1']));

$this->assertEquals([
new ArgumentMetadata('foo', self::class, false, false, null),
new ArgumentMetadata('bar', 'array', false, false, null),
new ArgumentMetadata('baz', 'callable', false, false, null),
], $arguments);
}

public function testNullableTypesSignature()
{
$arguments = $this->factory->createArgumentMetadata([new NullableController(), 'action']);
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.