Skip to content

Commit

Permalink
Apply fixes from StyleCI
Browse files Browse the repository at this point in the history
  • Loading branch information
samdark authored and StyleCIBot committed Nov 20, 2020
1 parent 0736adc commit 2437cc5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 21 deletions.
34 changes: 27 additions & 7 deletions src/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

namespace Yiisoft\Cookies;

use function array_filter;
use function array_shift;
use DateInterval;
use DateTimeImmutable;
use DateTimeInterface;
use Exception;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;

use function array_filter;
use function array_shift;
use function explode;
use function implode;
use function in_array;
use InvalidArgumentException;
use function preg_match;
use function preg_split;
use Psr\Http\Message\ResponseInterface;
use function strtolower;
use function time;

Expand All @@ -26,9 +26,9 @@
*/
final class Cookie
{

/**
* Regular Expression used to validate cookie name.
*
* @link https://tools.ietf.org/html/rfc6265#section-4.1.1
* @link https://tools.ietf.org/html/rfc2616#section-2.2
*/
Expand All @@ -40,6 +40,7 @@ final class Cookie
* E.g. a POST request from https://otherdomain.com to https://yourdomain.com will not include the cookie, however a GET request will.
* When a user follows a link from https://otherdomain.com to https://yourdomain.com it will include the cookie.
* This is the default value in modern browsers.
*
* @see $sameSite
*/
public const SAME_SITE_LAX = 'Lax';
Expand All @@ -49,13 +50,15 @@ final class Cookie
* regardless of the request method and even when following a regular link.
* E.g. a GET request from https://otherdomain.com to https://yourdomain.com or a user following a link from
* https://otherdomain.com to https://yourdomain.com will not include the cookie.
*
* @see $sameSite
*/
public const SAME_SITE_STRICT = 'Strict';

/**
* SameSite policy `None` cookies will be sent in all contexts, i.e. sending cross-origin is allowed.
* `None` requires the `Secure` attribute in latest browser versions.
*
* @see $sameSite
*/
public const SAME_SITE_NONE = 'None';
Expand All @@ -76,6 +79,7 @@ final class Cookie
* @var DateTimeInterface|null The maximum lifetime of the cookie.
* If unspecified, the cookie becomes a session cookie, which will be removed
* when the client shuts down.
*
* @link https://tools.ietf.org/html/rfc6265#section-4.1.1
* @link https://tools.ietf.org/html/rfc1123#page-55
*/
Expand Down Expand Up @@ -111,6 +115,7 @@ final class Cookie
/**
* @var string|null Asserts that a cookie must not be sent with cross-origin requests.
* This provides some protection against cross-site request forgery attacks (CSRF).
*
* @see https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#samesite-cookie-attribute More information about sameSite.
*/
private ?string $sameSite = null;
Expand Down Expand Up @@ -167,7 +172,9 @@ public function getName(): string
* Creates a cookie copy with a new value.
*
* @param $value string Value of the cookie.
*
* @return static
*
* @see $value for more information.
*/
public function withValue(string $value): self
Expand Down Expand Up @@ -196,7 +203,9 @@ private function setValue(string $value): void
* Creates a cookie copy with a new time the cookie expires.
*
* @param DateTimeInterface $dateTime
*
* @return static
*
* @see $expires for more information.
*/
public function withExpires(DateTimeInterface $dateTime): self
Expand Down Expand Up @@ -240,6 +249,7 @@ public function isExpired(): bool
* If zero or negative interval is passed, the cookie will expire immediately.
*
* @param DateInterval $interval Interval until the cookie expires.
*
* @return static
*/
public function withMaxAge(DateInterval $interval): self
Expand All @@ -251,6 +261,7 @@ public function withMaxAge(DateInterval $interval): self

/**
* Returns modified cookie that will expire immediately.
*
* @return static
*/
public function expire(): self
Expand All @@ -277,6 +288,7 @@ public function expireWhenBrowserIsClosed(): self
* Creates a cookie copy with a new domain set.
*
* @param string $domain
*
* @return static
*/
public function withDomain(string $domain): self
Expand All @@ -300,7 +312,9 @@ public function getDomain(): ?string
* Creates a cookie copy with a new path set.
*
* @param string $path To be set for the cookie.
*
* @return static
*
* @see $path for more information.
*/
public function withPath(string $path): self
Expand Down Expand Up @@ -333,6 +347,7 @@ public function getPath(): ?string
* Creates a cookie copy by making it secure or insecure.
*
* @param bool $secure Whether the cookie must be secure.
*
* @return static
*/
public function withSecure(bool $secure = true): self
Expand All @@ -356,6 +371,7 @@ public function isSecure(): bool
* Creates a cookie copy that would be accessible only through the HTTP protocol.
*
* @param bool $httpOnly
*
* @return static
*/
public function withHttpOnly(bool $httpOnly = true): self
Expand All @@ -379,6 +395,7 @@ public function isHttpOnly(): bool
* Creates a cookie copy with SameSite attribute.
*
* @param string $sameSite
*
* @return static
*/
public function withSameSite(string $sameSite): self
Expand Down Expand Up @@ -419,6 +436,7 @@ public function getSameSite(): ?string
* Adds the cookie to the response and returns it.
*
* @param ResponseInterface $response
*
* @return ResponseInterface Response with added cookie.
*/
public function addToResponse(ResponseInterface $response): ResponseInterface
Expand All @@ -434,7 +452,7 @@ public function addToResponse(ResponseInterface $response): ResponseInterface
public function __toString(): string
{
$cookieParts = [
$this->name . '=' . urlencode($this->value)
$this->name . '=' . urlencode($this->value),
];

if ($this->expires !== null) {
Expand Down Expand Up @@ -469,8 +487,10 @@ public function __toString(): string
* Parse `Set-Cookie` string and build Cookie object.
*
* @param string $string `Set-Cookie` header value to parse.
* @return static
*
* @throws Exception
*
* @return static
*/
public static function fromCookieString(string $string): self
{
Expand Down
34 changes: 25 additions & 9 deletions src/CookieCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,28 @@

namespace Yiisoft\Cookies;

use function array_keys;
use function array_values;
use function array_walk;
use ArrayAccess;
use ArrayIterator;
use function count;
use Countable;
use Exception;

use function in_array;
use InvalidArgumentException;
use IteratorAggregate;
use Psr\Http\Message\ResponseInterface;
use Yiisoft\Http\Header;

use function array_keys;
use function array_values;
use function array_walk;
use function count;
use function in_array;

/**
* A CookieCollection helps to work with many cookies at once and to read / modify response cookies.
*
* @see Cookie
*/
final class CookieCollection implements IteratorAggregate, ArrayAccess, Countable
{

/**
* @var Cookie[] The cookies in this collection (indexed by the cookie name).
* @psalm-var array<string, Cookie>
Expand All @@ -36,7 +35,7 @@ final class CookieCollection implements IteratorAggregate, ArrayAccess, Countabl
/**
* CookieCollection constructor.
*
* @param Cookie[]|array $cookies The cookies that this collection initially contains.
* @param array|Cookie[] $cookies The cookies that this collection initially contains.
*/
public function __construct(array $cookies = [])
{
Expand Down Expand Up @@ -79,6 +78,7 @@ public function getIterator(): ArrayIterator
* This is equivalent to {@see has()}.
*
* @param string $name The cookie name.
*
* @return bool Whether the named cookie exists.
*/
public function offsetExists($name): bool
Expand All @@ -93,6 +93,7 @@ public function offsetExists($name): bool
* This is equivalent to {@see get()}.
*
* @param string $name The cookie name.
*
* @return Cookie|null The cookie with the specified name, null if the named cookie does not exist.
*/
public function offsetGet($name): ?Cookie
Expand Down Expand Up @@ -143,7 +144,9 @@ public function count(): int
* Returns the cookie with the specified name.
*
* @param string $name The cookie name.
*
* @return Cookie|null The cookie with the specified name. Null if the named cookie does not exist.
*
* @see getValue()
*/
public function get(string $name): ?Cookie
Expand All @@ -156,7 +159,9 @@ public function get(string $name): ?Cookie
*
* @param string $name The cookie name.
* @param string|null $defaultValue The value that should be returned when the named cookie does not exist.
*
* @return string|null The value of the named cookie or the default value if cookie is not set.
*
* @see get()
*/
public function getValue(string $name, ?string $defaultValue = null): ?string
Expand All @@ -179,7 +184,9 @@ public function add(Cookie $cookie): void
* Returns whether there is a cookie with the specified name.
*
* @param string $name The cookie name.
*
* @return bool Whether the named cookie exists.
*
* @see remove()
*/
public function has(string $name): bool
Expand All @@ -191,6 +198,7 @@ public function has(string $name): bool
* Removes a cookie.
*
* @param string $name The name of the cookie to be removed.
*
* @return Cookie|null Cookie that was removed.
*/
public function remove(string $name): ?Cookie
Expand All @@ -217,7 +225,9 @@ public function clear(): void
* Returns whether the collection already contains the cookie.
*
* @param Cookie $cookie The cookie to check for.
*
* @return bool Whether cookie exists.
*
* @see has()
*/
public function contains(Cookie $cookie): bool
Expand All @@ -230,6 +240,7 @@ public function contains(Cookie $cookie): bool
*
* @param callable $p The predicate.
* @psalm-param callable(Cookie, string):bool $p
*
* @return bool Whether the predicate is true for at least on cookie.
*/
public function exists(callable $p): bool
Expand Down Expand Up @@ -311,6 +322,7 @@ public function isEmpty(): bool
* Populates the cookie collection from an array of 'name' => 'value' pairs.
*
* @param array $array The cookies 'name' => 'value' array to populate from.
*
* @return static Collection created from array.
*/
public static function fromArray(array $array): self
Expand All @@ -332,6 +344,7 @@ public static function fromArray(array $array): self
* Adds the cookies in the collection to response and returns it.
*
* @param ResponseInterface $response Response to add cookies to.
*
* @return ResponseInterface Response with added cookies.
*/
public function addToResponse(ResponseInterface $response): ResponseInterface
Expand All @@ -347,6 +360,7 @@ public function addToResponse(ResponseInterface $response): ResponseInterface
* Creates a copy of the response with cookies set from the collection.
*
* @param ResponseInterface $response Response to set cookies to.
*
* @return ResponseInterface Response with new cookies.
*/
public function setToResponse(ResponseInterface $response): ResponseInterface
Expand All @@ -359,8 +373,10 @@ public function setToResponse(ResponseInterface $response): ResponseInterface
* Populates the cookie collection from a ResponseInterface.
*
* @param ResponseInterface $response The response object to populate from.
* @return static Collection created from response.
*
* @throws Exception
*
* @return static Collection created from response.
*/
public static function fromResponse(ResponseInterface $response): self
{
Expand Down
1 change: 0 additions & 1 deletion tests/CookieCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

final class CookieCollectionTest extends TestCase
{

private CookieCollection $collection;

protected function setUp(): void
Expand Down
7 changes: 3 additions & 4 deletions tests/CookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

final class CookieTest extends TestCase
{

private function getCookieHeader(Cookie $cookie): string
{
$response = new Response();
Expand Down Expand Up @@ -163,7 +162,7 @@ public function fromCookieStringDataProvider(): array
true,
true,
Cookie::SAME_SITE_STRICT
)
),
],
[
'sessionId=e8bb43229de9; Domain=foo.example.com=test; '
Expand All @@ -177,7 +176,7 @@ public function fromCookieStringDataProvider(): array
false,
false,
null
)
),
],
[
'sessionId=e8bb43229de9; Domain=foo.example.com=test; Max-Age=bla',
Expand All @@ -190,7 +189,7 @@ public function fromCookieStringDataProvider(): array
false,
false,
null
)
),
],
];
}
Expand Down

0 comments on commit 2437cc5

Please sign in to comment.