Make AppFramework/Http/Dispatcher strict

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2018-02-21 08:51:46 +01:00
parent 6591a3bc36
commit bb0c7b2943
No known key found for this signature in database
GPG Key ID: F941078878347C0C
1 changed files with 16 additions and 8 deletions

View File

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