Skip to content
This repository was archived by the owner on Jun 4, 2025. It is now read-only.

Commit 21f9104

Browse files
committed
Enforce immutability with Psalm
1 parent b154d0e commit 21f9104

File tree

7 files changed

+36
-12
lines changed

7 files changed

+36
-12
lines changed

src/Message.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
/**
88
* Base class for Request and Response. This class is immutable.
9+
*
10+
* @psalm-immutable
911
*/
1012
abstract class Message
1113
{

src/Path.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
/**
88
* The path of a URL.
9+
*
10+
* @psalm-immutable
911
*/
1012
final class Path
1113
{

src/Request.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
/**
1010
* Represents an HTTP request received by the server. This class is immutable.
11+
*
12+
* @psalm-immutable
1113
*/
1214
final class Request extends Message
1315
{
@@ -332,6 +334,7 @@ public function withQuery(array $query): Request
332334

333335
// Ensure that we get a value for getQuery() that's consistent with the query string, and whose scalar values
334336
// have all been converted to strings, to get a result similar to what we'd get with an incoming HTTP request.
337+
/** @psalm-suppress ImpureFunctionCall */
335338
parse_str($that->queryString, $that->query);
336339

337340
$that->requestUri = $that->path;
@@ -378,6 +381,7 @@ public function withPost(array $post): Request
378381

379382
// Ensure that we get a value for getQuery() that's consistent with the query string, and whose scalar values
380383
// have all been converted to strings, to get a result similar to what we'd get with an incoming HTTP request.
384+
/** @psalm-suppress ImpureFunctionCall */
381385
parse_str(http_build_query($post), $that->post);
382386

383387
if (! $that->isContentType('multipart/form-data')) {
@@ -540,6 +544,8 @@ public function withCookies(array $cookies): Request
540544
$that = clone $this;
541545

542546
$query = http_build_query($cookies);
547+
548+
/** @psalm-suppress ImpureFunctionCall */
543549
parse_str($query, $that->cookies);
544550

545551
if ($cookies) {
@@ -855,7 +861,9 @@ public function withPath(string $path): Request
855861
$that = clone $this;
856862
$that->path = $path;
857863

858-
return $that->updateRequestUri();
864+
$that->requestUri = $that->recomputeRequestUri();
865+
866+
return $that;
859867
}
860868

861869
/**
@@ -890,25 +898,27 @@ public function withQueryString(string $queryString): Request
890898
$that = clone $this;
891899

892900
$that->queryString = $queryString;
901+
902+
/** @psalm-suppress ImpureFunctionCall */
893903
parse_str($that->queryString, $that->query);
894904

895-
return $that->updateRequestUri();
905+
$that->requestUri = $that->recomputeRequestUri();
906+
907+
return $that;
896908
}
897909

898910
/**
899-
* Updates the request URI from the values of path and query string.
900-
*
901-
* @return static This request.
911+
* Recomputes the request URI from the values of path and query string.
902912
*/
903-
private function updateRequestUri() : Request
913+
private function recomputeRequestUri(): string
904914
{
905-
$this->requestUri = $this->path;
915+
$requestUri = $this->path;
906916

907917
if ($this->queryString !== '') {
908-
$this->requestUri .= '?' . $this->queryString;
918+
$requestUri .= '?' . $this->queryString;
909919
}
910920

911-
return $this;
921+
return $requestUri;
912922
}
913923

914924
/**
@@ -953,6 +963,7 @@ public function withRequestUri(string $requestUri): Request
953963
if ($that->queryString === '') {
954964
$that->query = [];
955965
} else {
966+
/** @psalm-suppress ImpureFunctionCall */
956967
parse_str($that->queryString, $that->query);
957968
}
958969
}
@@ -1037,21 +1048,20 @@ public function withUrl(string $url): Request
10371048

10381049
if (isset($components['query'])) {
10391050
$that->queryString = $components['query'];
1051+
/** @psalm-suppress ImpureFunctionCall */
10401052
parse_str($that->queryString, $that->query);
10411053
$requestUri .= '?' . $that->queryString;
10421054
} else {
10431055
$that->queryString = '';
10441056
$that->query = [];
10451057
}
10461058

1047-
$that = $that->withHeader('Host', $hostHeader);
1048-
10491059
$that->host = $host;
10501060
$that->port = $port;
10511061
$that->isSecure = $isSecure;
10521062
$that->requestUri = $requestUri;
10531063

1054-
return $that;
1064+
return $that->withHeader('Host', $hostHeader);
10551065
}
10561066

10571067
/**

src/Response.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
/**
88
* Represents an HTTP response to send back to the client. This class is immutable.
9+
*
10+
* @psalm-immutable
911
*/
1012
final class Response extends Message
1113
{
@@ -311,6 +313,8 @@ public function isStatusCode(int $statusCode) : bool
311313
*
312314
* This method will fail (return `false`) if the headers have been already sent.
313315
*
316+
* @psalm-suppress ImpureFunctionCall
317+
*
314318
* @return bool Whether the response has been successfully sent.
315319
*/
316320
public function send() : bool

src/UploadedFile.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
/**
88
* Represents a file uploaded via an HTML form (RFC 1867).
9+
*
10+
* @psalm-immutable
911
*/
1012
final class UploadedFile
1113
{

src/UploadedFileMap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
/**
88
* Builds collections of UploadedFile instances, potentially nested in sub-arrays.
9+
*
10+
* @psalm-immutable
911
*/
1012
final class UploadedFileMap
1113
{

src/Url.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* - If a path is not present, it is defaulted to a single `/` character;
1515
* - If a query separator is present but the query string is empty, the separator is removed;
1616
* - If a fragment separator is present but the fragment is empty, the separator is removed.
17+
*
18+
* @psalm-immutable
1719
*/
1820
final class Url
1921
{

0 commit comments

Comments
 (0)