• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SwiftlyPHP / http / c5fcc54f-88c4-4a6d-8b01-1e14f8436386

26 May 2024 09:32PM UTC coverage: 98.621% (-1.4%) from 100.0%
c5fcc54f-88c4-4a6d-8b01-1e14f8436386

push

circleci

clvarley
Fix unit test failures

1 of 1 new or added line in 1 file covered. (100.0%)

4 existing lines in 1 file now uncovered.

286 of 290 relevant lines covered (98.62%)

5.2 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.95
/src/CookieCollection.php
1
<?php declare(strict_types=1);
2

3
namespace Swiftly\Http;
4

5
use Swiftly\Http\Cookie;
6

7
/**
8
 * Utility container for managing HTTP cookies
9
 *
10
 * @api
11
 */
12
class CookieCollection
13
{
14
    /** @var array<non-empty-string,Cookie> $cookies Stored HTTP cookies */
15
    protected $cookies = [];
16

17
    /**
18
     * Create a cookie collection from the `$_COOKIE` super-global.
19
     *
20
     * @return CookieCollection
21
     */
22
    public static function fromGlobals(): self
23
    {
UNCOV
24
        $collection = new self();
×
25

UNCOV
26
        foreach ($_COOKIE as $name => $value) {
×
UNCOV
27
            $collection->add($name, $value);
×
28
        }
29

UNCOV
30
        return $collection;
×
31
    }
32

33
    /**
34
     * Creates a new collection around the given cookies
35
     *
36
     * @param Cookie[] $cookies HTTP cookies
37
     */
38
    public function __construct(array $cookies = [])
39
    {
40
        foreach ($cookies as $cookie) {
6✔
41
            $this->cookies[$cookie->name] = $cookie;
6✔
42
        }
43
    }
44

45
    /**
46
     * Store a cookie in the collection
47
     *
48
     * @param Cookie $cookie Cookie
49
     */
50
    public function set(Cookie $cookie): void
51
    {
52
        $this->cookies[$cookie->name] = $cookie;
1✔
53
        $this->cookies[$cookie->name]->touch();
1✔
54
    }
55

56
    /**
57
     * Create a new cookie within this collection
58
     *
59
     * @param non-empty-string $name Cookie name
60
     * @param string $value          Cookie value
61
     * @param int $expires           Unix timestamp
62
     * @param string $path           URL path
63
     * @param string $domain         Allowed domains
64
     * @param bool $secure           HTTPS only
65
     * @param bool $httponly         HTTP only
66
     * @return Cookie                Created cookie
67
     */
68
    public function add(
69
        string $name,
70
        string $value,
71
        int $expires = 0,
72
        string $path = '',
73
        string $domain = '',
74
        bool $secure = true,
75
        bool $httponly = false
76
    ): Cookie {
77
        $cookie = new Cookie($name, $value);
1✔
78
        $cookie->expires = $expires;
1✔
79
        $cookie->path = $path;
1✔
80
        $cookie->domain = $domain;
1✔
81
        $cookie->secure = $secure;
1✔
82
        $cookie->httponly = $httponly;
1✔
83
        $cookie->touch();
1✔
84

85
        return $this->cookies[$name] = $cookie;
1✔
86
    }
87

88
    /**
89
     * Return a named cookie from the collection
90
     *
91
     * @param non-empty-string $name Cookie name
92
     * @return null|Cookie           Cookie object
93
     */
94
    public function get(string $name): ?Cookie
95
    {
96
        return $this->cookies[$name] ?? null;
4✔
97
    }
98

99
    /**
100
     * Checks to see if the named cookie exists
101
     *
102
     * @psalm-assert-if-true Cookie $this->get($name)
103
     *
104
     * @param non-empty-string $name Cookie name
105
     * @return bool                  Cookie in collection?
106
     */
107
    public function has(string $name): bool
108
    {
109
        return isset($this->cookies[$name]);
4✔
110
    }
111

112
    /**
113
     * Removes the named cookie
114
     *
115
     * Because we (presumably) want to delete the cookie from the client as
116
     * well, we set the expiry date a day into the past and wipe the value.
117
     *
118
     * Most spec conforming browsers treat this as an invalidation.
119
     *
120
     * @param non-empty-string $name Cookie name
121
     */
122
    public function remove(string $name): void
123
    {
124
        if (isset($this->cookies[$name])) {
1✔
125
            $this->cookies[$name]->invalidate();
1✔
126
        }
127
    }
128

129
    /**
130
     * Returns all cookies in this collection
131
     *
132
     * @return array<non-empty-string,Cookie>
133
     */
134
    public function all(): array
135
    {
136
        return $this->cookies;
1✔
137
    }
138
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc