Skip to content

Commit 947ac2e

Browse files
committed
added PHP 8 typehints
1 parent e87ddcf commit 947ac2e

17 files changed

+74
-173
lines changed

src/Http/Context.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ public function __construct(IRequest $request, IResponse $response)
3333

3434
/**
3535
* Attempts to cache the sent entity by its last modification date.
36-
* @param string|int|\DateTimeInterface $lastModified
3736
*/
38-
public function isModified($lastModified = null, string $etag = null): bool
37+
public function isModified(string|int|\DateTimeInterface $lastModified = null, string $etag = null): bool
3938
{
4039
if ($lastModified) {
4140
$this->response->setHeader('Last-Modified', Helpers::formatDate($lastModified));

src/Http/FileUpload.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,8 @@ public function hasFile(): bool
164164

165165
/**
166166
* Moves an uploaded file to a new location. If the destination file already exists, it will be overwritten.
167-
* @return static
168167
*/
169-
public function move(string $dest)
168+
public function move(string $dest): static
170169
{
171170
$dir = dirname($dest);
172171
Nette\Utils\FileSystem::createDir($dir);

src/Http/Helpers.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ final class Helpers
2626

2727
/**
2828
* Returns HTTP valid date format.
29-
* @param string|int|\DateTimeInterface $time
3029
*/
31-
public static function formatDate($time): string
30+
public static function formatDate(string|int|\DateTimeInterface $time): string
3231
{
3332
$time = DateTime::from($time)->setTimezone(new \DateTimeZone('GMT'));
3433
return $time->format('D, d M Y H:i:s \G\M\T');

src/Http/IRequest.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,19 @@ function getUrl(): UrlScript;
3737
/**
3838
* Returns variable provided to the script via URL query ($_GET).
3939
* If no key is passed, returns the entire array.
40-
* @return mixed
4140
*/
42-
function getQuery(string $key = null);
41+
function getQuery(string $key = null): mixed;
4342

4443
/**
4544
* Returns variable provided to the script via POST method ($_POST).
4645
* If no key is passed, returns the entire array.
47-
* @return mixed
4846
*/
49-
function getPost(string $key = null);
47+
function getPost(string $key = null): mixed;
5048

5149
/**
5250
* Returns uploaded file.
53-
* @return FileUpload|array|null
5451
*/
55-
function getFile(string $key);
52+
function getFile(string $key): ?FileUpload;
5653

5754
/**
5855
* Returns uploaded files.
@@ -61,9 +58,8 @@ function getFiles(): array;
6158

6259
/**
6360
* Returns variable provided to the script via HTTP cookies.
64-
* @return mixed
6561
*/
66-
function getCookie(string $key);
62+
function getCookie(string $key): mixed;
6763

6864
/**
6965
* Returns variables provided to the script via HTTP cookies.

src/Http/IResponse.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,8 @@ interface IResponse
155155

156156
/**
157157
* Sets HTTP response code.
158-
* @return static
159158
*/
160-
function setCode(int $code, string $reason = null);
159+
function setCode(int $code, string $reason = null): static;
161160

162161
/**
163162
* Returns HTTP response code.
@@ -166,21 +165,18 @@ function getCode(): int;
166165

167166
/**
168167
* Sends a HTTP header and replaces a previous one.
169-
* @return static
170168
*/
171-
function setHeader(string $name, string $value);
169+
function setHeader(string $name, string $value): static;
172170

173171
/**
174172
* Adds HTTP header.
175-
* @return static
176173
*/
177-
function addHeader(string $name, string $value);
174+
function addHeader(string $name, string $value): static;
178175

179176
/**
180177
* Sends a Content-type HTTP header.
181-
* @return static
182178
*/
183-
function setContentType(string $type, string $charset = null);
179+
function setContentType(string $type, string $charset = null): static;
184180

185181
/**
186182
* Redirects to a new URL.
@@ -189,9 +185,8 @@ function redirect(string $url, int $code = self::S302_FOUND): void;
189185

190186
/**
191187
* Sets the time (like '20 minutes') before a page cached on a browser expires, null means "must-revalidate".
192-
* @return static
193188
*/
194-
function setExpiration(?string $expire);
189+
function setExpiration(?string $expire): static;
195190

196191
/**
197192
* Checks if headers have been sent.
@@ -211,7 +206,6 @@ function getHeaders(): array;
211206
/**
212207
* Sends a cookie.
213208
* @param string|int|\DateTimeInterface $expire time, value null means "until the browser session ends"
214-
* @return static
215209
*/
216210
function setCookie(
217211
string $name,
@@ -221,7 +215,7 @@ function setCookie(
221215
string $domain = null,
222216
bool $secure = null,
223217
bool $httpOnly = null,
224-
);
218+
): static;
225219

226220
/**
227221
* Deletes a cookie.

src/Http/Request.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,8 @@ public function __construct(
7878

7979
/**
8080
* Returns a clone with a different URL.
81-
* @return static
8281
*/
83-
public function withUrl(UrlScript $url)
82+
public function withUrl(UrlScript $url): static
8483
{
8584
$dolly = clone $this;
8685
$dolly->url = $url;
@@ -103,9 +102,8 @@ public function getUrl(): UrlScript
103102
/**
104103
* Returns variable provided to the script via URL query ($_GET).
105104
* If no key is passed, returns the entire array.
106-
* @return mixed
107105
*/
108-
public function getQuery(string $key = null)
106+
public function getQuery(string $key = null): mixed
109107
{
110108
if (func_num_args() === 0) {
111109
return $this->url->getQueryParameters();
@@ -119,9 +117,8 @@ public function getQuery(string $key = null)
119117
/**
120118
* Returns variable provided to the script via POST method ($_POST).
121119
* If no key is passed, returns the entire array.
122-
* @return mixed
123120
*/
124-
public function getPost(string $key = null)
121+
public function getPost(string $key = null): mixed
125122
{
126123
if (func_num_args() === 0) {
127124
return $this->post;
@@ -135,9 +132,8 @@ public function getPost(string $key = null)
135132
/**
136133
* Returns uploaded file.
137134
* @param string|string[] $key
138-
* @return ?FileUpload
139135
*/
140-
public function getFile($key)
136+
public function getFile($key): ?FileUpload
141137
{
142138
$res = Nette\Utils\Arrays::get($this->files, $key, null);
143139
return $res instanceof FileUpload ? $res : null;
@@ -155,9 +151,8 @@ public function getFiles(): array
155151

156152
/**
157153
* Returns a cookie or `null` if it does not exist.
158-
* @return mixed
159154
*/
160-
public function getCookie(string $key)
155+
public function getCookie(string $key): mixed
161156
{
162157
if (func_num_args() > 1) {
163158
trigger_error(__METHOD__ . '() parameter $default is deprecated, use operator ??', E_USER_DEPRECATED);

src/Http/RequestFactory.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ class RequestFactory
3434
private array $proxies = [];
3535

3636

37-
/** @return static */
38-
public function setBinary(bool $binary = true)
37+
public function setBinary(bool $binary = true): static
3938
{
4039
$this->binary = $binary;
4140
return $this;
@@ -44,9 +43,8 @@ public function setBinary(bool $binary = true)
4443

4544
/**
4645
* @param string|string[] $proxy
47-
* @return static
4846
*/
49-
public function setProxy($proxy)
47+
public function setProxy($proxy): static
5048
{
5149
$this->proxies = (array) $proxy;
5250
return $this;

src/Http/Response.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,10 @@ public function __construct()
5454

5555
/**
5656
* Sets HTTP response code.
57-
* @return static
5857
* @throws Nette\InvalidArgumentException if code is invalid
5958
* @throws Nette\InvalidStateException if HTTP headers have been sent
6059
*/
61-
public function setCode(int $code, string $reason = null)
60+
public function setCode(int $code, string $reason = null): static
6261
{
6362
if ($code < 100 || $code > 599) {
6463
throw new Nette\InvalidArgumentException("Bad HTTP response '$code'.");
@@ -83,10 +82,9 @@ public function getCode(): int
8382

8483
/**
8584
* Sends an HTTP header and overwrites previously sent header of the same name.
86-
* @return static
8785
* @throws Nette\InvalidStateException if HTTP headers have been sent
8886
*/
89-
public function setHeader(string $name, ?string $value)
87+
public function setHeader(string $name, ?string $value): static
9088
{
9189
self::checkHeaders();
9290
if ($value === null) {
@@ -102,10 +100,9 @@ public function setHeader(string $name, ?string $value)
102100

103101
/**
104102
* Sends an HTTP header and doesn't overwrite previously sent header of the same name.
105-
* @return static
106103
* @throws Nette\InvalidStateException if HTTP headers have been sent
107104
*/
108-
public function addHeader(string $name, string $value)
105+
public function addHeader(string $name, string $value): static
109106
{
110107
self::checkHeaders();
111108
header($name . ': ' . $value, false);
@@ -115,10 +112,9 @@ public function addHeader(string $name, string $value)
115112

116113
/**
117114
* Deletes a previously sent HTTP header.
118-
* @return static
119115
* @throws Nette\InvalidStateException if HTTP headers have been sent
120116
*/
121-
public function deleteHeader(string $name)
117+
public function deleteHeader(string $name): static
122118
{
123119
self::checkHeaders();
124120
header_remove($name);
@@ -128,10 +124,9 @@ public function deleteHeader(string $name)
128124

129125
/**
130126
* Sends a Content-type HTTP header.
131-
* @return static
132127
* @throws Nette\InvalidStateException if HTTP headers have been sent
133128
*/
134-
public function setContentType(string $type, string $charset = null)
129+
public function setContentType(string $type, string $charset = null): static
135130
{
136131
$this->setHeader('Content-Type', $type . ($charset ? '; charset=' . $charset : ''));
137132
return $this;
@@ -140,10 +135,9 @@ public function setContentType(string $type, string $charset = null)
140135

141136
/**
142137
* Response should be downloaded with 'Save as' dialog.
143-
* @return static
144138
* @throws Nette\InvalidStateException if HTTP headers have been sent
145139
*/
146-
public function sendAsFile(string $fileName)
140+
public function sendAsFile(string $fileName): static
147141
{
148142
$this->setHeader(
149143
'Content-Disposition',
@@ -172,10 +166,9 @@ public function redirect(string $url, int $code = self::S302_FOUND): void
172166
/**
173167
* Sets the expiration of the HTTP document using the `Cache-Control` and `Expires` headers.
174168
* The parameter is either a time interval (as text) or `null`, which disables caching.
175-
* @return static
176169
* @throws Nette\InvalidStateException if HTTP headers have been sent
177170
*/
178-
public function setExpiration(?string $time)
171+
public function setExpiration(?string $time): static
179172
{
180173
$this->setHeader('Pragma', null);
181174
if (!$time) { // no cache
@@ -251,7 +244,6 @@ public function __destruct()
251244
/**
252245
* Sends a cookie.
253246
* @param string|int|\DateTimeInterface $time expiration time, value null means "until the browser session ends"
254-
* @return static
255247
* @throws Nette\InvalidStateException if HTTP headers have been sent
256248
*/
257249
public function setCookie(
@@ -263,7 +255,7 @@ public function setCookie(
263255
bool $secure = null,
264256
bool $httpOnly = null,
265257
string $sameSite = null,
266-
) {
258+
): static {
267259
self::checkHeaders();
268260
$options = [
269261
'expires' => $time ? (int) DateTime::from($time)->format('U') : 0,

src/Http/Session.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,8 @@ public function getId(): string
227227

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

257256
/**
258257
* Returns specified session section.
259-
* @throws Nette\InvalidArgumentException
260258
*/
261259
public function getSection(string $section, string $class = SessionSection::class): SessionSection
262260
{
@@ -314,11 +312,10 @@ public function clean(): void
314312

315313
/**
316314
* Sets session options.
317-
* @return static
318315
* @throws Nette\NotSupportedException
319316
* @throws Nette\InvalidStateException
320317
*/
321-
public function setOptions(array $options)
318+
public function setOptions(array $options): static
322319
{
323320
$normalized = [];
324321
$allowed = ini_get_all('session', false) + ['read_and_close' => 1, 'session.cookie_samesite' => 1]; // for PHP < 7.3
@@ -414,9 +411,8 @@ private function configure(array $config): void
414411
/**
415412
* Sets the amount of time (like '20 minutes') allowed between requests before the session will be terminated,
416413
* null means "for a maximum of 3 hours or until the browser is closed".
417-
* @return static
418414
*/
419-
public function setExpiration(?string $time)
415+
public function setExpiration(?string $time): static
420416
{
421417
if ($time === null) {
422418
return $this->setOptions([
@@ -436,14 +432,13 @@ public function setExpiration(?string $time)
436432

437433
/**
438434
* Sets the session cookie parameters.
439-
* @return static
440435
*/
441436
public function setCookieParameters(
442437
string $path,
443438
string $domain = null,
444439
bool $secure = null,
445440
string $sameSite = null,
446-
) {
441+
): static {
447442
return $this->setOptions([
448443
'cookie_path' => $path,
449444
'cookie_domain' => $domain,
@@ -463,9 +458,8 @@ public function getCookieParameters(): array
463458

464459
/**
465460
* Sets path of the directory used to save session data.
466-
* @return static
467461
*/
468-
public function setSavePath(string $path)
462+
public function setSavePath(string $path): static
469463
{
470464
return $this->setOptions([
471465
'save_path' => $path,
@@ -475,9 +469,8 @@ public function setSavePath(string $path)
475469

476470
/**
477471
* Sets user session handler.
478-
* @return static
479472
*/
480-
public function setHandler(\SessionHandlerInterface $handler)
473+
public function setHandler(\SessionHandlerInterface $handler): static
481474
{
482475
if ($this->started) {
483476
throw new Nette\InvalidStateException('Unable to set handler when session has been started.');

0 commit comments

Comments
 (0)