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 Mar 4, 2021
1 parent 1f597a4 commit d64cbd4
Show file tree
Hide file tree
Showing 17 changed files with 74 additions and 173 deletions.
3 changes: 1 addition & 2 deletions src/Http/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,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 $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 @@ -164,9 +164,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 @@ -26,9 +26,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
18 changes: 6 additions & 12 deletions src/Http/IResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,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 @@ -166,21 +165,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 @@ -189,9 +185,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 @@ -211,7 +206,6 @@ 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,
Expand All @@ -221,7 +215,7 @@ function setCookie(
string $domain = null,
bool $secure = null,
bool $httpOnly = null,
);
): static;

/**
* Deletes a cookie.
Expand Down
15 changes: 5 additions & 10 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,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 @@ -103,9 +102,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 @@ -119,9 +117,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 @@ -135,9 +132,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 @@ -155,9 +151,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
24 changes: 8 additions & 16 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 @@ -83,10 +82,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 @@ -102,10 +100,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 @@ -115,10 +112,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 @@ -128,10 +124,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 @@ -140,10 +135,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 @@ -172,10 +166,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 $time)
public function setExpiration(?string $time): static
{
$this->setHeader('Pragma', null);
if (!$time) { // no cache
Expand Down Expand Up @@ -251,7 +244,6 @@ public function __destruct()
/**
* Sends a cookie.
* @param string|int|\DateTimeInterface $time expiration time, value null means "until the browser session ends"
* @return static
* @throws Nette\InvalidStateException if HTTP headers have been sent
*/
public function setCookie(
Expand All @@ -263,7 +255,7 @@ public function setCookie(
bool $secure = null,
bool $httpOnly = null,
string $sameSite = null,
) {
): static {
self::checkHeaders();
$options = [
'expires' => $time ? (int) DateTime::from($time)->format('U') : 0,
Expand Down
19 changes: 6 additions & 13 deletions src/Http/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,8 @@ public function getId(): string

/**
* Sets the session name to a specified one.
* @return static
*/
public function setName(string $name)
public function setName(string $name): static
{
if (!preg_match('#[^0-9.][^.]*$#DA', $name)) {
throw new Nette\InvalidArgumentException('Session name cannot contain dot.');
Expand All @@ -256,7 +255,6 @@ public function getName(): string

/**
* Returns specified session section.
* @throws Nette\InvalidArgumentException
*/
public function getSection(string $section, string $class = SessionSection::class): SessionSection
{
Expand Down Expand Up @@ -314,11 +312,10 @@ public function clean(): void

/**
* Sets session options.
* @return static
* @throws Nette\NotSupportedException
* @throws Nette\InvalidStateException
*/
public function setOptions(array $options)
public function setOptions(array $options): static
{
$normalized = [];
$allowed = ini_get_all('session', false) + ['read_and_close' => 1, 'session.cookie_samesite' => 1]; // for PHP < 7.3
Expand Down Expand Up @@ -414,9 +411,8 @@ private function configure(array $config): void
/**
* Sets the amount of time (like '20 minutes') allowed between requests before the session will be terminated,
* null means "for a maximum of 3 hours or until the browser is closed".
* @return static
*/
public function setExpiration(?string $time)
public function setExpiration(?string $time): static
{
if ($time === null) {
return $this->setOptions([
Expand All @@ -436,14 +432,13 @@ public function setExpiration(?string $time)

/**
* Sets the session cookie parameters.
* @return static
*/
public function setCookieParameters(
string $path,
string $domain = null,
bool $secure = null,
string $sameSite = null,
) {
): static {
return $this->setOptions([
'cookie_path' => $path,
'cookie_domain' => $domain,
Expand All @@ -463,9 +458,8 @@ public function getCookieParameters(): array

/**
* Sets path of the directory used to save session data.
* @return static
*/
public function setSavePath(string $path)
public function setSavePath(string $path): static
{
return $this->setOptions([
'save_path' => $path,
Expand All @@ -475,9 +469,8 @@ public function setSavePath(string $path)

/**
* Sets user session handler.
* @return static
*/
public function setHandler(\SessionHandlerInterface $handler)
public function setHandler(\SessionHandlerInterface $handler): static
{
if ($this->started) {
throw new Nette\InvalidStateException('Unable to set handler when session has been started.');
Expand Down

0 comments on commit d64cbd4

Please sign in to comment.