From c5d818d28a504614e831ee1c80385ff499a09646 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Wed, 14 Jun 2023 09:02:56 +0200 Subject: [PATCH] Fix response types Signed-off-by: jld3103 --- apps/settings/lib/WellKnown/ChangePasswordHandler.php | 2 ++ .../AppFramework/Middleware/CompressionMiddleware.php | 4 ++++ lib/private/AppFramework/Middleware/OCSMiddleware.php | 7 ++++--- lib/private/AppFramework/OCS/BaseResponse.php | 10 +++++++++- lib/private/AppFramework/OCS/V1Response.php | 10 ++++++++++ lib/private/AppFramework/OCS/V2Response.php | 10 ++++++++++ 6 files changed, 39 insertions(+), 4 deletions(-) diff --git a/apps/settings/lib/WellKnown/ChangePasswordHandler.php b/apps/settings/lib/WellKnown/ChangePasswordHandler.php index 11f733474e311..b96e7d0e40f8c 100644 --- a/apps/settings/lib/WellKnown/ChangePasswordHandler.php +++ b/apps/settings/lib/WellKnown/ChangePasswordHandler.php @@ -26,6 +26,7 @@ namespace OCA\Settings\WellKnown; use OCP\AppFramework\Http\RedirectResponse; +use OCP\AppFramework\Http\Response; use OCP\Http\WellKnown\GenericResponse; use OCP\Http\WellKnown\IHandler; use OCP\Http\WellKnown\IRequestContext; @@ -45,6 +46,7 @@ public function handle(string $service, IRequestContext $context, ?IResponse $pr return $previousResponse; } + /** @var Response $response */ $response = new RedirectResponse($this->urlGenerator->linkToRouteAbsolute('settings.PersonalSettings.index', ['section' => 'security'])); return new GenericResponse($response); } diff --git a/lib/private/AppFramework/Middleware/CompressionMiddleware.php b/lib/private/AppFramework/Middleware/CompressionMiddleware.php index b1b89832b0094..babc4036caab8 100644 --- a/lib/private/AppFramework/Middleware/CompressionMiddleware.php +++ b/lib/private/AppFramework/Middleware/CompressionMiddleware.php @@ -7,6 +7,7 @@ * * @author Morris Jobke * @author Roeland Jago Douma + * @author Kate Döen * * @license GNU AGPL version 3 or any later version * @@ -46,6 +47,9 @@ public function __construct(IRequest $request) { $this->useGZip = false; } + /** + * @param Response> $response + */ public function afterController($controller, $methodName, Response $response) { // By default we do not gzip $allowGzip = false; diff --git a/lib/private/AppFramework/Middleware/OCSMiddleware.php b/lib/private/AppFramework/Middleware/OCSMiddleware.php index 955b15f80c81a..d8b794bf95b6b 100644 --- a/lib/private/AppFramework/Middleware/OCSMiddleware.php +++ b/lib/private/AppFramework/Middleware/OCSMiddleware.php @@ -6,6 +6,7 @@ * @author Joas Schilling * @author Lukas Reschke * @author Roeland Jago Douma + * @author Kate Döen * * @license GNU AGPL version 3 or any later version * @@ -90,7 +91,7 @@ public function afterException($controller, $methodName, \Exception $exception) /** * @param Controller $controller * @param string $methodName - * @param Response $response + * @param Response|JSONResponse $response * @return \OCP\AppFramework\Http\Response */ public function afterController($controller, $methodName, Response $response) { @@ -102,7 +103,7 @@ public function afterController($controller, $methodName, Response $response) { if ($response->getStatus() === Http::STATUS_UNAUTHORIZED) { $message = ''; if ($response instanceof JSONResponse) { - /** @var DataResponse $response */ + /** @var DataResponse $response */ $message = $response->getData()['message']; } @@ -111,7 +112,7 @@ public function afterController($controller, $methodName, Response $response) { if ($response->getStatus() === Http::STATUS_FORBIDDEN) { $message = ''; if ($response instanceof JSONResponse) { - /** @var DataResponse $response */ + /** @var DataResponse $response */ $message = $response->getData()['message']; } diff --git a/lib/private/AppFramework/OCS/BaseResponse.php b/lib/private/AppFramework/OCS/BaseResponse.php index 533cff74a3007..fe11222c5067b 100644 --- a/lib/private/AppFramework/OCS/BaseResponse.php +++ b/lib/private/AppFramework/OCS/BaseResponse.php @@ -7,6 +7,7 @@ * @author Joas Schilling * @author Lukas Reschke * @author Roeland Jago Douma + * @author Kate Döen * * @license GNU AGPL version 3 or any later version * @@ -30,6 +31,13 @@ use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\Response; +/** + * @psalm-import-type DataResponseType from DataResponse + * @template S of int + * @template-covariant T of DataResponseType + * @template H of array + * @template-extends Response + */ abstract class BaseResponse extends Response { /** @var array */ protected $data; @@ -49,7 +57,7 @@ abstract class BaseResponse extends Response { /** * BaseResponse constructor. * - * @param DataResponse $dataResponse + * @param DataResponse $dataResponse * @param string $format * @param string|null $statusMessage * @param int|null $itemsCount diff --git a/lib/private/AppFramework/OCS/V1Response.php b/lib/private/AppFramework/OCS/V1Response.php index cca3c267ec483..d1fb48d891e90 100644 --- a/lib/private/AppFramework/OCS/V1Response.php +++ b/lib/private/AppFramework/OCS/V1Response.php @@ -5,6 +5,7 @@ * @author Christoph Wurst * @author Joas Schilling * @author Roeland Jago Douma + * @author Kate Döen * * @license GNU AGPL version 3 or any later version * @@ -25,13 +26,22 @@ namespace OC\AppFramework\OCS; use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\OCSController; +/** + * @psalm-import-type DataResponseType from DataResponse + * @template S of int + * @template-covariant T of DataResponseType + * @template H of array + * @template-extends BaseResponse + */ class V1Response extends BaseResponse { /** * The V1 endpoint has very limited http status codes basically everything * is status 200 except 401 * + * @psalm-suppress ImplementedReturnTypeMismatch Expected because we match some status codes * @return int */ public function getStatus() { diff --git a/lib/private/AppFramework/OCS/V2Response.php b/lib/private/AppFramework/OCS/V2Response.php index 8bf4c37a5787b..19fa64bf1c3e2 100644 --- a/lib/private/AppFramework/OCS/V2Response.php +++ b/lib/private/AppFramework/OCS/V2Response.php @@ -4,6 +4,7 @@ * * @author Christoph Wurst * @author Roeland Jago Douma + * @author Kate Döen * * @license GNU AGPL version 3 or any later version * @@ -24,13 +25,22 @@ namespace OC\AppFramework\OCS; use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\OCSController; +/** + * @psalm-import-type DataResponseType from DataResponse + * @template S of int + * @template-covariant T of DataResponseType + * @template H of array + * @template-extends BaseResponse + */ class V2Response extends BaseResponse { /** * The V2 endpoint just passes on status codes. * Of course we have to map the OCS specific codes to proper HTTP status codes * + * @psalm-suppress ImplementedReturnTypeMismatch Expected because we match some status codes * @return int */ public function getStatus() {