Merge pull request #8465 from nextcloud/strict_dispatcher

Make AppFramework/Http/Dispatcher strict
This commit is contained in:
Morris Jobke 2018-02-21 10:48:06 +01:00 committed by GitHub
commit 09857e5a95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 8 deletions

View File

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
/** /**
* @copyright Copyright (c) 2016, ownCloud, Inc. * @copyright Copyright (c) 2016, ownCloud, Inc.
* *
@ -44,9 +45,16 @@ use OCP\IRequest;
*/ */
class Dispatcher { class Dispatcher {
/** @var MiddlewareDispatcher */
private $middlewareDispatcher; private $middlewareDispatcher;
/** @var Http */
private $protocol; private $protocol;
/** @var ControllerMethodReflector */
private $reflector; private $reflector;
/** @var IRequest */
private $request; private $request;
/** /**
@ -78,8 +86,8 @@ class Dispatcher {
* the response output * the response output
* @throws \Exception * @throws \Exception
*/ */
public function dispatch(Controller $controller, $methodName) { public function dispatch(Controller $controller, string $methodName): array {
$out = array(null, array(), null); $out = [null, [], null];
try { try {
// prefill reflector with everything thats needed for the // prefill reflector with everything thats needed for the
@ -97,7 +105,7 @@ class Dispatcher {
} catch(\Exception $exception){ } catch(\Exception $exception){
$response = $this->middlewareDispatcher->afterException( $response = $this->middlewareDispatcher->afterException(
$controller, $methodName, $exception); $controller, $methodName, $exception);
if (is_null($response)) { if ($response === null) {
throw $exception; throw $exception;
} }
} }
@ -126,11 +134,11 @@ class Dispatcher {
* @param string $methodName the method on the controller that should be executed * @param string $methodName the method on the controller that should be executed
* @return Response * @return Response
*/ */
private function executeController($controller, $methodName) { private function executeController(Controller $controller, string $methodName): Response {
$arguments = array(); $arguments = [];
// valid types that will be casted // valid types that will be casted
$types = array('int', 'integer', 'bool', 'boolean', 'float'); $types = ['int', 'integer', 'bool', 'boolean', 'float'];
foreach($this->reflector->getParameters() as $param => $default) { foreach($this->reflector->getParameters() as $param => $default) {
@ -151,14 +159,14 @@ class Dispatcher {
) { ) {
$value = false; $value = false;
} elseif($value !== null && in_array($type, $types)) { } elseif($value !== null && \in_array($type, $types, true)) {
settype($value, $type); settype($value, $type);
} }
$arguments[] = $value; $arguments[] = $value;
} }
$response = call_user_func_array(array($controller, $methodName), $arguments); $response = \call_user_func_array([$controller, $methodName], $arguments);
// format response // format response
if($response instanceof DataResponse || !($response instanceof Response)) { if($response instanceof DataResponse || !($response instanceof Response)) {