Skip to content

Commit f5ea7e4

Browse files
committed
IRequest, IResponse: added typehints, unification (BC break)
1 parent f39a633 commit f5ea7e4

4 files changed

Lines changed: 23 additions & 40 deletions

File tree

phpstan.neon

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ parameters:
99
- phtml
1010

1111
ignoreErrors:
12-
- # IResponse::setCookie() interface does not yet declare $sameSite parameter, Response::setCookie() does
13-
message: '#^Unknown parameter \$sameSite in call to method Nette\\Http\\IResponse\:\:setCookie\(\)\.$#'
14-
identifier: argument.unknown
15-
count: 1
16-
path: src/Http/Helpers.php
17-
1812
- # runtime validation of HTTP header value coming from untrusted client input
1913
message: '#^Right side of && is always true\.$#'
2014
identifier: booleanAnd.rightAlwaysTrue
@@ -33,12 +27,6 @@ parameters:
3327
count: 1
3428
path: src/Http/RequestFactory.php
3529

36-
- # IResponse::setCookie() interface does not yet declare $sameSite parameter, see Helpers.php
37-
message: '#^Method Nette\\Http\\IResponse\:\:setCookie\(\) invoked with 8 parameters, 3\-7 required\.$#'
38-
identifier: arguments.count
39-
count: 1
40-
path: src/Http/Session.php
41-
4230
- # session_get_cookie_params() declares 'samesite' key but it may legitimately be missing on older configurations
4331
message: '#^Offset ''samesite'' on array\{lifetime\: int\<0, max\>, path\: non\-falsy\-string, domain\: string, secure\: bool, httponly\: bool, samesite\: ''Lax''\|''lax''\|''None''\|''none''\|''Strict''\|''strict''\} on left side of \?\? always exists and is not nullable\.$#'
4432
identifier: nullCoalesce.offset

src/Http/IRequest.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,19 @@ function getUrl(): UrlScript;
5555

5656
/**
5757
* Returns a URL query parameter, or all parameters as an array if no key is given.
58-
* @return mixed
5958
*/
60-
function getQuery(?string $key = null);
59+
function getQuery(?string $key = null): mixed;
6160

6261
/**
6362
* Returns a POST parameter, or all POST parameters as an array if no key is given.
64-
* @return mixed
6563
*/
66-
function getPost(?string $key = null);
64+
function getPost(?string $key = null): mixed;
6765

6866
/**
6967
* Returns the uploaded file for the given key, or null if not present.
7068
* Accepts a string key or an array of keys for nested file structures (e.g. ['form', 'avatar']).
71-
* @return ?FileUpload
7269
*/
73-
function getFile(string $key);
70+
function getFile(string $key): ?FileUpload;
7471

7572
/**
7673
* Returns the tree of uploaded files, with each leaf being a FileUpload instance.
@@ -80,9 +77,8 @@ function getFiles(): array;
8077

8178
/**
8279
* Returns a cookie value, or null if it does not exist.
83-
* @return mixed
8480
*/
85-
function getCookie(string $key);
81+
function getCookie(string $key): mixed;
8682

8783
/**
8884
* Returns all cookies.

src/Http/IResponse.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,8 @@ interface IResponse
335335

336336
/**
337337
* Sets HTTP response code.
338-
* @return static
339338
*/
340-
function setCode(int $code, ?string $reason = null);
339+
function setCode(int $code, ?string $reason = null): static;
341340

342341
/**
343342
* Returns HTTP response code.
@@ -346,21 +345,18 @@ function getCode(): int;
346345

347346
/**
348347
* Sends an HTTP header, replacing any previously sent header with the same name.
349-
* @return static
350348
*/
351-
function setHeader(string $name, string $value);
349+
function setHeader(string $name, string $value): static;
352350

353351
/**
354352
* Adds an HTTP header without replacing a previously sent header with the same name.
355-
* @return static
356353
*/
357-
function addHeader(string $name, string $value);
354+
function addHeader(string $name, string $value): static;
358355

359356
/**
360357
* Sends a Content-type HTTP header.
361-
* @return static
362358
*/
363-
function setContentType(string $type, ?string $charset = null);
359+
function setContentType(string $type, ?string $charset = null): static;
364360

365361
/**
366362
* Redirects to a new URL.
@@ -370,9 +366,8 @@ function redirect(string $url, int $code = self::S302_Found): void;
370366
/**
371367
* Sets the Cache-Control and Expires headers. Pass a time string (e.g. '20 minutes') to enable caching,
372368
* or null to disable it.
373-
* @return static
374369
*/
375-
function setExpiration(?string $expire);
370+
function setExpiration(?string $expire): static;
376371

377372
/**
378373
* Checks whether HTTP headers have already been sent.
@@ -392,21 +387,27 @@ function getHeaders(): array;
392387

393388
/**
394389
* Sends a cookie.
395-
* @return static
396390
*/
397391
function setCookie(
398392
string $name,
399393
string $value,
400394
string|int|\DateTimeInterface|null $expire,
401395
?string $path = null,
402396
?string $domain = null,
403-
?bool $secure = null,
404-
?bool $httpOnly = null,
405-
);
397+
bool $secure = false,
398+
bool $httpOnly = true,
399+
SameSite $sameSite = SameSite::Lax,
400+
bool $partitioned = false,
401+
): static;
406402

407403
/**
408404
* Deletes a cookie.
409405
* @return void
410406
*/
411-
function deleteCookie(string $name, ?string $path = null, ?string $domain = null, ?bool $secure = null);
407+
function deleteCookie(
408+
string $name,
409+
?string $path = null,
410+
?string $domain = null,
411+
bool $secure = false,
412+
);
412413
}

src/Http/Response.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ public function getHeaders(): array
225225

226226
/**
227227
* Sends a cookie.
228-
* @param SameSite|self::SameSite*|null $sameSite
229228
* @throws Nette\InvalidStateException if HTTP headers have been sent
230229
*/
231230
public function setCookie(
@@ -235,8 +234,8 @@ public function setCookie(
235234
?string $path = null,
236235
?string $domain = null,
237236
?bool $secure = null,
238-
?bool $httpOnly = null,
239-
SameSite|string|null $sameSite = null,
237+
bool $httpOnly = true,
238+
SameSite|string|null $sameSite = SameSite::Lax,
240239
bool $partitioned = false,
241240
): static
242241
{
@@ -259,7 +258,6 @@ public function setCookie(
259258
}
260259

261260
$seconds = Helpers::expirationToSeconds($expire);
262-
$sameSite ??= SameSite::Lax->value;
263261
// both SameSite=None and Partitioned are rejected by the browser without the Secure attribute
264262
$secure = $sameSite === SameSite::None->value || $partitioned
265263
? true
@@ -271,7 +269,7 @@ public function setCookie(
271269
. '; path=' . $path
272270
. ($domain === '' ? '' : '; domain=' . $domain)
273271
. ($secure ? '; secure' : '')
274-
. (($httpOnly ?? true) ? '; HttpOnly' : '')
272+
. ($httpOnly ? '; HttpOnly' : '')
275273
. '; SameSite=' . $sameSite
276274
. ($partitioned ? '; Partitioned' : '');
277275
header('Set-Cookie: ' . $cookie, replace: false);

0 commit comments

Comments
 (0)