From 689f4ae14fbe26c1c20aad3fa2e335c98132a783 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sun, 27 Nov 2022 21:36:23 +0100 Subject: [PATCH] IRequest, IResponse: added typehints, unification (BC break) --- src/Http/IRequest.php | 12 ++++-------- src/Http/IResponse.php | 30 +++++++++++++++--------------- src/Http/Response.php | 8 ++++---- 3 files changed, 23 insertions(+), 27 deletions(-) diff --git a/src/Http/IRequest.php b/src/Http/IRequest.php index 9c3fb8f7..434682c1 100644 --- a/src/Http/IRequest.php +++ b/src/Http/IRequest.php @@ -58,22 +58,19 @@ function getUrl(): UrlScript; /** * Returns variable provided to the script via URL query ($_GET). * If no key is passed, returns the entire array. - * @return mixed */ - function getQuery(?string $key = null); + function getQuery(?string $key = null): mixed; /** * Returns variable provided to the script via POST method ($_POST). * If no key is passed, returns the entire array. - * @return mixed */ - function getPost(?string $key = null); + function getPost(?string $key = null): mixed; /** * Returns uploaded file. - * @return FileUpload|array|null */ - function getFile(string $key); + function getFile(string $key): ?FileUpload; /** * Returns uploaded files. @@ -82,9 +79,8 @@ function getFiles(): array; /** * Returns variable provided to the script via HTTP cookies. - * @return mixed */ - function getCookie(string $key); + function getCookie(string $key): mixed; /** * Returns variables provided to the script via HTTP cookies. diff --git a/src/Http/IResponse.php b/src/Http/IResponse.php index b71dfe29..f3e64844 100644 --- a/src/Http/IResponse.php +++ b/src/Http/IResponse.php @@ -337,9 +337,8 @@ interface IResponse /** * Sets HTTP response code. - * @return static */ - function setCode(int $code, ?string $reason = null); + function setCode(int $code, ?string $reason = null): static; /** * Returns HTTP response code. @@ -348,21 +347,18 @@ function getCode(): int; /** * Sends a HTTP header and replaces a previous one. - * @return static */ - function setHeader(string $name, string $value); + function setHeader(string $name, string $value): static; /** * Adds HTTP header. - * @return static */ - function addHeader(string $name, string $value); + function addHeader(string $name, string $value): static; /** * Sends a Content-type HTTP header. - * @return static */ - function setContentType(string $type, ?string $charset = null); + function setContentType(string $type, ?string $charset = null): static; /** * Redirects to a new URL. @@ -371,9 +367,8 @@ function redirect(string $url, int $code = self::S302_Found): void; /** * Sets the time (like '20 minutes') before a page cached on a browser expires, null means "must-revalidate". - * @return static */ - function setExpiration(?string $expire); + function setExpiration(?string $expire): static; /** * Checks if headers have been sent. @@ -392,7 +387,6 @@ function getHeaders(): array; /** * Sends a cookie. - * @return static */ function setCookie( string $name, @@ -400,12 +394,18 @@ function setCookie( string|int|\DateTimeInterface|null $expire, ?string $path = null, ?string $domain = null, - ?bool $secure = null, - ?bool $httpOnly = null, - ); + bool $secure = false, + bool $httpOnly = true, + string $sameSite = self::SameSiteLax, + ): static; /** * Deletes a cookie. */ - function deleteCookie(string $name, ?string $path = null, ?string $domain = null, ?bool $secure = null); + function deleteCookie( + string $name, + ?string $path = null, + ?string $domain = null, + bool $secure = false, + ); } diff --git a/src/Http/Response.php b/src/Http/Response.php index 86de0b4c..65725da5 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -233,8 +233,8 @@ public function setCookie( ?string $path = null, ?string $domain = null, ?bool $secure = null, - ?bool $httpOnly = null, - ?string $sameSite = null, + bool $httpOnly = true, + string $sameSite = self::SameSiteLax, ): static { self::checkHeaders(); @@ -243,8 +243,8 @@ public function setCookie( 'path' => $path ?? ($domain ? '/' : $this->cookiePath), 'domain' => $domain ?? ($path ? '' : $this->cookieDomain), 'secure' => $secure ?? $this->cookieSecure, - 'httponly' => $httpOnly ?? true, - 'samesite' => $sameSite ?? self::SameSiteLax, + 'httponly' => $httpOnly, + 'samesite' => $sameSite, ]); return $this; }