diff --git a/lib/private/AppFramework/OCS/BaseResponse.php b/lib/private/AppFramework/OCS/BaseResponse.php index 533cff74a3007..123b73d302c6f 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..e6b0165278999 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,8 +26,16 @@ 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 diff --git a/lib/private/AppFramework/OCS/V2Response.php b/lib/private/AppFramework/OCS/V2Response.php index 8bf4c37a5787b..1e81a3c7d938c 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,8 +25,16 @@ 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. diff --git a/lib/public/AppFramework/Http/DataDisplayResponse.php b/lib/public/AppFramework/Http/DataDisplayResponse.php index 78ab343abd643..c4b781e53578f 100644 --- a/lib/public/AppFramework/Http/DataDisplayResponse.php +++ b/lib/public/AppFramework/Http/DataDisplayResponse.php @@ -6,6 +6,7 @@ * @author Julius Härtl * @author Morris Jobke * @author Roeland Jago Douma + * @author Kate Döen * * @license AGPL-3.0 * @@ -30,6 +31,9 @@ * Class DataDisplayResponse * * @since 8.1.0 + * @template S of int + * @template H of array + * @template-extends Response> */ class DataDisplayResponse extends Response { /** @@ -41,8 +45,8 @@ class DataDisplayResponse extends Response { /** * @param string $data the data to display - * @param int $statusCode the Http status code, defaults to 200 - * @param array $headers additional key value based headers + * @param S $statusCode the Http status code, defaults to 200 + * @param H $headers additional key value based headers * @since 8.1.0 */ public function __construct($data = '', $statusCode = Http::STATUS_OK, diff --git a/lib/public/AppFramework/Http/DataDownloadResponse.php b/lib/public/AppFramework/Http/DataDownloadResponse.php index 7f2bc73f6e2e8..d578985bf6b59 100644 --- a/lib/public/AppFramework/Http/DataDownloadResponse.php +++ b/lib/public/AppFramework/Http/DataDownloadResponse.php @@ -5,6 +5,7 @@ * @author Georg Ehrke * @author Morris Jobke * @author Roeland Jago Douma + * @author Kate Döen * * @license AGPL-3.0 * @@ -27,6 +28,10 @@ * Class DataDownloadResponse * * @since 8.0.0 + * @template S of int + * @template C of string + * @template H of array + * @template-extends DownloadResponse> */ class DataDownloadResponse extends DownloadResponse { /** @@ -38,7 +43,7 @@ class DataDownloadResponse extends DownloadResponse { * Creates a response that prompts the user to download the text * @param string $data text to be downloaded * @param string $filename the name that the downloaded file should have - * @param string $contentType the mimetype that the downloaded file should have + * @param C $contentType the mimetype that the downloaded file should have * @since 8.0.0 */ public function __construct($data, $filename, $contentType) { diff --git a/lib/public/AppFramework/Http/DataResponse.php b/lib/public/AppFramework/Http/DataResponse.php index e329b9c297564..c8d78255b0110 100644 --- a/lib/public/AppFramework/Http/DataResponse.php +++ b/lib/public/AppFramework/Http/DataResponse.php @@ -6,6 +6,7 @@ * @author Christoph Wurst * @author Morris Jobke * @author Roeland Jago Douma + * @author Kate Döen * * @license AGPL-3.0 * @@ -30,19 +31,24 @@ * A generic DataResponse class that is used to return generic data responses * for responders to transform * @since 8.0.0 + * @psalm-type DataResponseType = array|int|float|string|bool|object|null|\stdClass|\JsonSerializable + * @template S of int + * @template-covariant T of DataResponseType + * @template H of array + * @template-extends Response> */ class DataResponse extends Response { /** * response data - * @var array|int|float|string|bool|object + * @var T */ protected $data; /** - * @param array|int|float|string|bool|object $data the object or array that should be transformed - * @param int $statusCode the Http status code, defaults to 200 - * @param array $headers additional key value based headers + * @param T $data the object or array that should be transformed + * @param S $statusCode the Http status code, defaults to 200 + * @param H $headers additional key value based headers * @since 8.0.0 */ public function __construct($data = [], $statusCode = Http::STATUS_OK, @@ -57,7 +63,8 @@ public function __construct($data = [], $statusCode = Http::STATUS_OK, /** * Sets values in the data json array - * @param array|int|float|string|object $data an array or object which will be transformed + * @psalm-suppress InvalidTemplateParam + * @param T $data an array or object which will be transformed * @return DataResponse Reference to this object * @since 8.0.0 */ @@ -70,7 +77,7 @@ public function setData($data) { /** * Used to get the set parameters - * @return array|int|float|string|bool|object the data + * @return T the data * @since 8.0.0 */ public function getData() { diff --git a/lib/public/AppFramework/Http/DownloadResponse.php b/lib/public/AppFramework/Http/DownloadResponse.php index b80f03958c0e8..2e54539412cc7 100644 --- a/lib/public/AppFramework/Http/DownloadResponse.php +++ b/lib/public/AppFramework/Http/DownloadResponse.php @@ -7,6 +7,7 @@ * @author Morris Jobke * @author Roeland Jago Douma * @author Thomas Müller + * @author Kate Döen * * @license AGPL-3.0 * @@ -28,12 +29,16 @@ /** * Prompts the user to download the a file * @since 7.0.0 + * @template S of int + * @template C of string + * @template H of array + * @template-extends Response> */ class DownloadResponse extends Response { /** * Creates a response that prompts the user to download the file * @param string $filename the name that the downloaded file should have - * @param string $contentType the mimetype that the downloaded file should have + * @param C $contentType the mimetype that the downloaded file should have * @since 7.0.0 */ public function __construct(string $filename, string $contentType) { diff --git a/lib/public/AppFramework/Http/FileDisplayResponse.php b/lib/public/AppFramework/Http/FileDisplayResponse.php index 41b452b555328..141376733b5c1 100644 --- a/lib/public/AppFramework/Http/FileDisplayResponse.php +++ b/lib/public/AppFramework/Http/FileDisplayResponse.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 * @@ -29,6 +30,9 @@ * Class FileDisplayResponse * * @since 11.0.0 + * @template S of int + * @template H of array + * @template-extends Response> */ class FileDisplayResponse extends Response implements ICallbackResponse { /** @var \OCP\Files\File|\OCP\Files\SimpleFS\ISimpleFile */ @@ -38,8 +42,8 @@ class FileDisplayResponse extends Response implements ICallbackResponse { * FileDisplayResponse constructor. * * @param \OCP\Files\File|\OCP\Files\SimpleFS\ISimpleFile $file - * @param int $statusCode - * @param array $headers + * @param S $statusCode + * @param H $headers * @since 11.0.0 */ public function __construct($file, $statusCode = Http::STATUS_OK, diff --git a/lib/public/AppFramework/Http/JSONResponse.php b/lib/public/AppFramework/Http/JSONResponse.php index d31a276167373..19389f97d3b51 100644 --- a/lib/public/AppFramework/Http/JSONResponse.php +++ b/lib/public/AppFramework/Http/JSONResponse.php @@ -9,6 +9,7 @@ * @author Roeland Jago Douma * @author Thomas Müller * @author Thomas Tanghus + * @author Kate Döen * * @license AGPL-3.0 * @@ -32,19 +33,23 @@ /** * A renderer for JSON calls * @since 6.0.0 + * @template S of int + * @template-covariant T of array|object|\stdClass|\JsonSerializable + * @template H of array + * @template-extends Response> */ class JSONResponse extends Response { /** * response data - * @var array|object + * @var T */ protected $data; /** * constructor of JSONResponse - * @param array|object $data the object or array that should be transformed - * @param int $statusCode the Http status code, defaults to 200 + * @param T $data the object or array that should be transformed + * @param S $statusCode the Http status code, defaults to 200 * @since 6.0.0 */ public function __construct($data = [], $statusCode = Http::STATUS_OK) { @@ -68,7 +73,8 @@ public function render() { /** * Sets values in the data json array - * @param array|object $data an array or object which will be transformed + * @psalm-suppress InvalidTemplateParam + * @param T $data an array or object which will be transformed * to JSON * @return JSONResponse Reference to this object * @since 6.0.0 - return value was added in 7.0.0 @@ -81,8 +87,7 @@ public function setData($data) { /** - * Used to get the set parameters - * @return array the data + * @return T the data * @since 6.0.0 */ public function getData() { diff --git a/lib/public/AppFramework/Http/NotFoundResponse.php b/lib/public/AppFramework/Http/NotFoundResponse.php index 34b74d353db8d..6a391c35dbb87 100644 --- a/lib/public/AppFramework/Http/NotFoundResponse.php +++ b/lib/public/AppFramework/Http/NotFoundResponse.php @@ -6,6 +6,7 @@ * @author Lukas Reschke * @author Morris Jobke * @author Roeland Jago Douma + * @author Kate Döen * * @license AGPL-3.0 * @@ -24,9 +25,13 @@ */ namespace OCP\AppFramework\Http; +use OCP\AppFramework\Http; + /** * A generic 404 response showing an 404 error page as well to the end-user * @since 8.1.0 + * @template H of array + * @template-extends TemplateResponse> */ class NotFoundResponse extends TemplateResponse { /** @@ -36,6 +41,6 @@ public function __construct() { parent::__construct('core', '404', [], 'guest'); $this->setContentSecurityPolicy(new ContentSecurityPolicy()); - $this->setStatus(404); + $this->setStatus(Http::STATUS_NOT_FOUND); } } diff --git a/lib/public/AppFramework/Http/RedirectResponse.php b/lib/public/AppFramework/Http/RedirectResponse.php index 87853391e863f..58da1b85d5900 100644 --- a/lib/public/AppFramework/Http/RedirectResponse.php +++ b/lib/public/AppFramework/Http/RedirectResponse.php @@ -7,6 +7,7 @@ * @author Roeland Jago Douma * @author Thomas Müller * @author v1r0x + * @author Kate Döen * * @license AGPL-3.0 * @@ -30,6 +31,8 @@ /** * Redirects to a different URL * @since 7.0.0 + * @template H of array + * @template-extends Response> */ class RedirectResponse extends Response { private $redirectURL; diff --git a/lib/public/AppFramework/Http/RedirectToDefaultAppResponse.php b/lib/public/AppFramework/Http/RedirectToDefaultAppResponse.php index ad11b53637b51..9b81ffa6a4fca 100644 --- a/lib/public/AppFramework/Http/RedirectToDefaultAppResponse.php +++ b/lib/public/AppFramework/Http/RedirectToDefaultAppResponse.php @@ -7,6 +7,7 @@ * * @author Joas Schilling * @author Roeland Jago Douma + * @author Kate Döen * * @license GNU AGPL version 3 or any later version * @@ -33,6 +34,8 @@ * * @since 16.0.0 * @deprecated 23.0.0 Use RedirectResponse() with IURLGenerator::linkToDefaultPageUrl() instead + * @template H of array + * @template-extends RedirectResponse> */ class RedirectToDefaultAppResponse extends RedirectResponse { /** diff --git a/lib/public/AppFramework/Http/Response.php b/lib/public/AppFramework/Http/Response.php index 152f8c4a3c5ea..714c54425455a 100644 --- a/lib/public/AppFramework/Http/Response.php +++ b/lib/public/AppFramework/Http/Response.php @@ -12,6 +12,7 @@ * @author Roeland Jago Douma * @author Thomas Müller * @author Thomas Tanghus + * @author Kate Döen * * @license AGPL-3.0 * @@ -41,6 +42,8 @@ * * It handles headers, HTTP status code, last modified and ETag. * @since 6.0.0 + * @template S of int + * @template H of array */ class Response { /** @@ -61,9 +64,9 @@ class Response { /** * HTTP status code - defaults to STATUS OK - * @var int + * @var S */ - private $status = Http::STATUS_OK; + private $status; /** @@ -100,6 +103,7 @@ public function __construct() { */ $request = \OC::$server->get(IRequest::class); $this->addHeader("X-Request-Id", $request->getId()); + $this->setStatus(Http::STATUS_OK); } /** @@ -231,7 +235,9 @@ public function addHeader($name, $value) { /** * Set the headers - * @param array $headers value header pairs + * @template NewH as array + * @param NewH $headers value header pairs + * @psalm-this-out self * @return $this * @since 8.0.0 */ @@ -244,7 +250,7 @@ public function setHeaders(array $headers) { /** * Returns the set headers - * @return array the headers + * @return array{Last-Modified?: string, Content-Security-Policy: string, Feature-Policy: string, X-Robots-Tag: string, ETag?: string, ...H} the headers * @since 6.0.0 */ public function getHeaders() { @@ -279,11 +285,14 @@ public function render() { /** * Set response status - * @param int $status a HTTP status code, see also the STATUS constants + * @template NewS as int + * @param NewS $status a HTTP status code, see also the STATUS constants + * @psalm-this-out self * @return Response Reference to this object * @since 6.0.0 - return value was added in 7.0.0 */ public function setStatus($status) { + /** @psalm-suppress InvalidPropertyAssignmentValue Expected due to @psalm-this-out */ $this->status = $status; return $this; @@ -338,6 +347,7 @@ public function setFeaturePolicy(EmptyFeaturePolicy $featurePolicy): self { /** * Get response status * @since 6.0.0 + * @return S */ public function getStatus() { return $this->status; diff --git a/lib/public/AppFramework/Http/StandaloneTemplateResponse.php b/lib/public/AppFramework/Http/StandaloneTemplateResponse.php index 35a48481333be..8a39dca71e3f0 100644 --- a/lib/public/AppFramework/Http/StandaloneTemplateResponse.php +++ b/lib/public/AppFramework/Http/StandaloneTemplateResponse.php @@ -6,6 +6,7 @@ * @copyright Copyright (c) 2019, Roeland Jago Douma * * @author Roeland Jago Douma + * @author Kate Döen * * @license GNU AGPL version 3 or any later version * @@ -32,6 +33,9 @@ * full nextcloud UI. Like the 2FA page, or the grant page in the login flow. * * @since 16.0.0 + * @template S of int + * @template H of array + * @template-extends TemplateResponse> */ class StandaloneTemplateResponse extends TemplateResponse { } diff --git a/lib/public/AppFramework/Http/StreamResponse.php b/lib/public/AppFramework/Http/StreamResponse.php index 25ad37e5d9a22..3aa60a4157e1f 100644 --- a/lib/public/AppFramework/Http/StreamResponse.php +++ b/lib/public/AppFramework/Http/StreamResponse.php @@ -8,6 +8,7 @@ * @author Morris Jobke * @author Robin Appelman * @author Roeland Jago Douma + * @author Kate Döen * * @license AGPL-3.0 * @@ -32,6 +33,9 @@ * Class StreamResponse * * @since 8.1.0 + * @template S of int + * @template H of array + * @template-extends Response> */ class StreamResponse extends Response implements ICallbackResponse { /** @var string */ diff --git a/lib/public/AppFramework/Http/Template/PublicTemplateResponse.php b/lib/public/AppFramework/Http/Template/PublicTemplateResponse.php index 1196c90935dd0..34940cc7f7445 100644 --- a/lib/public/AppFramework/Http/Template/PublicTemplateResponse.php +++ b/lib/public/AppFramework/Http/Template/PublicTemplateResponse.php @@ -5,6 +5,7 @@ * @author Christoph Wurst * @author Julius Härtl * @author Roeland Jago Douma + * @author Kate Döen * * @license GNU AGPL version 3 or any later version * @@ -31,6 +32,9 @@ * Class PublicTemplateResponse * * @since 14.0.0 + * @template H of array + * @template S of int + * @template-extends TemplateResponse> */ class PublicTemplateResponse extends TemplateResponse { private $headerTitle = ''; diff --git a/lib/public/AppFramework/Http/TemplateResponse.php b/lib/public/AppFramework/Http/TemplateResponse.php index 48097674fdf20..b1db08b140c5e 100644 --- a/lib/public/AppFramework/Http/TemplateResponse.php +++ b/lib/public/AppFramework/Http/TemplateResponse.php @@ -10,6 +10,7 @@ * @author Roeland Jago Douma * @author Thomas Müller * @author Thomas Tanghus + * @author Kate Döen * * @license AGPL-3.0 * @@ -31,6 +32,10 @@ /** * Response for a normal template * @since 6.0.0 + * + * @template S of int + * @template H of array + * @template-extends Response> */ class TemplateResponse extends Response { /** diff --git a/lib/public/AppFramework/Http/TextPlainResponse.php b/lib/public/AppFramework/Http/TextPlainResponse.php index 93edf70486301..81975a60b3d69 100644 --- a/lib/public/AppFramework/Http/TextPlainResponse.php +++ b/lib/public/AppFramework/Http/TextPlainResponse.php @@ -6,6 +6,7 @@ * @copyright 2021 Lukas Reschke * * @author 2021 Lukas Reschke + * @author Kate Döen * * @license GNU AGPL version 3 or any later version * @@ -30,6 +31,9 @@ /** * A renderer for text responses * @since 22.0.0 + * @template S of int + * @template H of array + * @template-extends Response> */ class TextPlainResponse extends Response { /** @var string */ @@ -38,7 +42,7 @@ class TextPlainResponse extends Response { /** * constructor of TextPlainResponse * @param string $text The text body - * @param int $statusCode the Http status code, defaults to 200 + * @param S $statusCode the Http status code, defaults to 200 * @since 22.0.0 */ public function __construct(string $text = '', int $statusCode = Http::STATUS_OK) { diff --git a/lib/public/AppFramework/Http/TooManyRequestsResponse.php b/lib/public/AppFramework/Http/TooManyRequestsResponse.php index caf565ee954eb..7f0b907034d38 100644 --- a/lib/public/AppFramework/Http/TooManyRequestsResponse.php +++ b/lib/public/AppFramework/Http/TooManyRequestsResponse.php @@ -6,6 +6,7 @@ * @copyright Copyright (c) 2020 Joas Schilling * * @author Joas Schilling + * @author Kate Döen * * @license GNU AGPL version 3 or any later version * @@ -26,10 +27,13 @@ namespace OCP\AppFramework\Http; use OCP\Template; +use OCP\AppFramework\Http; /** * A generic 429 response showing an 404 error page as well to the end-user * @since 19.0.0 + * @template H of array + * @template-extends Response> */ class TooManyRequestsResponse extends Response { /** @@ -39,7 +43,7 @@ public function __construct() { parent::__construct(); $this->setContentSecurityPolicy(new ContentSecurityPolicy()); - $this->setStatus(429); + $this->setStatus(Http::STATUS_TOO_MANY_REQUESTS); } /** diff --git a/lib/public/AppFramework/Http/ZipResponse.php b/lib/public/AppFramework/Http/ZipResponse.php index 23e9f1f7a9455..daf414df71b5d 100644 --- a/lib/public/AppFramework/Http/ZipResponse.php +++ b/lib/public/AppFramework/Http/ZipResponse.php @@ -9,6 +9,7 @@ * @author Jakob Sack * @author Morris Jobke * @author Roeland Jago Douma + * @author Kate Döen * * @license GNU AGPL version 3 or any later version * @@ -35,6 +36,9 @@ * Public library to send several files in one zip archive. * * @since 15.0.0 + * @template S of int + * @template H of array + * @template-extends Response> */ class ZipResponse extends Response implements ICallbackResponse { /** @var array{internalName: string, resource: resource, size: int, time: int}[] Files to be added to the zip response */