Skip to content

Commit 10b01c9

Browse files
committed
[HttpFoundation] fix return types and handling of zero in Response
1 parent 75952af commit 10b01c9

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,8 @@ public function setDate(\DateTime $date)
614614
*/
615615
public function getAge()
616616
{
617-
if ($age = $this->headers->get('Age')) {
618-
return $age;
617+
if (null !== $age = $this->headers->get('Age')) {
618+
return (int) $age;
619619
}
620620

621621
return max(time() - $this->getDate()->format('U'), 0);
@@ -691,12 +691,12 @@ public function setExpires(\DateTime $date = null)
691691
*/
692692
public function getMaxAge()
693693
{
694-
if ($age = $this->headers->getCacheControlDirective('s-maxage')) {
695-
return $age;
694+
if ($this->headers->hasCacheControlDirective('s-maxage')) {
695+
return (int) $this->headers->getCacheControlDirective('s-maxage');
696696
}
697697

698-
if ($age = $this->headers->getCacheControlDirective('max-age')) {
699-
return $age;
698+
if ($this->headers->hasCacheControlDirective('max-age')) {
699+
return (int) $this->headers->getCacheControlDirective('max-age');
700700
}
701701

702702
if (null !== $this->getExpires()) {
@@ -757,7 +757,7 @@ public function setSharedMaxAge($value)
757757
*/
758758
public function getTtl()
759759
{
760-
if ($maxAge = $this->getMaxAge()) {
760+
if (null !== $maxAge = $this->getMaxAge()) {
761761
return $maxAge - $this->getAge();
762762
}
763763

@@ -961,7 +961,7 @@ public function setNotModified()
961961
*/
962962
public function hasVary()
963963
{
964-
return (Boolean) $this->headers->get('Vary');
964+
return null !== $this->headers->get('Vary');
965965
}
966966

967967
/**

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public function testExpire()
223223
$response = new Response();
224224
$response->headers->set('Expires', -1);
225225
$response->expire();
226-
$this->assertEquals(0, $response->headers->get('Age'), '->expire() does not set the Age to 0');
226+
$this->assertNull($response->headers->get('Age'), '->expire() does not set the Age when the response is expired');
227227
}
228228

229229
public function testGetTtl()
@@ -237,7 +237,12 @@ public function testGetTtl()
237237

238238
$response = new Response();
239239
$response->headers->set('Expires', $this->createDateTimeOneHourAgo()->format(DATE_RFC2822));
240-
$this->assertLessThan(0, $response->getTtl(), '->getTtl() returns negative values when Expires is in part');
240+
$this->assertLessThan(0, $response->getTtl(), '->getTtl() returns negative values when Expires is in past');
241+
242+
$response = new Response();
243+
$response->headers->set('Expires', $response->getDate()->format(DATE_RFC2822));
244+
$response->headers->set('Age', 0);
245+
$this->assertSame(0, $response->getTtl(), '->getTtl() correctly handles zero');
241246

242247
$response = new Response();
243248
$response->headers->set('Cache-Control', 'max-age=60');

0 commit comments

Comments
 (0)