Skip to content

Commit

Permalink
Merge pull request cakephp#17338 from cakephp/controllerfactory-unsup…
Browse files Browse the repository at this point in the history
…ported-reflectiontype

Don't throw an exception for unsupoorted types.
  • Loading branch information
othercorey authored Oct 11, 2023
2 parents 1c26321 + 5579533 commit 8c1c9a1
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 17 deletions.
11 changes: 0 additions & 11 deletions src/Controller/ControllerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,6 @@ protected function getActionArgs(Closure $action, array $passedParams): array
$function = new ReflectionFunction($action);
foreach ($function->getParameters() as $parameter) {
$type = $parameter->getType();
if ($type && !$type instanceof ReflectionNamedType) {
// Only single types are supported
throw new InvalidParameterException([
'template' => 'unsupported_type',
'parameter' => $parameter->getName(),
'controller' => $this->controller->getName(),
'action' => $this->controller->getRequest()->getParam('action'),
'prefix' => $this->controller->getRequest()->getParam('prefix'),
'plugin' => $this->controller->getRequest()->getParam('plugin'),
]);
}

// Check for dependency injection for classes
if ($type instanceof ReflectionNamedType && !$type->isBuiltin()) {
Expand Down
11 changes: 6 additions & 5 deletions tests/TestCase/Controller/ControllerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -887,15 +887,16 @@ public function testInvokePassedParamUnsupportedReflectionType(): void
'params' => [
'plugin' => null,
'controller' => 'Dependencies',
'action' => 'unsupportedTypedUnion',
'pass' => ['test'],
'action' => 'typedUnion',
'pass' => ['1'],
],
]);
$controller = $this->factory->create($request);

$this->expectException(InvalidParameterException::class);
$this->expectExceptionMessage('Type declaration for `one` in action `Dependencies::unsupportedTypedUnion()` is unsupported.');
$this->factory->invoke($controller);
$result = $this->factory->invoke($controller);
$data = json_decode((string)$result->getBody(), true);

$this->assertSame(['one' => '1'], $data);
}

public function testMiddleware(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function unsupportedTyped(iterable $one)
return $this->response->withStringBody(json_encode(compact('one')));
}

public function unsupportedTypedUnion(string|int $one)
public function typedUnion(string|int $one)
{
return $this->response->withStringBody(json_encode(compact('one')));
}
Expand Down

0 comments on commit 8c1c9a1

Please sign in to comment.