Skip to content

Commit e76a8f0

Browse files
committed
Throws an exception if cookie date string is unparseable
1 parent 9890014 commit e76a8f0

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

spec/CookieUtilSpec.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace spec\Http\Message;
44

5+
use Http\Message\Exception\UnexpectedValueException;
56
use PhpSpec\ObjectBehavior;
67

78
class CookieUtilSpec extends ObjectBehavior
@@ -19,10 +20,10 @@ function it_parses_cookie_date_string($cookieDateString, $expectedString)
1920
/**
2021
* @dataProvider getInvalidCookieDateStrings
2122
*/
22-
function it_returns_false_if_cookie_date_string_is_invalid($cookieDateString)
23+
function it_throws_an_exception_if_cookie_date_string_is_unparseable($cookieDateString)
2324
{
2425
$this->beConstructedThrough('parseDate', [$cookieDateString]);
25-
$this->shouldBe(false);
26+
$this->shouldThrow(UnexpectedValueException::class);
2627
}
2728

2829
/**

src/CookieUtil.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Http\Message;
44

5+
use Http\Message\Exception\UnexpectedValueException;
6+
57
final class CookieUtil
68
{
79
/**
@@ -26,7 +28,9 @@ final class CookieUtil
2628
*
2729
* @param string $dateValue
2830
*
29-
* @return \DateTime|false
31+
* @return \DateTime
32+
*
33+
* @throws UnexpectedValueException if we cannot parse the cookie date string.
3034
*/
3135
public static function parseDate($dateValue)
3236
{
@@ -35,7 +39,15 @@ public static function parseDate($dateValue)
3539
return $date;
3640
}
3741
}
42+
3843
// attempt a fallback for unusual formatting
39-
return date_create($dateValue, new \DateTimeZone('GMT'));
44+
if (false !== $date = date_create($dateValue, new \DateTimeZone('GMT'))) {
45+
return $date;
46+
}
47+
48+
throw new UnexpectedValueException(sprintf(
49+
'Unparseable cookie date string "%s"',
50+
$dateValue
51+
));
4052
}
4153
}

src/Exception.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Http\Message;
4+
/**
5+
* An interface implemented by all HTTP message related exceptions.
6+
*/
7+
interface Exception
8+
{
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Http\Message\Exception;
4+
5+
use Http\Message\Exception;
6+
7+
final class UnexpectedValueException extends \UnexpectedValueException implements Exception
8+
{
9+
}

0 commit comments

Comments
 (0)