Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3cea8e7

Browse files
committed
bug #45631 [HttpFoundation] Fix PHP 8.1 deprecation in Response::isNotModified (HypeMC)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpFoundation] Fix PHP 8.1 deprecation in `Response::isNotModified` | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - If an `If-None-Match` header is provided in the request and there's no `ETag` header in the response, the following deprecation notice occurs on PHP 8.1 in `Response::isNotModified`: ``` Deprecated: strncmp(): Passing null to parameter #1 ($string1) of type string is deprecated ``` Commits ------- b909acf [HttpFoundation] Fix PHP 8.1 deprecation in isNotModified
2 parents 6ef994c + b909acf commit 3cea8e7

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,8 +1079,7 @@ public function isNotModified(Request $request): bool
10791079
$lastModified = $this->headers->get('Last-Modified');
10801080
$modifiedSince = $request->headers->get('If-Modified-Since');
10811081

1082-
if ($ifNoneMatchEtags = $request->getETags()) {
1083-
$etag = $this->getEtag();
1082+
if (($ifNoneMatchEtags = $request->getETags()) && (null !== $etag = $this->getEtag())) {
10841083
if (0 == strncmp($etag, 'W/', 2)) {
10851084
$etag = substr($etag, 2);
10861085
}

src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,20 @@ public function testIsNotModifiedIfModifiedSinceAndEtagWithoutLastModified()
284284
$this->assertFalse($response->isNotModified($request));
285285
}
286286

287+
public function testIfNoneMatchWithoutETag()
288+
{
289+
$request = new Request();
290+
$request->headers->set('If-None-Match', 'randomly_generated_etag');
291+
292+
$this->assertFalse((new Response())->isNotModified($request));
293+
294+
// Test wildcard
295+
$request = new Request();
296+
$request->headers->set('If-None-Match', '*');
297+
298+
$this->assertFalse((new Response())->isNotModified($request));
299+
}
300+
287301
public function testIsValidateable()
288302
{
289303
$response = new Response('', 200, ['Last-Modified' => $this->createDateTimeOneHourAgo()->format(\DATE_RFC2822)]);

0 commit comments

Comments
 (0)