Skip to content

Commit

Permalink
Add template types to responses
Browse files Browse the repository at this point in the history
Signed-off-by: jld3103 <[email protected]>
  • Loading branch information
provokateurin committed Jun 23, 2023
1 parent 05f360c commit 6077be9
Show file tree
Hide file tree
Showing 20 changed files with 133 additions and 27 deletions.
10 changes: 9 additions & 1 deletion lib/private/AppFramework/OCS/BaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @author Joas Schilling <[email protected]>
* @author Lukas Reschke <[email protected]>
* @author Roeland Jago Douma <[email protected]>
* @author Kate Döen <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -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<string, mixed>
* @template-extends Response<int, array<string, mixed>>
*/
abstract class BaseResponse extends Response {
/** @var array */
protected $data;
Expand All @@ -49,7 +57,7 @@ abstract class BaseResponse extends Response {
/**
* BaseResponse constructor.
*
* @param DataResponse $dataResponse
* @param DataResponse<S, T, H> $dataResponse
* @param string $format
* @param string|null $statusMessage
* @param int|null $itemsCount
Expand Down
9 changes: 9 additions & 0 deletions lib/private/AppFramework/OCS/V1Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @author Christoph Wurst <[email protected]>
* @author Joas Schilling <[email protected]>
* @author Roeland Jago Douma <[email protected]>
* @author Kate Döen <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -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<string, mixed>
* @template-extends BaseResponse<int, DataResponseType, array<string, mixed>>
*/
class V1Response extends BaseResponse {
/**
* The V1 endpoint has very limited http status codes basically everything
Expand Down
9 changes: 9 additions & 0 deletions lib/private/AppFramework/OCS/V2Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*
* @author Christoph Wurst <[email protected]>
* @author Roeland Jago Douma <[email protected]>
* @author Kate Döen <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -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<string, mixed>
* @template-extends BaseResponse<int, DataResponseType, array<string, mixed>>
*/
class V2Response extends BaseResponse {
/**
* The V2 endpoint just passes on status codes.
Expand Down
8 changes: 6 additions & 2 deletions lib/public/AppFramework/Http/DataDisplayResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @author Julius Härtl <[email protected]>
* @author Morris Jobke <[email protected]>
* @author Roeland Jago Douma <[email protected]>
* @author Kate Döen <[email protected]>
*
* @license AGPL-3.0
*
Expand All @@ -30,6 +31,9 @@
* Class DataDisplayResponse
*
* @since 8.1.0
* @template S of int
* @template H of array<string, mixed>
* @template-extends Response<int, array<string, mixed>>
*/
class DataDisplayResponse extends Response {
/**
Expand All @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion lib/public/AppFramework/Http/DataDownloadResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @author Georg Ehrke <[email protected]>
* @author Morris Jobke <[email protected]>
* @author Roeland Jago Douma <[email protected]>
* @author Kate Döen <[email protected]>
*
* @license AGPL-3.0
*
Expand All @@ -27,6 +28,10 @@
* Class DataDownloadResponse
*
* @since 8.0.0
* @template S of int
* @template C of string
* @template H of array<string, mixed>
* @template-extends DownloadResponse<int, string, array<string, mixed>>
*/
class DataDownloadResponse extends DownloadResponse {
/**
Expand All @@ -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) {
Expand Down
19 changes: 13 additions & 6 deletions lib/public/AppFramework/Http/DataResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @author Christoph Wurst <[email protected]>
* @author Morris Jobke <[email protected]>
* @author Roeland Jago Douma <[email protected]>
* @author Kate Döen <[email protected]>
*
* @license AGPL-3.0
*
Expand All @@ -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<string, mixed>
* @template-extends Response<int, array<string, mixed>>
*/
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,
Expand All @@ -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
*/
Expand All @@ -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() {
Expand Down
7 changes: 6 additions & 1 deletion lib/public/AppFramework/Http/DownloadResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @author Morris Jobke <[email protected]>
* @author Roeland Jago Douma <[email protected]>
* @author Thomas Müller <[email protected]>
* @author Kate Döen <[email protected]>
*
* @license AGPL-3.0
*
Expand All @@ -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<string, mixed>
* @template-extends Response<int, array<string, mixed>>
*/
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) {
Expand Down
8 changes: 6 additions & 2 deletions lib/public/AppFramework/Http/FileDisplayResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*
* @author Christoph Wurst <[email protected]>
* @author Roeland Jago Douma <[email protected]>
* @author Kate Döen <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -29,6 +30,9 @@
* Class FileDisplayResponse
*
* @since 11.0.0
* @template S of int
* @template H of array<string, mixed>
* @template-extends Response<int, array<string, mixed>>
*/
class FileDisplayResponse extends Response implements ICallbackResponse {
/** @var \OCP\Files\File|\OCP\Files\SimpleFS\ISimpleFile */
Expand All @@ -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,
Expand Down
17 changes: 11 additions & 6 deletions lib/public/AppFramework/Http/JSONResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @author Roeland Jago Douma <[email protected]>
* @author Thomas Müller <[email protected]>
* @author Thomas Tanghus <[email protected]>
* @author Kate Döen <[email protected]>
*
* @license AGPL-3.0
*
Expand All @@ -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<string, mixed>
* @template-extends Response<int, array<string, mixed>>
*/
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) {
Expand All @@ -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
Expand All @@ -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() {
Expand Down
7 changes: 6 additions & 1 deletion lib/public/AppFramework/Http/NotFoundResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @author Lukas Reschke <[email protected]>
* @author Morris Jobke <[email protected]>
* @author Roeland Jago Douma <[email protected]>
* @author Kate Döen <[email protected]>
*
* @license AGPL-3.0
*
Expand All @@ -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<string, mixed>
* @template-extends TemplateResponse<int, array<string, mixed>>
*/
class NotFoundResponse extends TemplateResponse {
/**
Expand All @@ -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);
}
}
3 changes: 3 additions & 0 deletions lib/public/AppFramework/Http/RedirectResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @author Roeland Jago Douma <[email protected]>
* @author Thomas Müller <[email protected]>
* @author v1r0x <[email protected]>
* @author Kate Döen <[email protected]>
*
* @license AGPL-3.0
*
Expand All @@ -30,6 +31,8 @@
/**
* Redirects to a different URL
* @since 7.0.0
* @template H of array<string, mixed>
* @template-extends Response<int, array<string, mixed>>
*/
class RedirectResponse extends Response {
private $redirectURL;
Expand Down
3 changes: 3 additions & 0 deletions lib/public/AppFramework/Http/RedirectToDefaultAppResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*
* @author Joas Schilling <[email protected]>
* @author Roeland Jago Douma <[email protected]>
* @author Kate Döen <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -33,6 +34,8 @@
*
* @since 16.0.0
* @deprecated 23.0.0 Use RedirectResponse() with IURLGenerator::linkToDefaultPageUrl() instead
* @template H of array<string, mixed>
* @template-extends RedirectResponse<array<string, mixed>>
*/
class RedirectToDefaultAppResponse extends RedirectResponse {
/**
Expand Down
Loading

0 comments on commit 6077be9

Please sign in to comment.