Skip to content

Commit 8969e6f

Browse files
feature #63541 [HttpFoundation] Deprecate setting public properties of Request and Response objects directly (nicolas-grekas)
This PR was merged into the 8.1 branch. Discussion ---------- [HttpFoundation] Deprecate setting public properties of `Request` and `Response` objects directly | Q | A | ------------- | --- | Branch? | 8.1 | Bug fix? | no | New feature? | yes | Deprecations? | yes | Issues | - | License | MIT PHP 8.4 property hooks allow adding a set hook to existing properties without breaking the public API. This PR leverages that to deprecate external direct assignments to Request and Response properties. The goal is to make our beloved `Request` and `Response` objects safer by using `public private(set)` in 9.0. That's only for public properties, which are the ones that hold value objects (`ParameterBag`/`HeaderBag`) which shouldn't be reassigned at will. Commits ------- 8b2209e3972 [HttpFoundation] Deprecate setting public properties of `Request` and `Response` objects directly
2 parents 0ca8d86 + b70e5ee commit 8969e6f

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

HttpClientKernel.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,25 @@ public function handle(Request $request, int $type = HttpKernelInterface::MAIN_R
5555
'body' => $body,
5656
] + $request->attributes->get('http_client_options', []));
5757

58-
$response = new Response($response->getContent(!$catch), $response->getStatusCode(), $response->getHeaders(!$catch));
59-
60-
$response->headers->remove('X-Body-File');
61-
$response->headers->remove('X-Body-Eval');
62-
$response->headers->remove('X-Content-Digest');
63-
64-
$response->headers = new class($response->headers->all()) extends ResponseHeaderBag {
58+
$headers = new class($response->getHeaders(!$catch)) extends ResponseHeaderBag {
6559
protected function computeCacheControlValue(): string
6660
{
6761
return $this->getCacheControlHeader(); // preserve the original value
6862
}
6963
};
70-
71-
return $response;
64+
$headers->remove('X-Body-File');
65+
$headers->remove('X-Body-Eval');
66+
$headers->remove('X-Content-Digest');
67+
68+
try {
69+
return new Response($response->getContent(!$catch), $response->getStatusCode(), $headers);
70+
} catch (\TypeError) {
71+
// BC with Symfony < 8.1
72+
$response = new Response($response->getContent(!$catch), $response->getStatusCode());
73+
$response->headers = $headers;
74+
75+
return $response;
76+
}
7277
}
7378

7479
private function getBody(Request $request): ?AbstractPart

Tests/HttpClientKernelTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public function testHandlePassesMaxRedirectsHttpClientOption()
2424
$request = new Request();
2525
$request->attributes->set('http_client_options', ['max_redirects' => 50]);
2626

27-
$response = $this->createMock(ResponseInterface::class);
28-
$response->expects($this->once())->method('getStatusCode')->willReturn(200);
27+
$response = $this->createStub(ResponseInterface::class);
28+
$response->method('getStatusCode')->willReturn(200);
2929

3030
$client = $this->createMock(HttpClientInterface::class);
3131
$client

0 commit comments

Comments
 (0)