Skip to content

Commit

Permalink
added PHP 8 typehints
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 30, 2022
1 parent 43deaa7 commit cbf7edb
Show file tree
Hide file tree
Showing 20 changed files with 104 additions and 204 deletions.
3 changes: 1 addition & 2 deletions src/Http/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ public function __construct(IRequest $request, IResponse $response)

/**
* Attempts to cache the sent entity by its last modification date.
* @param string|int|\DateTimeInterface $lastModified
*/
public function isModified($lastModified = null, ?string $etag = null): bool
public function isModified(string|int|\DateTimeInterface|null $lastModified = null, ?string $etag = null): bool
{
if ($lastModified) {
$this->response->setHeader('Last-Modified', Helpers::formatDate($lastModified));
Expand Down
3 changes: 1 addition & 2 deletions src/Http/FileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,8 @@ public function hasFile(): bool

/**
* Moves an uploaded file to a new location. If the destination file already exists, it will be overwritten.
* @return static
*/
public function move(string $dest)
public function move(string $dest): static
{
$dir = dirname($dest);
Nette\Utils\FileSystem::createDir($dir);
Expand Down
3 changes: 1 addition & 2 deletions src/Http/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ final class Helpers

/**
* Returns HTTP valid date format.
* @param string|int|\DateTimeInterface $time
*/
public static function formatDate($time): string
public static function formatDate(string|int|\DateTimeInterface $time): string
{
$time = DateTime::from($time)->setTimezone(new \DateTimeZone('GMT'));
return $time->format('D, d M Y H:i:s \G\M\T');
Expand Down
12 changes: 4 additions & 8 deletions src/Http/IRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,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.
Expand All @@ -61,9 +58,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.
Expand Down
28 changes: 13 additions & 15 deletions src/Http/IResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,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.
Expand All @@ -354,21 +353,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.
Expand All @@ -377,9 +373,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.
Expand All @@ -398,21 +393,24 @@ function getHeaders(): array;

/**
* Sends a cookie.
* @param string|int|\DateTimeInterface $expire time, value null means "until the browser session ends"
* @return static
*/
function setCookie(
string $name,
string $value,
$expire,
?int $expire,
?string $path = null,
?string $domain = null,
?bool $secure = null,
?bool $httpOnly = null,
);
): 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 = null,
);
}
15 changes: 5 additions & 10 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ public function __construct(

/**
* Returns a clone with a different URL.
* @return static
*/
public function withUrl(UrlScript $url)
public function withUrl(UrlScript $url): static
{
$dolly = clone $this;
$dolly->url = $url;
Expand All @@ -96,9 +95,8 @@ public function getUrl(): UrlScript
/**
* Returns variable provided to the script via URL query ($_GET).
* If no key is passed, returns the entire array.
* @return mixed
*/
public function getQuery(?string $key = null)
public function getQuery(?string $key = null): mixed
{
if (func_num_args() === 0) {
return $this->url->getQueryParameters();
Expand All @@ -113,9 +111,8 @@ public function getQuery(?string $key = null)
/**
* Returns variable provided to the script via POST method ($_POST).
* If no key is passed, returns the entire array.
* @return mixed
*/
public function getPost(?string $key = null)
public function getPost(?string $key = null): mixed
{
if (func_num_args() === 0) {
return $this->post;
Expand All @@ -130,9 +127,8 @@ public function getPost(?string $key = null)
/**
* Returns uploaded file.
* @param string|string[] $key
* @return ?FileUpload
*/
public function getFile($key)
public function getFile($key): ?FileUpload
{
$res = Nette\Utils\Arrays::get($this->files, $key, null);
return $res instanceof FileUpload ? $res : null;
Expand All @@ -150,9 +146,8 @@ public function getFiles(): array

/**
* Returns a cookie or `null` if it does not exist.
* @return mixed
*/
public function getCookie(string $key)
public function getCookie(string $key): mixed
{
if (func_num_args() > 1) {
trigger_error(__METHOD__ . '() parameter $default is deprecated, use operator ??', E_USER_DEPRECATED);
Expand Down
6 changes: 2 additions & 4 deletions src/Http/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ class RequestFactory
private array $proxies = [];


/** @return static */
public function setBinary(bool $binary = true)
public function setBinary(bool $binary = true): static
{
$this->binary = $binary;
return $this;
Expand All @@ -44,9 +43,8 @@ public function setBinary(bool $binary = true)

/**
* @param string|string[] $proxy
* @return static
*/
public function setProxy($proxy)
public function setProxy($proxy): static
{
$this->proxies = (array) $proxy;
return $this;
Expand Down
35 changes: 16 additions & 19 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@ public function __construct()

/**
* Sets HTTP response code.
* @return static
* @throws Nette\InvalidArgumentException if code is invalid
* @throws Nette\InvalidStateException if HTTP headers have been sent
*/
public function setCode(int $code, ?string $reason = null)
public function setCode(int $code, ?string $reason = null): static
{
if ($code < 100 || $code > 599) {
throw new Nette\InvalidArgumentException("Bad HTTP response '$code'.");
Expand All @@ -84,10 +83,9 @@ public function getCode(): int

/**
* Sends an HTTP header and overwrites previously sent header of the same name.
* @return static
* @throws Nette\InvalidStateException if HTTP headers have been sent
*/
public function setHeader(string $name, ?string $value)
public function setHeader(string $name, ?string $value): static
{
self::checkHeaders();
if ($value === null) {
Expand All @@ -104,10 +102,9 @@ public function setHeader(string $name, ?string $value)

/**
* Sends an HTTP header and doesn't overwrite previously sent header of the same name.
* @return static
* @throws Nette\InvalidStateException if HTTP headers have been sent
*/
public function addHeader(string $name, string $value)
public function addHeader(string $name, string $value): static
{
self::checkHeaders();
header($name . ': ' . $value, false);
Expand All @@ -117,10 +114,9 @@ public function addHeader(string $name, string $value)

/**
* Deletes a previously sent HTTP header.
* @return static
* @throws Nette\InvalidStateException if HTTP headers have been sent
*/
public function deleteHeader(string $name)
public function deleteHeader(string $name): static
{
self::checkHeaders();
header_remove($name);
Expand All @@ -130,10 +126,9 @@ public function deleteHeader(string $name)

/**
* Sends a Content-type HTTP header.
* @return static
* @throws Nette\InvalidStateException if HTTP headers have been sent
*/
public function setContentType(string $type, ?string $charset = null)
public function setContentType(string $type, ?string $charset = null): static
{
$this->setHeader('Content-Type', $type . ($charset ? '; charset=' . $charset : ''));
return $this;
Expand All @@ -142,10 +137,9 @@ public function setContentType(string $type, ?string $charset = null)

/**
* Response should be downloaded with 'Save as' dialog.
* @return static
* @throws Nette\InvalidStateException if HTTP headers have been sent
*/
public function sendAsFile(string $fileName)
public function sendAsFile(string $fileName): static
{
$this->setHeader(
'Content-Disposition',
Expand Down Expand Up @@ -174,10 +168,9 @@ public function redirect(string $url, int $code = self::S302_Found): void
/**
* Sets the expiration of the HTTP document using the `Cache-Control` and `Expires` headers.
* The parameter is either a time interval (as text) or `null`, which disables caching.
* @return static
* @throws Nette\InvalidStateException if HTTP headers have been sent
*/
public function setExpiration(?string $expire)
public function setExpiration(?string $expire): static
{
$this->setHeader('Pragma', null);
if (!$expire) { // no cache
Expand Down Expand Up @@ -255,20 +248,19 @@ public function __destruct()

/**
* Sends a cookie.
* @param string|int|\DateTimeInterface $expire expiration time, value null means "until the browser session ends"
* @return static
* @throws Nette\InvalidStateException if HTTP headers have been sent
*/
public function setCookie(
string $name,
string $value,
$expire,
string|int|\DateTimeInterface|null $expire,
?string $path = null,
?string $domain = null,
?bool $secure = null,
?bool $httpOnly = null,
?string $sameSite = null,
) {
): static
{
self::checkHeaders();
setcookie($name, $value, [
'expires' => $expire ? (int) DateTime::from($expire)->format('U') : 0,
Expand All @@ -286,7 +278,12 @@ public function setCookie(
* Deletes a cookie.
* @throws Nette\InvalidStateException if HTTP headers have been sent
*/
public function deleteCookie(string $name, ?string $path = null, ?string $domain = null, ?bool $secure = null): void
public function deleteCookie(
string $name,
?string $path = null,
?string $domain = null,
?bool $secure = null,
): void
{
$this->setCookie($name, '', 0, $path, $domain, $secure);
}
Expand Down
Loading

0 comments on commit cbf7edb

Please sign in to comment.